Forum Moderators: coopster

Message Too Old, No Replies

determining the opposite colour of a HEX value

         

ahmedtheking

9:23 pm on Sep 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is there a function prewritten that determines the opposite colour of a HEX value?

ahmedtheking

9:26 pm on Sep 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



After some googling, I got me this:

<?php

function invert_colour($start_colour) {
$colour_red = hexdec(substr($start_colour, 1, 2));
$colour_green = hexdec(substr($start_colour, 3, 2));
$colour_blue = hexdec(substr($start_colour, 5, 2));

$new_red = dechex(255 - $colour_red);
$new_green = dechex(255 - $colour_green);
$new_blue = dechex(255 - $colour_blue);

if (strlen($new_red) == 1) {$new_red .= '0';}
if (strlen($new_green) == 1) {$new_green .= '0';}
if (strlen($new_blue) == 1) {$new_blue .= '0';}

$new_colour = '#'.$new_red.$new_green.$new_blue;

return $new_colour;
}

echo '<table border="0" cellpadding="1" cellspacing="2" width="200">';

$start_colour = '#ffa040';
echo '<tr><td align="center" bgcolor="'.$start_colour.'"><b><font color="'.invert_colour($start_colour).'">'.$start_colour.'</font></b>< /td></tr>';
echo '<tr><td align="center" bgcolor="'.invert_colour($start_colour).'"><b><font color="'.$start_colour.'">'.invert_colour($start_colour).'</font></b>< /td></tr>';

$start_colour = '#eeff99';
echo '<tr><td align="center" bgcolor="'.$start_colour.'"><b><font color="'.invert_colour($start_colour).'">'.$start_colour.'</font></b>< /td></tr>';
echo '<tr><td align="center" bgcolor="'.invert_colour($start_colour).'"><b><font color="'.$start_colour.'">'.invert_colour($start_colour).'</font></b>< /td></tr>';

$start_colour = '#ff00ff';
echo '<tr><td align="center" bgcolor="'.$start_colour.'"><b><font color="'.invert_colour($start_colour).'">'.$start_colour.'</font></b>< /td></tr>';
echo '<tr><td align="center" bgcolor="'.invert_colour($start_colour).'"><b><font color="'.$start_colour.'">'.invert_colour($start_colour).'</font></b>< /td></tr>';

echo '</table>';

?>