Jamoma API  0.6.0.a19
ColorDataspace.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup foundationDataspaceLib
4  *
5  * @brief The #ColorDataspace converts between different measurement units describing colors.
6  *
7  * @details Possible units are CMY, HSL, HSV, RGB and RGB8. The neutral unit of the color dataspace is RGB where each of the color values is normalised to the [0, 1] range.@n
8  * @n
9  * @n
10  * Code for RGB <-> HSV convertion is in part based on source code provided by Marcelo Gattass:@n
11  * http://www.tecgraf.puc-rio.br/~mgattass/color/ColorIndex.html @n
12  * Last retrieved 2009-07-30 @n
13  * @n
14  * License: @n
15  * @n
16  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: @n
17  * @n
18  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. @n
19  * @n
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * @authors Tim Place, Trond Lossius, Nils Peters, ...
23  *
24  * @copyright Copyright © 2007 by Tim Place @n
25  * This code is licensed under the terms of the "New BSD License" @n
26  * http://creativecommons.org/licenses/BSD/
27  */
28 
29 
30 #include "ColorDataspace.h"
31 
32 // Utility Functions
33 
34 double hls_value(double n1, double n2, double hue)
35 {
36  if(hue > 360.0)
37  hue -= 360.;
38  if(hue < 0.0)
39  hue += 360.;
40  if(hue < 60.)
41  return (n1+(n2-n1)*hue/60.);
42  if(hue < 180.)
43  return n2;
44  if(hue < 240.)
45  return (n1+(n2-n1)*(240-hue)/60.);
46  return n1;
47 }
48 
49 
50 
51 // Actual Colorspace Units
52 
53 #define thisTTClass CMYUnit
54 #define thisTTClassName "unit.cmy"
55 #define thisTTClassTags "dataspace.unit, color"
56 
58 TTDataspaceUnit(arguments)
59 {;}
60 
61 CMYUnit::~CMYUnit(){;}
62 
63 void CMYUnit::convertToNeutral(const TTValue& input, TTValue& output)
64 {
65  output.resize(3);
66  output[0] = (255 - (TTFloat64)input[0]) * kTTInv255;
67  output[1] = (255 - (TTFloat64)input[1]) * kTTInv255;
68  output[2] = (255 - (TTFloat64)input[2]) * kTTInv255;
69 }
70 
71 
72 void CMYUnit::convertFromNeutral(const TTValue& input, TTValue& output)
73 {
74  output.resize(3);
75  output[0] = 255 * (1 - (TTFloat64)input[0]);
76  output[1] = 255 * (1 - (TTFloat64)input[1]);
77  output[2] = 255 * (1 - (TTFloat64)input[2]);
78 }
79 
80 
81 #undef thisTTClass
82 #undef thisTTClassName
83 #undef thisTTClassTags
84 
85 /***********************************************************************************************/
86 
87 #define thisTTClass HSLUnit
88 #define thisTTClassName "unit.hsl"
89 #define thisTTClassTags "dataspace.unit, color"
90 
92 TTDataspaceUnit(arguments)
93 {;}
94 
95 HSLUnit::~HSLUnit(){;}
96 
97 void HSLUnit::convertToNeutral(const TTValue& input, TTValue& output)
98 {
99  double h = (TTFloat64)input[0];// (TTFloat64)input[0];
100  double s = (TTFloat64)input[1];// (TTFloat64)input[1];
101  double l = (TTFloat64)input[2];// (TTFloat64)input[2];
102  double red, green, blue;
103  double m1, m2, hue, lightness, saturation;
104 
105  // scale to floating point... number range should be 360, 1, 1
106  hue = h;
107  saturation = s/100.0;
108  lightness = l/100.0;
109 
110  if(lightness <= 0.5)
111  m2 = lightness*(1.0+saturation);
112  else
113  m2 = lightness+saturation-lightness*saturation;
114 
115  m1 = 2.0 * lightness-m2;
116  if(saturation == 0.0){
117  red = lightness;
118  green = lightness;
119  blue = lightness;
120  }
121  else{
122  red = hls_value(m1, m2, hue+120.0);
123  green = hls_value(m1, m2, hue);
124  blue = hls_value(m1, m2, hue-120.0);
125  }
126 
127  output.resize(3);
128  output[0] = red;
129  output[1] = green;
130  output[2] = blue;
131 }
132 
133 
134 void HSLUnit::convertFromNeutral(const TTValue& input, TTValue& output)
135 {
136  double r = ((TTFloat64)input[0]);
137  double g = ((TTFloat64)input[1]);
138  double b = ((TTFloat64)input[2]);
139  double hue, lightness, saturation;
140  double max,min,delta;
141  double H,L,S;
142 
143  max = r;
144  if(max<g)
145  max=g;
146  if(max<b)
147  max=b;
148 
149  min = r;
150  if(min>g)
151  min=g;
152  if(min>b)
153  min=b;
154 
155  L = (max+min)/2.0;
156  if(max == min){
157  S = 0;
158  H = 0;
159  }
160  else{
161  delta = max-min;
162  if(L < 0.5)
163  S = delta/(max+min);
164  else
165  S = delta/(2.0-max-min);
166  if(r == max)
167  H = (g-b)/delta;
168  else if(g == max)
169  H = 2.0+(b-r)/delta;
170  else if(b == max)
171  H = 4.0+(r-g)/delta;
172  }
173 
174  H *= 60.0;
175  if(H < 0)
176  H += 360.0;
177 
178  hue = H;
179  saturation = S * 100.0;
180  lightness = L * 100.0;
181 
182  output.resize(3);
183  output[0] = hue;
184  output[1] = saturation;
185  output[2] = lightness;
186 }
187 
188 
189 #undef thisTTClass
190 #undef thisTTClassName
191 #undef thisTTClassTags
192 
193 /***********************************************************************************************
194 
195 Code for RGB <-> HSV convertion is in part based on source code provided by Marcelo Gattass:
196 http://www.tecgraf.puc-rio.br/~mgattass/color/ColorIndex.html
197 Last retrieved 2009-07-30
198 
199 License:
200 
201 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
202 
203 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
204 
205 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
206 
207 */
208 
209 #define thisTTClass HSVUnit
210 #define thisTTClassName "unit.hsv"
211 #define thisTTClassTags "dataspace.unit, color"
212 
214 TTDataspaceUnit(arguments)
215 {;}
216 
217 HSVUnit::~HSVUnit(){;}
218 
219 void HSVUnit::convertToNeutral(const TTValue& input, TTValue& output)
220 {
221  double h = (TTFloat64)input[0]/360.;
222  double s = (TTFloat64)input[1]/100.;
223  double v = (TTFloat64)input[2]/100.;
224  double r, g, b;
225  //double h1, a[7], q, f;
226 
227  if ( s == 0 )
228  {
229  r = v;
230  g = v;
231  b = v;
232  }
233  else
234  {
235  double var_h = h * 6;
236  double var_i = floor( var_h );
237  double var_1 = v * ( 1 - s );
238  double var_2 = v * ( 1 - s * ( var_h - var_i ) );
239  double var_3 = v * ( 1 - s * ( 1 - ( var_h - var_i ) ) );
240 
241  if ( var_i == 0 ) { r = v ; g = var_3 ; b = var_1; }
242  else if ( var_i == 1 ) { r = var_2 ; g = v ; b = var_1; }
243  else if ( var_i == 2 ) { r = var_1 ; g = v ; b = var_3; }
244  else if ( var_i == 3 ) { r = var_1 ; g = var_2 ; b = v; }
245  else if ( var_i == 4 ) { r = var_3 ; g = var_1 ; b = v; }
246  else { r = v ; g = var_1 ; b = var_2; }
247  }
248 
249  output.resize(3);
250  output[0] = r;
251  output[1] = g;
252  output[2] = b;
253 }
254 
255 
256 void HSVUnit::convertFromNeutral(const TTValue& input, TTValue& output)
257 {
258  double r = (TTFloat64)input[0];
259  double g = (TTFloat64)input[1];
260  double b = (TTFloat64)input[2];
261 
262  double h,s,v;
263 
264  double min;
265  double max;
266  double delta;
267 
268  //Min. value of RGB
269  min = r;
270  if (g<min) min = g;
271  if (b<min) min = b;
272 
273  //Max. value of RGB
274  max = r;
275  if (g>max) max = g;
276  if (b>max) max = b;
277 
278  //Delta RGB value
279  delta = max - min;
280 
281  v = max;
282 
283  if ( max == 0 )
284  {
285  h = 0;
286  s = 0;
287  }
288  else
289  {
290  s = delta / max;
291 
292  if(r == max)
293  h = (g-b)/delta;
294  else if(g == max)
295  h = 2.0+(b-r)/delta;
296  else if(b == max)
297  h = 4.0+(r-g)/delta;
298 
299  h *= 60.0;
300  if(h < 0)
301  h += 360.0;
302  }
303 
304  output.resize(3);
305  output[0] = h;
306  output[1] = s*100;
307  output[2] = v*100;
308 }
309 
310 
311 #undef thisTTClass
312 #undef thisTTClassName
313 #undef thisTTClassTags
314 
315 /***********************************************************************************************/
316 
317 #define thisTTClass RGBUnit
318 #define thisTTClassName "unit.rgb"
319 #define thisTTClassTags "dataspace.unit, color"
320 
322 TTDataspaceUnit(arguments)
323 {;}
324 
325 RGBUnit::~RGBUnit(){;}
326 
327 void RGBUnit::convertToNeutral(const TTValue& input, TTValue& output)
328 {
329  output = input;
330 }
331 
332 
333 void RGBUnit::convertFromNeutral(const TTValue& input, TTValue& output)
334 {
335  output = input;
336 }
337 
338 
339 #undef thisTTClass
340 #undef thisTTClassName
341 #undef thisTTClassTags
342 
343 /***********************************************************************************************/
344 
345 #define thisTTClass RGB8Unit
346 #define thisTTClassName "unit.rgb8"
347 #define thisTTClassTags "dataspace.unit, color"
348 
350 TTDataspaceUnit(arguments)
351 {;}
352 
353 RGB8Unit::~RGB8Unit(){;}
354 
355 void RGB8Unit::convertToNeutral(const TTValue& input, TTValue& output)
356 {
357  output.resize(3);
358  output[0] = (TTFloat64)input[0]*kTTInv255;
359  output[1] = (TTFloat64)input[1]*kTTInv255;
360  output[2] = (TTFloat64)input[2]*kTTInv255;
361 }
362 
363 
364 void RGB8Unit::convertFromNeutral(const TTValue& input, TTValue& output)
365 {
366  output.resize(3);
367  output[0] = (TTFloat64)input[0]*255;
368  output[1] = (TTFloat64)input[1]*255;
369  output[2] = (TTFloat64)input[2]*255;
370 }
371 
372 
373 #undef thisTTClass
374 #undef thisTTClassName
375 #undef thisTTClassTags
376 
377 /***********************************************************************************************/
378 
379 #define thisTTClass ColorDataspace
380 #define thisTTClassName "dataspace.color"
381 #define thisTTClassTags "foundationDataspaceLib, dataspace, color"
382 
384 {
385  // Create one of each kind of unit, and cache them in a hash
386  registerUnit(TT("unit.cmy"), TT("cmy"));
387  registerUnit(TT("unit.hsl"), TT("hsl"));
388  registerUnit(TT("unit.hsv"), TT("hsv"));
389  registerUnit(TT("unit.rgb"), TT("rgb"));
390  registerUnit(TT("unit.rgb8"), TT("rgb8"));
391 
392  // Set our neutral unit (the unit through which all conversions are made)
393  neutralUnit = TT("rgb");
394 
395  // Now that the cache is created, we can create a set of default units
396  setInputUnit(neutralUnit);
397  setOutputUnit(neutralUnit);
398 }
399 
400 
401 ColorDataspace::~ColorDataspace()
402 {
403  ;
404 }
405 
406 #undef thisTTClass
407 #undef thisTTClassName
408 #undef thisTTClassTags
void convertToNeutral(const TTValue &input, TTValue &output)
Convert from HSV to neutral unit.
void convertFromNeutral(const TTValue &input, TTValue &output)
Convert from neutral unit to HSL.
void convertToNeutral(const TTValue &input, TTValue &output)
Convert from radians to neutral unit.
TTFOUNDATION_EXPORT const TTFloat64 kTTInv255
Constant for color representation when converting from char8 to float representation.
Definition: TTBase.cpp:36
void convertFromNeutral(const TTValue &input, TTValue &output)
Convert from neutral unit to RGB8.
void convertToNeutral(const TTValue &input, TTValue &output)
Convert from RGB8 to neutral unit.
double TTFloat64
64 bit floating point number
Definition: TTBase.h:188
#define TT
This macro is defined as a shortcut for doing a lookup in the symbol table.
Definition: TTSymbol.h:155
void convertFromNeutral(const TTValue &input, TTValue &output)
Convert from neutral unit to HSV.
Specification for the base class of each DataspaceUnit.
void convertFromNeutral(const TTValue &input, TTValue &output)
Convert from neutral unit to CMY.
The ColorDataspace converts between different measurement units describing colors.
void convertFromNeutral(const TTValue &input, TTValue &output)
Convert from neutral unit to RGB.
TT_OBJECT_CONSTRUCTOR
Constructor macro.
void resize(size_type n)
Change the number of elements.
void convertToNeutral(const TTValue &input, TTValue &output)
Convert from HSL to neutral unit.
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34
void convertToNeutral(const TTValue &input, TTValue &output)
Convert from CMY to neutral unit.