Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End RGB Color Darkener…

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #32002
    nchipping
    Member

    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?



    /**
    * 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.

    #54434
    nchipping
    Member

    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.

    #54380

    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

    #74620
    nchipping
    Member

    That worked – thank you!!

Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘Back End’ is closed to new topics and replies.