Advertisement
patryk

Untitled

Apr 26th, 2015
581
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.73 KB | None | 0 0
  1. void rgbToHSV(double r, double g, double b, double hsv[]) {
  2.   double computedH, computedS, computedV;
  3.  double minRGB, maxRGB;
  4.  double d = 0, h = 0;
  5.  
  6.   r = r/1023; g = g/1023; b = b/1023;
  7.   minRGB = min(r,min(g,b));
  8.   maxRGB = max(r,max(g,b));
  9.  
  10.   if (minRGB == maxRGB) {
  11.     // Black-gray-white
  12.     computedV = minRGB;
  13.     hsv[0] = 0; hsv[1] = 0; hsv[2] = computedV;
  14.   } else {
  15.     // Colors other than black-gray-white:
  16.     d = (r == minRGB) ? g-b : ((b == minRGB) ? r-g : b-r);
  17.     h = (r == minRGB) ? 3 : ((b == minRGB) ? 1 : 5);
  18.  
  19.     computedH = 60*(h - (d/(maxRGB - minRGB)));
  20.     computedS = (maxRGB - minRGB)/maxRGB;
  21.     computedV = maxRGB;
  22.    
  23.     hsv[0] = computedH; hsv[1] = computedS; hsv[2] = computedV;
  24.   }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement