Mercurial > hg > octave-nkf
comparison scripts/image/rgb2hsv.m @ 3803:63c75bc3db82
[project @ 2001-02-28 08:24:40 by jwe]
author | jwe |
---|---|
date | Wed, 28 Feb 2001 08:24:43 +0000 |
parents | |
children | 6b00ac653c0f |
comparison
equal
deleted
inserted
replaced
3802:4f1a26a730fd | 3803:63c75bc3db82 |
---|---|
1 ## Copyright (C) 1999, 2000 Kai Habel | |
2 ## | |
3 ## This file is part of Octave. | |
4 ## | |
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 2, or (at your option) | |
8 ## any later version. | |
9 ## | |
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. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
16 ## along with Octave; see the file COPYING. If not, write to the Free | |
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA | |
18 ## 02111-1307, USA. | |
19 | |
20 ## -*- texinfo -*- | |
21 ## @deftypefn {Function File} {} @var{hsv_map} = rgb2hsv (@var{rgb_map}) | |
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. | |
28 ## Hue describes the dominant wavelegth. | |
29 ## Saturation is the amount of Hue mixed into the color. | |
30 ## @end deftypefn | |
31 ## @seealso{hsv2rgb} | |
32 | |
33 ## Author: Kai Habel <kai.habel@gmx.de> | |
34 ## Adapted-by: jwe | |
35 | |
36 function hsval = rgb2hsv (rgb) | |
37 | |
38 if (nargin != 1) | |
39 usage ("hsv_map = rgb2hsv (rgb_map)"); | |
40 endif | |
41 | |
42 if (! is_matrix (rgb) || columns (rgb) != 3) | |
43 error ("rgb2hsv: argument must be a matrix of size n x 3"); | |
44 endif | |
45 | |
46 # get saturation and value | |
47 v = max (rgb'); | |
48 s = (v' > 0) .* (1 .- min (rgb') ./ v)'; | |
49 | |
50 # if v==0 set s to 0 too | |
51 s(isnan (s)) = 0; | |
52 | |
53 # subtract minimum and divide trough maximum | |
54 # to get the bright and saturated colors | |
55 sc = (rgb - kron ([1, 1, 1], min (rgb')')); | |
56 sv = sc ./ kron([1, 1, 1], max (sc')'); | |
57 | |
58 # if r=g=b (gray value) set hue to 0 | |
59 sv(isnan (sv)) = 0; | |
60 | |
61 # hue=f(color) must be splitted into 6 parts | |
62 # 2 for each color | |
63 | |
64 # h1(green) | |
65 tmp = (sv(:, 1) == 1 & sv(:,3) == 0) .* (1/6 * sv(:,2) + eps); | |
66 # avoid problems with h2(red) since hue(0)==hue(1) | |
67 h = (tmp < 1/6) .* tmp; | |
68 # h2(green) | |
69 h = h + ((h == 0) & sv(:,1) == 0 & sv(:,3) == 1) \ | |
70 .* (-1/6 * sv(:,2) + 2/3 + eps); | |
71 | |
72 # h1(red) | |
73 h = h + ((h == 0) & sv(:,2) == 1 & sv(:,3) == 0) \ | |
74 .* (-1/6 * sv(:,1) + 1/3 + eps); | |
75 | |
76 # h2(red) | |
77 h = h + ((h == 0) & sv(:,2) == 0 & sv(:,3) == 1) \ | |
78 .* (1/6 * sv(:,1) + 2/3 + eps); | |
79 | |
80 # h1(blue) | |
81 h = h + ((h == 0) & sv(:,1) == 1 & sv(:,2) == 0) \ | |
82 .* (-1/6 * sv(:,3) + 1 + eps); | |
83 | |
84 # h2(blue) | |
85 h = h + ((h == 0) & sv(:,1) == 0 & sv(:,2) == 1) \ | |
86 .* (1/6 * sv(:,3) + 1/3); | |
87 | |
88 hsval = [h, s, v']; | |
89 | |
90 endfunction |