1## Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2008
4## This file is part of Octave.
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
8## the Free Software Foundation; either version 3 of the License, or (at
9## your option) any later version.
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.
16## You should have received a copy of the GNU General Public License
17## along with Octave; see the file COPYING. If not, see
18## <http://www.gnu.org/licenses/>.
21## @deftypefn {Function File} {@var{hsv_map} =} rgb2hsv (@var{rgb_map})
22## Transform a colormap or image from the rgb space to the hsv space.
24## A color n the RGB space consists of the red, green and blue intensities.
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.
28## Hue describes the dominant wavelength.
29## Saturation is the amount of Hue mixed into the color.
33## Author: Kai Habel <kai.habel@gmx.de>
36function hsval = rgb2hsv (rgb)
42 ## If we have an image convert it into a color map.
43 if (ismatrix (rgb) && ndims (rgb) == 3)
46 rgb = [rgb(:,:,1)(:), rgb(:,:,2)(:), rgb(:,:,3)(:)];
47 ## Convert to a double image.
50 low = double (intmin (C));
51 high = double (intmax (C));
52 rgb = (double (rgb) - low) / (high - low);
58 if (! ismatrix (rgb) || columns (rgb) != 3)
59 error ("rgb2hsv: argument must be a matrix of size n x 3");
62 ## get the max and min
66 ## set hue to zero for undefined values (gray has no hue)
71 idx = (v == rgb(:,3) & notgray);
73 h(idx) = 2/3 + 1/6 * (rgb(idx,1) - rgb(idx,2)) ./ (v(idx) - s(idx));
77 idx = (v == rgb(:,2) & notgray);
79 h(idx) = 1/3 + 1/6 * (rgb(idx,3) - rgb(idx,1)) ./ (v(idx) - s(idx));
83 idx = (v == rgb(:,1) & notgray);
85 h(idx) = 1/6 * (rgb(idx,2) - rgb(idx,3)) ./ (v(idx) - s(idx));
88 ## correct for negative red
94 s(notgray) = 1 - s(notgray) ./ v(notgray);
98 ## If input was an image, convert it back into one.
100 hsval = reshape (hsval, Sz);