At 10/9/2002, you wrote:
>Hi,
>
>I have a site whereby the user can select the colour of links for their
>individual sections.
>
>What I would like to do is to get the exact opposite colour of the one that
>they choose and use that in the 'hover over' option of the <a> tag.
>
>The value for the colour is stored in hex.
>
>To add to this, the user can also choose the background colour. I have made
>sure that they cannot have the same background colour as colour of the link,
>but obviously if the background colour is the exact opposite of the link
>colour, then when they hover over the link, it would 'disappear'. In this
>case I would like to choose HALFWAY between the two sets of colours. I know
>that this might not always produce the best colour combinations, but I'm
>trying to get it so that the links always 'stand out'. I cannot let them
>choose the colour of the 'Hover Over' option unfortunately.
>
>Any suggestions as to how to achieve this, especially the calculations, or
>for that matter improve upon it, would be most appreciated.

Hi,

I struggled with the same issues a few months ago, and coded a function for 
brightening or darkening a HEX color.
Maybe it can help you in your dilemma.

The code converts HEX to RGB, multiplies each color channel
and converts back to HEX. This way the hue stays correct. The function
could of course have been written in more concise manner, but as I'm
only learning PHP more deeply, I like to write code that even I can
understand :)

function hexcolorshifter($color,$amount)
{
         $r = hexdec(substr($color, 0, 2));
         $g = hexdec(substr($color, 2, 2));
         $b = hexdec(substr($color, 4, 2));
         $new_r = abs(intval($r*$amount)); if ($new_r>255) $new_r=255;
         $new_g = abs(intval($g*$amount)); if ($new_g>255) $new_g=255;
         $new_b = abs(intval($b*$amount)); if ($new_b>255) $new_b=255;
         $hex_r =  sprintf("%02X",$new_r);
         $hex_g =  sprintf("%02X",$new_g);
         $hex_b =  sprintf("%02X",$new_b);
         // print line is only for debugging:
         // print " R:" . $new_r  . "," .  $hex_r . " G:" .
$new_g . "," .  $hex_g . " B:" . $new_b  . "," .  $hex_b . "<br>";
         $color = $hex_r .$hex_g .$hex_b;
         return $color;
}


Use it like this:
$my_bg_color = hexcolorshifter($my_bg_color,".8");

If you brighten a color, each channel will clip at 255, so in
situations where you have light source color you will most likely have
hue shifts if channels don't clip 'equally'.





-------------------------
Pekka Saarinen
http://photography-on-the.net
-------------------------



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to