Grow your CSS skills. Land your dream job.

RGB Color Darkener…

  • # March 14, 2011 at 11:04 am

    I am in need of making this color class work well – I am trying to have it so that if I give it a color, it will take that color, make it 20% darker, and return it to me. Any pointers on what I’m doing wrong here?


    < ?php

    /**
    * COLOR
    * For use with changing colors from HSV to RGB & the other way around
    */
    class Color
    {
    private $color;
    public function HSVtoRGB($tmp)
    {
    $tmp = explode(",",$tmp);
    $H = $tmp[0] / 360;
    $S = $tmp[1] / 100;
    $V = $tmp[2] / 100;

    if ($S == 0) {
    $R = $V * 255;
    $G = $V * 255;
    $B = $V * 255;
    } else {
    $var_h = $H * 6;
    $var_i = floor($var_h);
    $var_1 = $V * (1 - $S);
    $var_2 = $V * (1 - $S * ($var_h - $var_i));
    $var_3 = $V * (1 - $S * (1 - ($var_h - $var_i)));

    if ($var_i == 0) {$var_r = $V; $var_g = $var_3; $var_b = $var_1;
    } else if ($var_i == 1) {$var_r =$var_2;$var_g = $V;$var_b =$var_1;
    } else if ($var_i == 2) {$var_r =$var_1;$var_g = $V;$var_b =$var_3;
    } else if ($var_i == 3) {$var_r =$var_1;$var_g =$var_2;$var_b = $V;
    } else if ($var_i == 4) {$var_r =$var_3;$var_g =$var_1;$var_b = $V;
    } else {$var_r = $V;$var_g =$var_1;$var_b =$var_2;
    }
    $R = $var_r * 255;
    $G = $var_g * 255;
    $B = $var_b * 255;
    }
    $color = round($R) . "," . round($G) . "," . round($B);
    return $color;
    }

    public function RGBtoHSV($tmp)
    {
    $tmp = explode(",",$tmp);
    $R = $tmp[0] / 255.0;
    $G = $tmp[1] / 255.0;
    $B = $tmp[2] / 255.0;
    $H = 0;
    $S = 0;
    $V = 0;

    $min = min(min($R, $G),$B);
    $max = max(max($d, $G),$B);
    $delta = $max - $min;

    $V = $max;
    if($delta == 0) {
    $H = 0;
    $S = 0;
    } else {
    $S = $delta / $max;
    $dR = ((($max - $R) / 6) + ($delta / 2)) / $delta;
    $dG = ((($max - $G) / 6) + ($delta / 2)) / $delta;
    $dB = ((($max - $B) / 6) + ($delta / 2)) / $delta;

    if ($R == $max) {
    $H = $dB - $dG;
    } else if($G == $max) {
    $H = (1/3) + $dR - $dB;
    } else {
    $H = (2/3) + $dG - $dR;
    }
    if ($H < 0) { $H += 1; }
    if ($H > 1) { $H -= 1; }
    }
    $color = round($H*360) . ',' . round($S*100) . ',' . round($V*100);
    return $color;
    }

    public function RGBtoDark($tmp)
    {
    $HSV = $this->RGBtoHSV($tmp);
    $HSV = explode(",",$HSV);
    $HSV[2] = $HSV[2] - 20;
    $HSV = $HSV[0] . "," . $HSV[1] . "," . $HSV[2];
    $RGB = $this->HSVtoRGB($HSV);
    return $RGB;
    }
    }
    ?>

    By the way – the only reason I’m converting to HSV, and then back to RGB is that I saw that Photoshop can take HSV values in percentages and I tried to follow that. Thanks.

    # March 23, 2011 at 1:48 pm

    Does anyone have any ideas on this one? I really don’t know much about color conversion, and any help of getting a more accurate conversion would be very, very helpful.

    # March 23, 2011 at 6:50 pm

    Howdy, I can’t really help aside from point to some work that has already been done. Perhaps this guy’s work, and tutorial, will help you figure this out…

    http://serennu.com/colour/rgbtohsl.php

    # June 1, 2011 at 2:04 pm

    That worked – thank you!!

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".