/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] HEX Hue Change

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

to post a comment
PHP

13 Comments(s)

Copy linkTweet thisAlerts:
@bokehJun 28.2006 — Yes!
Copy linkTweet thisAlerts:
@Kyleva2204authorJun 29.2006 — Woot! Uhm, would you mind letting me in on how? :-.. lol thanks ?
Copy linkTweet thisAlerts:
@NogDogJun 29.2006 — Due to the way the human eye processes colors, you probably want to use this formula for computing luminance:
<i>
</i>luminance = (0.30 * red) + (0.59 * green) + (0.11 * blue)

So, if you wanted to convert a rgb color to the gray shade with the same (approximate) luminance, you might do:
[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]
Copy linkTweet thisAlerts:
@Kyleva2204authorJun 29.2006 — Thanks NogDog! ?.. That wasn't exactly what I was looking for.. but w/e I did some googling and after about 2 hours of trial and error, and messing around and not knowing what the heck to do, I came up with this code.. ? and surprisingly it works! O_O I was shocked it worked, but yeah ? it does.. wh00t, if you see any errors, please tell me.. heh
[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]


So thanks ? hehe..
Copy linkTweet thisAlerts:
@bokehJun 29.2006 — Woot! Uhm, would you mind letting me in on how? :-.. lol thanks ?[/QUOTE]I really thought this question was about images. I didn't realise it was just for changing a font colour.<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 [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.
Copy linkTweet thisAlerts:
@Kyleva2204authorJun 29.2006 — oops, hmm it seems that my code returns a really, extra long HEX code.. and im illiterate in Colors and HEX in PHP.. or in general.. :-... Does anyone think they can help a tad?.
Copy linkTweet thisAlerts:
@bokehJun 29.2006 — [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]
Copy linkTweet thisAlerts:
@Kyleva2204authorJun 29.2006 — WOAH! Thanks a bunch man! ?

Thanks NogDog and Bokeh for all the help, greatly appreciated!
Copy linkTweet thisAlerts:
@bokehJun 29.2006 — [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]
Copy linkTweet thisAlerts:
@Kyleva2204authorJun 29.2006 — Yeah, I haven't used the font tag in so long.. It was just a quicker way, for me.. cuz im just now getting into CSS ans styles. it was just an easier route, I 98.9% of the time use CSS to identify Styles ?
Copy linkTweet thisAlerts:
@bokehJun 29.2006 — According to my HTML reference book (purchased in 1999) [I]<font>[/I] is depracated. Also the following [B]does not validate[/B] under a strict DOCTYPE although it does under a transitional.[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]
Copy linkTweet thisAlerts:
@NogDogJun 29.2006 — 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]
I've seen that weighting (.3/.59/.11) referenced in a few different web articles. Admittedly it's an approximation, and it's certainly not an area in which I'd claim expertise -- it's just a decent starting point. If converting a photo to gray-scale, you'd likely need to tweak those numbers on a case-by-case basis depending on which colors are dominant, as well as other artistic factors for which I'm a total ignoramus (nobody has ever accused me of being particularly artistic).
Copy linkTweet thisAlerts:
@bokehJun 29.2006 — I guess they are two very separate things. You are talking sensitivity and I'm thinking about photography. In black and white photography the #25 and #29 filters are a favourite. #25 is dark red and #29 is bordering on infra red. But this is more for artistic impression though. It allows blue skies and to be nearly black and plants to be very dark. It also makes skin look a lot smoother. This is demonstrated pretty well by [URL=http://www.abmedia.com/astro/newmexico/anseladams.jpg]"Moonrise" by Ansel Adams[/URL] which was taken in full daylight with a #29 filter. Extremely moody but certainly does not convey a realistic sense of luminence.
×

Success!

Help @Kyleva2204 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...