Advertisement
SRD75

convert_RGA_HSV

Jan 6th, 2013
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.70 KB | None | 0 0
  1. function convert_RGA_HSV ($colour) {
  2.     $r = substr($colour,1,2);
  3.     $g = substr($colour,3,2);
  4.     $b = substr($colour,5,2);
  5.    
  6.     $r = $r/255;
  7.     $g = $g/255;
  8.     $b = $b/255;
  9.  
  10.     $h = 0;
  11.     $s = 0;
  12.     $v = 0;
  13.  
  14.     $min = min(min($r, $g),$b);
  15.     $max = max(max($r, $g),$b);
  16.  
  17.     $r = $max-$min;
  18.     $v = $max;
  19.  
  20.     if($r == 0){
  21.         $h = 0;
  22.         $s = 0;
  23.     }
  24.     else {
  25.     $s = $r / $max;
  26.  
  27.     $hr = ((($max - $r) / 6) + ($r / 2)) / $r;
  28.     $hg = ((($max - $g) / 6) + ($r / 2)) / $r;
  29.     $hb = ((($max - $b) / 6) + ($r / 2)) / $r;
  30.  
  31.     if ($r == $max) $h = $hb - $hg;
  32.     else if($g == $max) $h = (1/3) + $hr - $hb;
  33.     else if ($b == $max) $h = (2/3) + $hg - $hr;
  34.  
  35.     if ($h < 0)$h += 1;
  36.     if ($h > 1)$h -= 1;
  37.     }
  38.     return array($h, $s, $v);
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement