3803
|
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 |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
3803
|
19 |
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} @var{rgb_map} = hsv2rgb (@var{hsv_map}) |
|
22 ## Transform a colormap from the hsv space to the rgb space. |
5642
|
23 ## @seealso{rgb2hsv} |
3803
|
24 ## @end deftypefn |
|
25 |
|
26 ## Author: Kai Habel <kai.habel@gmx.de> |
|
27 ## Adapted-by: jwe |
|
28 |
|
29 function rgb_map = hsv2rgb (hsv_map) |
|
30 |
|
31 ## each color value x=(r,g,b) is calculated with |
|
32 ## x = (1-sat)*val+sat*val*f_x(hue) |
|
33 ## where f_x(hue) is a piecewise defined function for |
|
34 ## each color with f_r(hue-2/3) = f_g(hue) = f_b(hue-1/3) |
|
35 |
|
36 if (nargin != 1) |
|
37 usage ("rgb_map = hsv2rgb (hsv_map)"); |
|
38 endif |
|
39 |
4030
|
40 if (! ismatrix (hsv_map) || columns (hsv_map) != 3) |
3803
|
41 error ("hsv2rgb: argument must be a matrix of size nx3"); |
|
42 endif |
|
43 |
|
44 ## set values <0 to 0 and >1 to 1 |
|
45 hsv_map = (hsv_map >= 0 & hsv_map <= 1) .* hsv_map \ |
|
46 + (hsv_map < 0) .* 0 + (hsv_map > 1); |
|
47 |
|
48 ## fill rgb map with v*(1-s) |
|
49 rgb_map = kron ([1, 1, 1], hsv_map(:,3) .* (1 - hsv_map(:,2))); |
|
50 |
|
51 ## red(hue-2/3)=green(hue)=blue(hue-1/3) |
|
52 ## apply modulo 1 for red and blue |
|
53 t = hsv_map(:,1); |
|
54 tp = t'; |
|
55 hue = [(tp - 2/3 - floor (t - 2/3)'); |
|
56 tp; |
|
57 (tp - 1/3 - floor (t - 1/3)')]'; |
|
58 |
|
59 ## factor s*v -> f |
|
60 f = kron ([1, 1, 1], hsv_map(:,2)) .* kron ([1, 1, 1], hsv_map(:,3)); |
|
61 |
|
62 ## add s*v* hue-function to rgb map |
|
63 rgb_map = rgb_map + f .* (6 * (hue < 1/6) .* hue |
|
64 + (hue >= 1/6 & hue < 1/2) |
|
65 + (hue >= 1/2 & hue < 2/3) .* (4 - 6 * hue)); |
|
66 |
|
67 endfunction |