Generate colors based on letters in PHP
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 oth开发者_JAVA百科er letters would be the gradually changing spectrum in between?
Try this function:
Generate Gradient Within Hex Range In PHP
A function that generates an array of hex colors that forms a gradient, starting from a hex color and ending with another hex color. The number of gradient steps can also be defined.
function Gradient($HexFrom, $HexTo, $ColorSteps) { $FromRGB['r'] = hexdec(substr($HexFrom, 0, 2)); $FromRGB['g'] = hexdec(substr($HexFrom, 2, 2)); $FromRGB['b'] = hexdec(substr($HexFrom, 4, 2)); $ToRGB['r'] = hexdec(substr($HexTo, 0, 2)); $ToRGB['g'] = hexdec(substr($HexTo, 2, 2)); $ToRGB['b'] = hexdec(substr($HexTo, 4, 2)); $StepRGB['r'] = ($FromRGB['r'] - $ToRGB['r']) / ($ColorSteps - 1); $StepRGB['g'] = ($FromRGB['g'] - $ToRGB['g']) / ($ColorSteps - 1); $StepRGB['b'] = ($FromRGB['b'] - $ToRGB['b']) / ($ColorSteps - 1); $GradientColors = array(); for($i = 0; $i <= $ColorSteps; $i++) { $RGB['r'] = floor($FromRGB['r'] - ($StepRGB['r'] * $i)); $RGB['g'] = floor($FromRGB['g'] - ($StepRGB['g'] * $i)); $RGB['b'] = floor($FromRGB['b'] - ($StepRGB['b'] * $i)); $HexRGB['r'] = sprintf('%02x', ($RGB['r'])); $HexRGB['g'] = sprintf('%02x', ($RGB['g'])); $HexRGB['b'] = sprintf('%02x', ($RGB['b'])); $GradientColors[] = implode(NULL, $HexRGB); } return $GradientColors; } $Gradients = Gradient("FF5B5B", "FFCA5B", 32); foreach($Gradients as $Gradient) { echo "<div style=\"background-color: #".$Gradient."; width: 100px; height: 25px;\"></div>"; }
From https://web.archive.org/web/20160915121028/http://www.geekpedia.com/code163_Generate-Gradient-Within-Hex-Range-In-PHP.html
$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 the Wikipedia page on HSV color space and convering to RGB.
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
I came up with this more simple solution based on the idea of converting the first six alphabetic characters (including spaces) to a relevant hexadecimal character. With the hexadecimal, it is helpful to know that (for example #123456), if you change 1 and 2, it will become more CYAN at the values toward zero, and more white towards values of F. If you change 3 and 4, it will become more MAGENTA at the values toward zero, and more white towards the value of F. If you change five and six it will become more YELLOW at the values towards zero, and more white towards values of F. After making this, I also tried to calculate the complimentary colour (good for background) using a similar strategy. But it's quite hard the middle level values in the hexadecimal become grey (7,8).
$word="word 1";
$letter = substr(strtolower($word), 0,6);
$i=0;
$hd="#";
while ($i<=5){
$l2=substr($letter,$i,1);
if($l2=="a" || $l2=="b"){$hd=$hd."0";}
elseif($l2=="c" || $l2=="d"){$hd=$hd."1";}
elseif($l2=="e"){$hd=$hd."2";}
elseif($l2=="f" || $l2=="g"){$hd=$hd."3";}
elseif($l2=="h" || $l2=="i"){$hd=$hd."4";}
elseif($l2=="j"){$hd=$hd."5";}
elseif($l2=="k" || $l2=="l"){$hd=$hd."6";}
elseif($l2=="m" || $l2=="n"){$hd=$hd."7";}
elseif($l2=="o"){$hd=$hd."8";}
elseif($l2=="p" || $l2=="q"){$hd=$hd."9";}
elseif($l2=="r" || $l2=="s"){$hd=$hd."A";}
elseif($l2=="t"){$hd=$hd."B";}
elseif($l2=="u" || $l2=="v"){$hd=$hd."C";}
elseif($l2=="w" || $l2=="x"){$hd=$hd."D";}
elseif($l2=="y"){$hd=$hd."E";}
elseif($l2=="z" || $l2==" " || $l2==""){$hd=$hd."F";}
$i++;
}
//calculating the complimentary colour
$o=1;
$comp="#";
while($o<=6){
$see=substr($hd,$o,1);
if($see=="F"){$comp=$comp."0";}
elseif($see=="E"){$comp=$comp."1";}
elseif($see=="D"){$comp=$comp."2";}
elseif($see=="C"){$comp=$comp."3";}
elseif($see=="B"){$comp=$comp."4";}
elseif($see=="A"){$comp=$comp."5";}
elseif($see=="9"){$comp=$comp."F";}
elseif($see=="8"){$comp=$comp."0";}
elseif($see=="7"){$comp=$comp."F";}
elseif($see=="6"){$comp=$comp."0";}
elseif($see=="5"){$comp=$comp."F";}
elseif($see=="4"){$comp=$comp."B";}
elseif($see=="3"){$comp=$comp."C";}
elseif($see=="2"){$comp=$comp."D";}
elseif($see=="1"){$comp=$comp."E";}
elseif($see=="0"){$comp=$comp."F";}
$o++;
}
echo" hexadecimal is $hd, complimentary colour is $comp";
精彩评论