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}) |
3803
|
22 ## Transform a colormap from the rgb space to the hsv space. |
|
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 |
4030
|
42 if (! ismatrix (rgb) || columns (rgb) != 3) |
3803
|
43 error ("rgb2hsv: argument must be a matrix of size n x 3"); |
|
44 endif |
|
45 |
3904
|
46 ## get the max and min |
|
47 s = min (rgb')'; |
|
48 v = max (rgb')'; |
3803
|
49 |
3904
|
50 ## set hue to zero for undefined values (gray has no hue) |
|
51 h = zeros (size (v)); |
|
52 notgray = (s != v); |
|
53 |
|
54 ## blue hue |
|
55 idx = (v == rgb(:,3) & notgray); |
|
56 if (any (idx)) |
|
57 h(idx) = 2/3 + 1/6 * (rgb(idx,1) - rgb(idx,2)) ./ (v(idx) - s(idx)); |
|
58 endif |
3803
|
59 |
3904
|
60 ## green hue |
|
61 idx = (v == rgb(:,2) & notgray); |
|
62 if (any (idx)) |
|
63 h(idx) = 1/3 + 1/6 * (rgb(idx,3) - rgb(idx,1)) ./ (v(idx) - s(idx)); |
|
64 endif |
3803
|
65 |
3904
|
66 ## red hue |
|
67 idx = (v == rgb(:,1) & notgray); |
|
68 if (any (idx)) |
|
69 h(idx) = 1/6 * (rgb(idx,2) - rgb(idx,3)) ./ (v(idx) - s(idx)); |
|
70 endif |
3803
|
71 |
3904
|
72 ## correct for negative red |
|
73 idx = (h < 0); |
|
74 h(idx) = 1+h(idx); |
3803
|
75 |
3904
|
76 ## set the saturation |
|
77 s(! notgray) = 0; |
|
78 s(notgray) = 1 - s(notgray) ./ v(notgray); |
3803
|
79 |
3904
|
80 hsval = [h, s, v]; |
3803
|
81 |
|
82 endfunction |