1 ## Copyright (C) 1999-2012 Kai Habel
3 ## This file is part of Octave.
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {@var{hsv_map} =} rgb2hsv (@var{rgb})
21 ## Transform a colormap or image from the RGB space to the HSV space.
23 ## A color in the RGB space consists of the red, green and blue intensities.
25 ## In the HSV space each color is represented by their hue, saturation
26 ## and value (brightness). Value gives the amount of light in the color.
27 ## Hue describes the dominant wavelength.
28 ## Saturation is the amount of hue mixed into the color.
32 ## Author: Kai Habel <kai.habel@gmx.de>
35 function hsv_map = rgb2hsv (rgb)
41 ## If we have an image convert it into a color map.
42 if (ismatrix (rgb) && ndims (rgb) == 3)
45 rgb = [rgb(:,:,1)(:), rgb(:,:,2)(:), rgb(:,:,3)(:)];
46 ## Convert to a double image.
49 low = double (intmin (C));
50 high = double (intmax (C));
51 rgb = (double (rgb) - low) / (high - low);
57 if (! ismatrix (rgb) || columns (rgb) != 3)
58 error ("rgb2hsv: RGB_MAP must be a matrix of size n x 3");
61 ## get the max and min
65 ## set hue to zero for undefined values (gray has no hue)
70 idx = (v == rgb(:,3) & notgray);
72 h(idx) = 2/3 + 1/6 * (rgb(idx,1) - rgb(idx,2)) ./ (v(idx) - s(idx));
76 idx = (v == rgb(:,2) & notgray);
78 h(idx) = 1/3 + 1/6 * (rgb(idx,3) - rgb(idx,1)) ./ (v(idx) - s(idx));
82 idx = (v == rgb(:,1) & notgray);
84 h(idx) = 1/6 * (rgb(idx,2) - rgb(idx,3)) ./ (v(idx) - s(idx));
87 ## correct for negative red
93 s(notgray) = 1 - s(notgray) ./ v(notgray);
97 ## If input was an image, convert it back into one.
99 hsv_map = reshape (hsv_map, Sz);