How would I generate a color based on what letter a string begins with - perhaps A could be blue, Z could be green and the other letters would be the gradually changing spectrum in between?
Thanks in advance.
How would I generate a color based on what letter a string begins with - perhaps A could be blue, Z could be green and the other letters would be the gradually changing spectrum in between?
Thanks in advance.
$str = iconv("CURRENT CHARSET HERE", "ASCII//TRANSLIT", $orig_string);
$letter = ucfirst($str);
//A is 65, Z is 90
$hue = 2*pi() * ((ord($letter) - 65)/(90-65));
This is will give you the hue in radians. Then, it's just a matter of picking a certain saturation and brightness and convert to RGB or whatever. See wikipedia.
You're definitely going to want to use HSV, since it's trivial to smoothly transition from one hue to another in that space.
Probably not the most efficient code in the world, but here goes. There's a little test page at the bottom of the code which you can remove/disregard, of course.
<?php
// RGB_TO_HSV copied from http://www.actionscript.org/forums/showthread.php3?t=50746
function HSV_TO_RGB ($H, $S, $V) // HSV Values:Number 0-1
{ // RGB Results:Number 0-255
$RGB = array();
if($S == 0)
{
$R = $G = $B = $V * 255;
}
else
{
$var_H = $H * 6;
$var_i = floor( $var_H );
$var_1 = $V * ( 1 - $S );
$var_2 = $V * ( 1 - $S * ( $var_H - $var_i ) );
$var_3 = $V * ( 1 - $S * (1 - ( $var_H - $var_i ) ) );
if ($var_i == 0) { $var_R = $V ; $var_G = $var_3 ; $var_B = $var_1 ; }
else if ($var_i == 1) { $var_R = $var_2 ; $var_G = $V ; $var_B = $var_1 ; }
else if ($var_i == 2) { $var_R = $var_1 ; $var_G = $V ; $var_B = $var_3 ; }
else if ($var_i == 3) { $var_R = $var_1 ; $var_G = $var_2 ; $var_B = $V ; }
else if ($var_i == 4) { $var_R = $var_3 ; $var_G = $var_1 ; $var_B = $V ; }
else { $var_R = $V ; $var_G = $var_1 ; $var_B = $var_2 ; }
$R = $var_R * 255;
$G = $var_G * 255;
$B = $var_B * 255;
}
$RGB['R'] = $R;
$RGB['G'] = $G;
$RGB['B'] = $B;
return $RGB;
}
function getColorForWord($word) {
// get the percent of the first letter ranging from 0-1
$first_letter_code = (ord(strtolower($word[0]))-97)/25.0;
// add a phase depending on where you want to start on the color spectrum
// red is 0, green is 0.25, cyan is 0.5, blue is ~0.75, and 1 is back to red
$hue = $first_letter_code + 0.25;
// you may also want to divide by how much of the spectrum you want to cover
// (making the colors range only from green to blue, for instance)
// but i'll leave that as an exercise
// constrain it to 0-1
if ($hue > 1.0)
$hue -= 1.0;
// the second value is the saturation ("colorfulness", ranging from gray to fully-colored)
// the third is the value (brightness)
$rgb = HSV_TO_RGB($hue, 1, 0.75);
$hexstring = "#";
foreach ($rgb as $c)
$hexstring .= str_pad(dechex($c), 2, "0", STR_PAD_LEFT);
return $hexstring;
}
?>
<html>
<head>
</head>
<body>
<form method="POST" action="<?=$_SERVER["PHP_SELF"]?>">
<input type="text" name="target_word" />
<?php
if ($_REQUEST["sub"] && $_REQUEST["target_word"] != "") {
print "<span style=\"font-weight: bold; color: ".getColorForWord($_REQUEST["target_word"]).";\">".$_REQUEST["target_word"]."</span>";
}
?>
<br />
<input type="submit" name="sub" value="Colorize" />
</form>
</body>
I would specify the RGB code of the first color (100,100,100) and the RGB code of the last color (200,200,200) and basically do
1..25 B..Y
resultingR = firstR + (lastR-firstR) * (1..25/26) resultingG = firstG + (lastG-firstG) * (1..25/26) resultingB = firstB + (lastB-firstB) * (1..25/26)
so B would give 100 + floor((200-100) * (1 / 26)) 104,104,104
and Y would be 100 + floor((200-100) * (1 / 26)) 196,196,196
This is a base code, but it will allow gradient on all the 3 color, or only 1 (example 100,100,100 to 100,100,200) which would do a gradient toward Blue