1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2002-2012, Industrial Light & Magic, a division of Lucas
4 // Digital Ltd. LLC
5 //
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are
10 // met:
11 // *       Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // *       Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 // *       Neither the name of Industrial Light & Magic nor the names of
18 // its contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 ///////////////////////////////////////////////////////////////////////////
34 
35 
36 //----------------------------------------------------------------------------
37 //
38 //	Implementation of non-template items declared in ImathColorAlgo.h
39 //
40 //----------------------------------------------------------------------------
41 
42 #include "ImathColorAlgo.h"
43 
44 IMATH_INTERNAL_NAMESPACE_SOURCE_ENTER
45 
46 
47 Vec3<double>
hsv2rgb_d(const Vec3<double> & hsv)48 hsv2rgb_d(const Vec3<double> &hsv)
49 {
50     double hue = hsv.x;
51     double sat = hsv.y;
52     double val = hsv.z;
53 
54     double x = 0.0, y = 0.0, z = 0.0;
55 
56     if (hue == 1) hue = 0;
57     else hue *= 6;
58 
59     int i = int(Math<double>::floor(hue));
60     double f = hue-i;
61     double p = val*(1-sat);
62     double q = val*(1-(sat*f));
63     double t = val*(1-(sat*(1-f)));
64 
65     switch (i)
66     {
67       case 0: x = val; y = t; z = p; break;
68       case 1: x = q; y = val; z = p; break;
69       case 2: x = p; y = val; z = t; break;
70       case 3: x = p; y = q; z = val; break;
71       case 4: x = t; y = p; z = val; break;
72       case 5: x = val; y = p; z = q; break;
73     }
74 
75     return Vec3<double>(x,y,z);
76 }
77 
78 
79 Color4<double>
hsv2rgb_d(const Color4<double> & hsv)80 hsv2rgb_d(const Color4<double> &hsv)
81 {
82     double hue = hsv.r;
83     double sat = hsv.g;
84     double val = hsv.b;
85 
86     double   r = 0.0, g = 0.0, b = 0.0;
87 
88     if (hue == 1) hue = 0;
89     else hue *= 6;
90 
91     int i = int(Math<double>::floor(hue));
92     double f = hue-i;
93     double p = val*(1-sat);
94     double q = val*(1-(sat*f));
95     double t = val*(1-(sat*(1-f)));
96 
97     switch (i)
98     {
99       case 0: r = val; g = t; b = p; break;
100       case 1: r = q; g = val; b = p; break;
101       case 2: r = p; g = val; b = t; break;
102       case 3: r = p; g = q; b = val; break;
103       case 4: r = t; g = p; b = val; break;
104       case 5: r = val; g = p; b = q; break;
105     }
106 
107     return Color4<double>(r,g,b,hsv.a);
108 }
109 
110 
111 
112 Vec3<double>
rgb2hsv_d(const Vec3<double> & c)113 rgb2hsv_d(const Vec3<double> &c)
114 {
115     const double &x = c.x;
116     const double &y = c.y;
117     const double &z = c.z;
118 
119     double max	 = (x > y) ? ((x > z) ? x : z) : ((y > z) ? y : z);
120     double min	 = (x < y) ? ((x < z) ? x : z) : ((y < z) ? y : z);
121     double range = max - min;
122     double val	 = max;
123     double sat   = 0;
124     double hue   = 0;
125 
126     if (max != 0)   sat = range/max;
127 
128     if (sat != 0)
129     {
130 	double h;
131 
132 	if      (x == max)	h =     (y - z) / range;
133 	else if (y == max)	h = 2 + (z - x) / range;
134 	else		h = 4 + (x - y) / range;
135 
136 	hue = h/6.;
137 
138 	if (hue < 0.)
139 	    hue += 1.0;
140     }
141     return Vec3<double>(hue,sat,val);
142 }
143 
144 
145 Color4<double>
rgb2hsv_d(const Color4<double> & c)146 rgb2hsv_d(const Color4<double> &c)
147 {
148     const double &r = c.r;
149     const double &g = c.g;
150     const double &b = c.b;
151 
152     double max	 = (r > g) ? ((r > b) ? r : b) : ((g > b) ? g : b);
153     double min	 = (r < g) ? ((r < b) ? r : b) : ((g < b) ? g : b);
154     double range = max - min;
155     double val	 = max;
156     double sat   = 0;
157     double hue   = 0;
158 
159     if (max != 0)   sat = range/max;
160 
161     if (sat != 0)
162     {
163 	double h;
164 
165 	if      (r == max)	h =     (g - b) / range;
166 	else if (g == max)	h = 2 + (b - r) / range;
167 	else		h = 4 + (r - g) / range;
168 
169 	hue = h/6.;
170 
171 	if (hue < 0.)
172 	    hue += 1.0;
173     }
174     return Color4<double>(hue,sat,val,c.a);
175 }
176 
177 
178 IMATH_INTERNAL_NAMESPACE_SOURCE_EXIT
179