7017
|
1 ## Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007 |
|
2 ## Kai Habel |
3803
|
3 ## |
|
4 ## This file is part of Octave. |
|
5 ## |
|
6 ## Octave is free software; you can redistribute it and/or modify it |
|
7 ## under the terms of the GNU General Public License as published by |
7016
|
8 ## the Free Software Foundation; either version 3 of the License, or (at |
|
9 ## your option) any later version. |
3803
|
10 ## |
|
11 ## Octave is distributed in the hope that it will be useful, but |
|
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 ## General Public License for more details. |
|
15 ## |
|
16 ## You should have received a copy of the GNU General Public License |
7016
|
17 ## along with Octave; see the file COPYING. If not, see |
|
18 ## <http://www.gnu.org/licenses/>. |
3803
|
19 |
|
20 ## -*- texinfo -*- |
6547
|
21 ## @deftypefn {Function File} {@var{hsv_map} =} rgb2hsv (@var{rgb_map}) |
7375
|
22 ## Transform a colormap or image from the rgb space to the hsv space. |
3803
|
23 ## |
|
24 ## A color n the RGB space consists of the red, green and blue intensities. |
|
25 ## |
|
26 ## In the HSV space each color is represented by their hue, saturation |
|
27 ## and value (brightness). Value gives the amount of light in the color. |
7001
|
28 ## Hue describes the dominant wavelength. |
5642
|
29 ## Saturation is the amount of Hue mixed into the color. |
|
30 ## @seealso{hsv2rgb} |
3803
|
31 ## @end deftypefn |
|
32 |
|
33 ## Author: Kai Habel <kai.habel@gmx.de> |
|
34 ## Adapted-by: jwe |
|
35 |
|
36 function hsval = rgb2hsv (rgb) |
|
37 |
|
38 if (nargin != 1) |
6046
|
39 print_usage (); |
3803
|
40 endif |
|
41 |
7375
|
42 ## If we have an image convert it into a color map. |
|
43 if (ismatrix (rgb) && ndims (rgb) == 3) |
|
44 is_image = true; |
|
45 Sz = size (rgb); |
|
46 rgb = [rgb(:,:,1)(:), rgb(:,:,2)(:), rgb(:,:,3)(:)]; |
|
47 ## Convert to a double image. |
|
48 if (isinteger (rgb)) |
|
49 C = class (rgb); |
|
50 low = double (intmin (C)); |
|
51 high = double (intmax (C)); |
|
52 rgb = (double (rgb) - low) / (high - low); |
|
53 endif |
|
54 else |
|
55 is_image = false; |
|
56 endif |
|
57 |
4030
|
58 if (! ismatrix (rgb) || columns (rgb) != 3) |
3803
|
59 error ("rgb2hsv: argument must be a matrix of size n x 3"); |
|
60 endif |
|
61 |
3904
|
62 ## get the max and min |
|
63 s = min (rgb')'; |
|
64 v = max (rgb')'; |
3803
|
65 |
3904
|
66 ## set hue to zero for undefined values (gray has no hue) |
|
67 h = zeros (size (v)); |
|
68 notgray = (s != v); |
|
69 |
|
70 ## blue hue |
|
71 idx = (v == rgb(:,3) & notgray); |
|
72 if (any (idx)) |
|
73 h(idx) = 2/3 + 1/6 * (rgb(idx,1) - rgb(idx,2)) ./ (v(idx) - s(idx)); |
|
74 endif |
3803
|
75 |
3904
|
76 ## green hue |
|
77 idx = (v == rgb(:,2) & notgray); |
|
78 if (any (idx)) |
|
79 h(idx) = 1/3 + 1/6 * (rgb(idx,3) - rgb(idx,1)) ./ (v(idx) - s(idx)); |
|
80 endif |
3803
|
81 |
3904
|
82 ## red hue |
|
83 idx = (v == rgb(:,1) & notgray); |
|
84 if (any (idx)) |
|
85 h(idx) = 1/6 * (rgb(idx,2) - rgb(idx,3)) ./ (v(idx) - s(idx)); |
|
86 endif |
3803
|
87 |
3904
|
88 ## correct for negative red |
|
89 idx = (h < 0); |
|
90 h(idx) = 1+h(idx); |
3803
|
91 |
3904
|
92 ## set the saturation |
|
93 s(! notgray) = 0; |
|
94 s(notgray) = 1 - s(notgray) ./ v(notgray); |
3803
|
95 |
3904
|
96 hsval = [h, s, v]; |
7375
|
97 |
|
98 ## If input was an image, convert it back into one. |
|
99 if (is_image) |
|
100 hsval = reshape (hsval, Sz); |
|
101 endif |
3803
|
102 |
|
103 endfunction |