Skip to content

Instantly share code, notes, and snippets.

@rjindael
Last active August 11, 2021 01:41
Show Gist options
  • Save rjindael/57539ea567114e634bef50eaf42efa78 to your computer and use it in GitHub Desktop.
Save rjindael/57539ea567114e634bef50eaf42efa78 to your computer and use it in GitHub Desktop.
<?php
function lua_mod($a, $b)
{
return ($a - floor($a / $b) * $b);
}
function compute_name_color($name, $modern_colors = true)
{
$base_palette = [
[ "R" => 107, "G" => 50, "B" => 124 ],
[ "R" => 218, "G" => 133, "B" => 65 ],
[ "R" => 245, "G" => 205, "B" => 48 ],
[ "R" => 232, "G" => 186, "B" => 200 ],
[ "R" => 215, "G" => 197, "B" => 154 ]
];
$modern_palette = [
[ "R" => 253, "G" => 41, "B" => 67 ],
[ "R" => 1, "G" => 162, "B" => 255 ],
[ "R" => 2, "G" => 184, "B" => 87 ]
];
$modern_palette = array_merge($modern_palette, $base_palette);
$old_palette = [
[ "R" => 196, "G" => 40, "B" => 28 ],
[ "R" => 13, "G" => 105, "B" => 172 ],
[ "R" => 39, "G" => 70, "B" => 45 ]
];
$old_palette = array_merge($old_palette, $base_palette);
$palette = ($modern_colors ? $modern_palette : $old_palette);
$val = 0;
for ($i = 0; $i < strlen($name); $i++)
{
$cv = ord($name[$i]);
$ri = strlen($name) - $i;
if (lua_mod(strlen($name), 2) == 1)
{
$ri--;
}
if (lua_mod($ri, 4) >= 2)
{
$cv = -$cv;
}
$val = $val + $cv;
}
return $palette[lua_mod($val, count($palette))];
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment