home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 2.ddi / COLOR.DI$ / RGB2HSV.M < prev    next >
Encoding:
Text File  |  1993-03-07  |  1.0 KB  |  38 lines

  1. function H = rgb2hsv(M);
  2. %RGB2HSV Red-green-blue to hue-saturation-value conversion.
  3. %    H = RGB2HSV(M) converts an RGB color map to an HSV color map.
  4. %    Each map is a matrix with any number of rows, exactly three columns,
  5. %    and elements in the interval 0 to 1.  The columns of the input matrix,
  6. %    M, represent intensity of red, blue and green, respectively.  The
  7. %    columns of the resulting output matrix, H, represent hue, saturation
  8. %    and color value, respectively.
  9. %
  10. %    See also HSV2RGB, COLORMAP, RGBPLOT. 
  11.  
  12. %    See Alvy Ray Smith, Color Gamut Transform Pairs, SIGGRAPH '78.
  13. %    C. B. Moler, 8-17-86, 5-10-91, 2-2-92.
  14. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  15.  
  16. v = max(M')';
  17. s = zeros(size(v));
  18. h = zeros(size(v));
  19. d = (v - min(M')');
  20. k = find(v);
  21. s(k) = d(k)./v(k);
  22. z = ~d;
  23. d = d + z;
  24. r = M(:,1);
  25. g = M(:,2);
  26. b = M(:,3);
  27. k = find(r == v);
  28. h(k) = (g(k) - b(k))./d(k);
  29. k = find(g == v);
  30. h(k) = 2 + (b(k) - r(k))./d(k);
  31. k = find(b == v);
  32. h(k) = 4 + (r(k) - g(k))./d(k);
  33. h = h/6;
  34. k = find(h < 0);
  35. h(k) = h(k) + 1;
  36. h = (~z).*h;
  37. H = [h s v];
  38.