Color inverse

This function inverses a color to it's opposite.
(White to black, blue to yellow, etc.)

Snippet information

Author:
Jonas John

License:
Public Domain

Language:
PHP

Created:
06/13/2006

Updated:
06/13/2006

Tags:


function color_inverse($color){
    $color = str_replace('#', '', $color);
    if (strlen($color) != 6){ return '000000'; }
    $rgb = '';
    for ($x=0;$x<3;$x++){
        $c = 255 - hexdec(substr($color,(2*$x),2));
        $c = ($c < 0) ? 0 : dechex($c);
        $rgb .= (strlen($c) < 2) ? '0'.$c : $c;
    }
    return '#'.$rgb;
}


Found a bug? Or do you have a better solution for this?
Feel free to leave a message:

Add a comment


Leave a comment

dasdas June 03, 2010 at 10:30
sadasdas