Menu
Hmm.. Ok, I have no idea on how to go about this.. But, I think? Maybe there is a way.. Can I change a HEX code like #0066FF, and stip down the color, but leave the saturation, and brightness in tact, IE make it B/W.. then change the HUE to another color? So say I had the hex code #0066FF and stripped it down, and added 111 HUE.. and the result was #71BF40.. can I do this in PHP? (SOrry, I have a bad way of explaining things)
Re-explination: Basically change a HEX codes HUE, while leaving the brightness and saturation in tact ?
Thanks for the help in advanced guys/gals ?
Kyle
<i>
</i>luminance = (0.30 * red) + (0.59 * green) + (0.11 * blue)
[code=php]
function luminance($rgb)
{
if(preg_match('/^#{0,1}([a-f0-9]{6,6})$/i', $rgb, $matches))
{
$colors = chunk_split($matches[1],2);
$rgbHex = explode("n", $colors);
foreach($rgbHex as $key => $val)
{
$rgbDec[$key] = hexdec($val);
}
$luminance = round(
($rgbDec[0] * .3) + ($rgbDec[1] * .59) + ($rgbDec[2] * .11), 0);
return(sprintf("#%02X%02X%02X", $luminance, $luminance, $luminance));
}
else
{
return(FALSE); // input not in "#xxxxxx" or "xxxxxx" format
}
}[/code]
[code=php]
<?php
// Input is $var_r, $var_g and $var_b from above
// Output is HSL equivalent as $h, $s and $l — these are again expressed as fractions of 1, like the input values
function rgb_hsl($rgb){
$var_min = min($rgb[0],$rgb[1],$rgb[2]);
$var_max = max($rgb[0],$rgb[1],$rgb[2]);
$del_max = $var_max - $var_min;
$l = ($var_max + $var_min) / 2;
if ($del_max == 0){
$h = 0;
$s = 0;
}
else{
if ($l < 0.5){
$s = $del_max / ($var_max + $var_min);
}
else{
$s = $del_max / (2 - $var_max - $var_min);
}
$del_r = ((($var_max - $rgb[0]) / 6) + ($del_max / 2)) / $del_max;
$del_g = ((($var_max - $rgb[1]) / 6) + ($del_max / 2)) / $del_max;
$del_b = ((($var_max - $rgb[2]) / 6) + ($del_max / 2)) / $del_max;
if ($rgb[0] == $var_max){
$h = $del_b - $del_g;
}
elseif ($rgb[1] == $var_max){
$h = (1 / 3) + $del_r - $del_b;
}
elseif ($rgb[2] == $var_max){
$h = (2 / 3) + $del_g - $del_r;
}
if ($h < 0){
$h += 1;
}
if ($h > 1){
$h -= 1;
}
}
return "$h,$s,$l";
}
function luminance($rgb,$fav)
{
if(preg_match('/^#{0,1}([a-f0-9]{6,6})$/i', $rgb, $matches))
{
$colors = chunk_split($matches[1],2);
$rgbHex = explode("n", $colors);
foreach($rgbHex as $key => $val)
{
$rgbDec[$key] = hexdec($val);
}
//// yeah
$first_sl = rgb_hsl(array($rgbDec[0],$rgbDec[1],$rgbDec[2]));
$first_sl = explode(",",$first_sl);
}
else
{
return(FALSE); // input not in "#xxxxxx" or "xxxxxx" format
}
if(preg_match('/^#{0,1}([a-f0-9]{6,6})$/i', $fav, $matches))
{
$colors = chunk_split($matches[1],2);
$rgbHex = explode("n", $colors);
foreach($rgbHex as $key => $val)
{
$rgbDec[$key] = hexdec($val);
}
//// yeah
$second_hsl = rgb_hsl(array($rgbDec[0],$rgbDec[1],$rgbDec[2]));
$final = explode(",",$second_hsl);
// Function to convert hue to RGB, called from above
function hue_2_rgb($v1,$v2,$vh){
if ($vh < 0){
$vh += 1;
}
if ($vh > 1){
$vh -= 1;
}
if ((6 * $vh) < 1){
return ($v1 + ($v2 - $v1) * 6 * $vh);
}
if ((2 * $vh) < 1){
return ($v2);
}
if ((3 * $vh) < 2){
return ($v1 + ($v2 - $v1) * ((2 / 3 - $vh) * 6));
}
return ($v1);
}
// meow //
if ($first_sl[1] == 0){
$r = $first_sl[2] * 255;
$g = $first_sl[2] * 255;
$b = $first_sl[2] * 255;
}
else{
if ($first_sl[2] < 0.5){
$var_2 = $first_sl[2] * (1 + $first_sl[1]);
}
else{
$var_2 = ($first_sl[2] + $first_sl[1]) - ($first_sl[1] * $first_sl[2]);
}
$var_1 = 2 * $first_sl[2] - $var_2;
$r = 255 * hue_2_rgb($var_1,$var_2,$final[0] + (1 / 3));
$g = 255 * hue_2_rgb($var_1,$var_2,$final[0]);
$b = 255 * hue_2_rgb($var_1,$var_2,$final[0] - (1 / 3));
}
$rhex = sprintf("%02X",round($r));
$ghex = sprintf("%02X",round($g));
$bhex = sprintf("%02X",round($b));
$rgbhex = $rhex.$ghex.$bhex;
return "#$rgbhex";
}
else
{
return(FALSE); // input not in "#xxxxxx" or "xxxxxx" format
}
}
echo "<font color="" . luminance("#1F4174","#FF0000") . "">OMG... it.. WORKS! somewhat?</font>";
?>
[/code]
Woot! Uhm, would you mind letting me in on how? :-I really thought this question was about images. I didn't realise it was just for changing a font colour.. . lol thanks ?[/QUOTE]
<i>
</i>luminance = (0.30 * red) + (0.59 * green) + (0.11 * blue)
[/QUOTE]
I was wondering where got the information you are basing that on? I guess you are probably somewhere near it for the human eye in daylight. With grayscaling images I normally put more bias on the red channel though [code=php]$rgbDec[$key] = hexdec($val); [/code]
[/QUOTE]
That line occurs twice. Change it to the following in both instances:[code=php]$rgbDec[$key] = (hexdec($val)/256);[/code]
[code=php]echo "<font color="" . luminance("#1F4174","#FF0000") . "">OMG... it.. WORKS! somewhat?</font>";[/code]
[/QUOTE]
By the way that HTML sucks. Try writing it like this:[code=php]echo '<span style="color:' . luminance("##1F4174","#FF0000") . '">OMG... it.. WORKS! somewhat?</span>';[/code]
[code=html]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<p><font size=2>size=2</font></p>
</body>
</html>[/code]
I really thought this question was about images. I didn't realise it was just for changing a font colour.I was wondering where got the information you are basing that on? I guess you are probably somewhere near it for the human eye in daylight. With grayscaling images I normally put more bias on the red channel though[URL=http://bokehman.com/grayscale/]as can be seen here[/URL] but this is more for effect than anything else. Also with images the blue channel contains the majority of the noise so its good to give it little importance. Going back to the eye, as the light level falls to the extreme the sensitivity migrates to the blue end of the spectrum.[/QUOTE]
0.1.9 — BETA 6.17