PHP HSV to RGB formula comprehension

后端 未结 6 1859
挽巷
挽巷 2020-12-01 16:37

I can convert RGB values to HSV with the following code...

 $r = $r/255;
 $g = $g/255;
 $b = $b/255;

 $h = 0;
 $s = 0;
 $v         


        
6条回答
  •  日久生厌
    2020-12-01 16:56

    Found this post too late, my alternate take on it:

    hsv2rgb function PHP

    hue: 0-360, sat: 0-100, val: 0-100

      function hsv2rgb($hue,$sat,$val) {;
        $rgb = array(0,0,0);
        //calc rgb for 100% SV, go +1 for BR-range
        for($i=0;$i<4;$i++) {
          if (abs($hue - $i*120)<120) {
            $distance = max(60,abs($hue - $i*120));
            $rgb[$i % 3] = 1 - (($distance-60) / 60);
          }
        }
        //desaturate by increasing lower levels
        $max = max($rgb);
        $factor = 255 * ($val/100);
        for($i=0;$i<3;$i++) {
          //use distance between 0 and max (1) and multiply with value
          $rgb[$i] = round(($rgb[$i] + ($max - $rgb[$i]) * (1 - $sat/100)) * $factor);
        }
        $rgb['html'] = sprintf('#%02X%02X%02X', $rgb[0], $rgb[1], $rgb[2]);
        return $rgb;
      }
    

提交回复
热议问题