Color Inverter

,

I found this nice little function to take a color and give you its inverse color.  It is pretty useful to display a color on a background that is opposite so you can ensure it will display correctly without having to know what the color is.

<?php
// color_inverse function by Jonas John
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;
}

 

 

Skills

Posted on

May 18, 2018

Submit a Comment

Your email address will not be published. Required fields are marked *