Your Ad Here

Posted By

michaJlS on 02/10/11


Tagged

RGB hsv


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

alexteg


RGB2HSV


 / Published in: PHP
 

i found it somewhere...

  1. function RGB_TO_HSV ($R, $G, $B) // RGB Values:Number 0-255
  2. { // HSV Results:Number 0-1
  3. $HSL = array();
  4.  
  5. $var_R = ($R / 255);
  6. $var_G = ($G / 255);
  7. $var_B = ($B / 255);
  8.  
  9. $var_Min = min($var_R, $var_G, $var_B);
  10. $var_Max = max($var_R, $var_G, $var_B);
  11. $del_Max = $var_Max - $var_Min;
  12.  
  13. $V = $var_Max;
  14.  
  15. if ($del_Max == 0)
  16. {
  17. $H = 0;
  18. $S = 0;
  19. }
  20. else
  21. {
  22. $S = $del_Max / $var_Max;
  23.  
  24. $del_R = ( ( ( $max - $var_R ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max;
  25. $del_G = ( ( ( $max - $var_G ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max;
  26. $del_B = ( ( ( $max - $var_B ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max;
  27.  
  28. if ($var_R == $var_Max) $H = $del_B - $del_G;
  29. else if ($var_G == $var_Max) $H = ( 1 / 3 ) + $del_R - $del_B;
  30. else if ($var_B == $var_Max) $H = ( 2 / 3 ) + $del_G - $del_R;
  31.  
  32. if (H<0) $H++;
  33. if (H>1) $H--;
  34. }
  35.  
  36. $HSL['H'] = $H;
  37. $HSL['S'] = $S;
  38. $HSL['V'] = $V;
  39.  
  40. return $HSL;
  41. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: micksp on October 24, 2011

It contains at least two errors: $max in lines 24-26 is undefined. My guess is that should be: $var_Max (which i think is a terrible name...) . Furthermore H in lines 32 and 33 should be $H. regards, Michael

You need to login to post a comment.