changelog shortlog tags changeset files revisions annotate raw

scripts/image/hsv2rgb.m

changeset 10289: 4b124317dc38
parent:cadc73247d65
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (62 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1## Copyright (C) 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2008, 2009
2## Kai Habel
3##
4## This file is part of Octave.
5##
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.
10##
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.
15##
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/>.
19
20## -*- texinfo -*-
21## @deftypefn {Function File} {@var{rgb_map} =} hsv2rgb (@var{hsv_map})
22## Transform a colormap or image from the hsv space to the rgb space.
23## @seealso{rgb2hsv}
24## @end deftypefn
25
26## Author: Kai Habel <kai.habel@gmx.de>
27## Adapted-by: jwe
28
29function 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 print_usage ();
38 endif
39
40 ## If we have an image convert it into a color map.
41 if (ismatrix (hsv_map) && ndims (hsv_map) == 3)
42 is_image = true;
43 Sz = size (hsv_map);
44 hsv_map = [hsv_map(:,:,1)(:), hsv_map(:,:,2)(:), hsv_map(:,:,3)(:)];
45 ## Convert to a double image.
46 if (isinteger (hsv_map))
47 C = class (hsv_map);
48 low = double (intmin (C));
49 high = double (intmax (C));
50 hsv_map = (double (hsv_map) - low) / (high - low);
51 endif
52 else
53 is_image = false;
54 endif
55
56 if (! ismatrix (hsv_map) || columns (hsv_map) != 3)
57 error ("hsv2rgb: argument must be a matrix of size nx3");
58 endif
59
60 ## set values <0 to 0 and >1 to 1
61 hsv_map = (hsv_map >= 0 & hsv_map <= 1) .* hsv_map \
62 + (hsv_map < 0) .* 0 + (hsv_map > 1);
63
64 ## fill rgb map with v*(1-s)
65 rgb_map = kron ([1, 1, 1], hsv_map(:,3) .* (1 - hsv_map(:,2)));
66
67 ## red(hue-2/3)=green(hue)=blue(hue-1/3)
68 ## apply modulo 1 for red and blue
69 t = hsv_map(:,1);
70 tp = t';
71 hue = [(tp - 2/3 - floor (t - 2/3)');
72 tp;
73 (tp - 1/3 - floor (t - 1/3)')]';
74
75 ## factor s*v -> f
76 f = kron ([1, 1, 1], hsv_map(:,2)) .* kron ([1, 1, 1], hsv_map(:,3));
77
78 ## add s*v* hue-function to rgb map
79 rgb_map = rgb_map + f .* (6 * (hue < 1/6) .* hue
80 + (hue >= 1/6 & hue < 1/2)
81 + (hue >= 1/2 & hue < 2/3) .* (4 - 6 * hue));
82
83 ## If input was an image, convert it back into one.
84 if (is_image)
85 rgb_map = reshape (rgb_map, Sz);
86 endif
87
88endfunction