Mercurial > octave-nkf
annotate scripts/image/hsv2rgb.m @ 14138:72c96de7a403 stable
maint: update copyright notices for 2012
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 02 Jan 2012 14:25:41 -0500 |
parents | c792872f8942 |
children | 1f911333ed3d |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
1 ## Copyright (C) 1999-2012 Kai Habel |
3803 | 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
3803 | 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 | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
3803 | 18 |
19 ## -*- texinfo -*- | |
6547 | 20 ## @deftypefn {Function File} {@var{rgb_map} =} hsv2rgb (@var{hsv_map}) |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
21 ## Transform a colormap or image from the HSV space to the RGB space. |
5642 | 22 ## @seealso{rgb2hsv} |
3803 | 23 ## @end deftypefn |
24 | |
25 ## Author: Kai Habel <kai.habel@gmx.de> | |
26 ## Adapted-by: jwe | |
27 | |
28 function rgb_map = hsv2rgb (hsv_map) | |
29 | |
8507 | 30 ## Each color value x = (r,g,b) is calculated with |
3803 | 31 ## x = (1-sat)*val+sat*val*f_x(hue) |
32 ## where f_x(hue) is a piecewise defined function for | |
8507 | 33 ## each color with f_r(hue-2/3) = f_g(hue) = f_b(hue-1/3). |
3803 | 34 |
35 if (nargin != 1) | |
6046 | 36 print_usage (); |
3803 | 37 endif |
38 | |
7375 | 39 ## If we have an image convert it into a color map. |
40 if (ismatrix (hsv_map) && ndims (hsv_map) == 3) | |
41 is_image = true; | |
42 Sz = size (hsv_map); | |
43 hsv_map = [hsv_map(:,:,1)(:), hsv_map(:,:,2)(:), hsv_map(:,:,3)(:)]; | |
44 ## Convert to a double image. | |
45 if (isinteger (hsv_map)) | |
46 C = class (hsv_map); | |
47 low = double (intmin (C)); | |
48 high = double (intmax (C)); | |
49 hsv_map = (double (hsv_map) - low) / (high - low); | |
50 endif | |
51 else | |
52 is_image = false; | |
53 endif | |
54 | |
4030 | 55 if (! ismatrix (hsv_map) || columns (hsv_map) != 3) |
3803 | 56 error ("hsv2rgb: argument must be a matrix of size nx3"); |
57 endif | |
58 | |
59 ## set values <0 to 0 and >1 to 1 | |
60 hsv_map = (hsv_map >= 0 & hsv_map <= 1) .* hsv_map \ | |
61 + (hsv_map < 0) .* 0 + (hsv_map > 1); | |
62 | |
63 ## fill rgb map with v*(1-s) | |
64 rgb_map = kron ([1, 1, 1], hsv_map(:,3) .* (1 - hsv_map(:,2))); | |
65 | |
66 ## red(hue-2/3)=green(hue)=blue(hue-1/3) | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
67 ## apply modulo 1 for red and blue |
3803 | 68 t = hsv_map(:,1); |
69 tp = t'; | |
70 hue = [(tp - 2/3 - floor (t - 2/3)'); | |
71 tp; | |
72 (tp - 1/3 - floor (t - 1/3)')]'; | |
73 | |
74 ## factor s*v -> f | |
75 f = kron ([1, 1, 1], hsv_map(:,2)) .* kron ([1, 1, 1], hsv_map(:,3)); | |
76 | |
77 ## add s*v* hue-function to rgb map | |
78 rgb_map = rgb_map + f .* (6 * (hue < 1/6) .* hue | |
10433
2c01d24459fb
Detabify scripts in 'scripts/image/'
Soren Hauberg <hauberg@gmail.com>
parents:
8920
diff
changeset
|
79 + (hue >= 1/6 & hue < 1/2) |
2c01d24459fb
Detabify scripts in 'scripts/image/'
Soren Hauberg <hauberg@gmail.com>
parents:
8920
diff
changeset
|
80 + (hue >= 1/2 & hue < 2/3) .* (4 - 6 * hue)); |
3803 | 81 |
7375 | 82 ## If input was an image, convert it back into one. |
83 if (is_image) | |
84 rgb_map = reshape (rgb_map, Sz); | |
85 endif | |
86 | |
3803 | 87 endfunction |