File: Color.h

package info (click to toggle)
gravitation 3%2Bdfsg1-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,180 kB
  • sloc: cpp: 13,160; objc: 245; sh: 134; makefile: 96; perl: 67
file content (459 lines) | stat: -rw-r--r-- 11,181 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
// Jason Rohrer
// Color.h

/**
*
*	Color object for RadiosGL
*
*	Float components in RGB (0..1)
*	Color of emitted energy can be greater than 1
*
*
*	Created 9-5-99
*	Mods:
*		Jason Rohrer	11-6-99			Added 32-bit composite member
*		Jason Rohrer	11-13-99		Added printing functionality
*		Jason Rohrer	11-15-99		Added rebuildComposite function
*											Added weightColor function
*											Added getMax function
*		Jason Rohrer	2004-June-12	Fixed a bug in copy function.
*		Jason Rohrer	2004-June-12	Added a linear sum function.
*
*       Jason Rohrer    2004-August-12  Optimized Color constructor.
*       Jason Rohrer    2004-August-27  Added an equality test.
*       Jason Rohrer    2005-February-4 Added weighting and setValue functions.
*       Jason Rohrer    2005-February-10  Added invert and saturate functions.
*       Jason Rohrer    2005-February-11  Added HSV conversion function.
*       Jason Rohrer    2005-April-5  Disabled default building of composite.
*       Jason Rohrer    2006-August-29  Changed alpha default to 1.
*       Jason Rohrer    2006-October-3  Disabled building composite in
*                                       default Color() constuctor.
*/

#ifndef COLOR_INCLUDED
#define COLOR_INCLUDED

#include <stdio.h>
#include <float.h>

class Color {
	public :
        /**
         * Constructs a color.
         *
         * @param red, green, blue the components of the color, each in [0,1].
         * @param alpha the alpha value in [0,1].  Defaults to 1.
         * @param inBuildComposite set to true to build the composite
         *   upon construction, or false to skip building composite (faster).
         *   Defaults to false.
         */
		Color(float red, float green, float blue, float alpha=1,
              char inBuildComposite=false );


        
        /**
         * Constructs a all-zero color with no composite built.
         */
        Color();

        

        /**
         * Constructs an rgb color from HSV components.
         *
         * @param inHue, inSaturation, inValue the HSV components of the
         *   color, each in [0,1].
         * @param alpha the alpha value in [0,1].  Defaults to 1.
         * @param inBuildComposite set to true to build the composite
         *   upon construction, or false to skip building composite (faster).
         *   Defaults to true.
         *
         * @return an RGBA color equivalent to the HSV color.
         *   Must be destroyed by caller.
         */
        static Color *makeColorFromHSV(
            float inHue, float inSaturation, float inValue,
            float inAlpha=1, char inBuildComposite=false ); 


        
		float r, g, b, a;

        char mCompositeBuilt;
		unsigned long composite;		// 32-bit composite color

        
		Color *copy();	// make a copy of this color


        
        /**
         * Sets the RGBA values of this color.
         *
         * @param red, green, blue, alpha the values to set.
         *   Alpha defaults to 0.
         */
        void setValues( float red, float green, float blue, float alpha=1 );

        
        
        /**
         * Sets the RGBA values of this color using the values from
         * another color.
         *
         * @param inOtherColor the color to copy values from.
         *   Must be destroyed by caller.
         */
        void setValues( Color *inOtherColor ); 

        
        
        /**
         * Tests whether this color is equal to another color.
         *
         * @param inOtherColor the other color.
         *   Must be destroyed by caller.
         *
         * @return true if they are equal, or false otherwise.
         */
        char equals( Color *inOtherColor );

        
		void print();


		/**
		 * Computes the linear weighted sum of two colors.
		 *
		 * @param inFirst the first color.
		 * @param inSecond the second color.
		 * @param inFirstWeight the weight given to the first color in the
		 *   sum.  The second color is weighted (1-inFirstWeight).
		 *
		 * @return the sum color.  Must be destroyed by caller.
		 */
		static Color *linearSum( Color *inFirst, Color *inSecond,
			float inFirstWeight );

        
		// after adjusting the r, g, b, a values exterally
		// call this to remake the composite unsigned long
		unsigned long rebuildComposite();
		
		// get largest component of R,G,B
		float getMax();
		
		// alter color data by multiplying by weight
		void weightColor( float weight );

        
        
        /**
         * Alters color data by multiplying by a weight color.
         *
         * @param inWeightColor the color to multiply this color by.
         *   Must be destroyed by caller.
         */
		void weightColor( Color *inWeightColor );

        

        /**
         * Inverts this color.
         *
         * Ignores alpha channel.
         */
        void invert();

        

        /**
         * Saturates this color, ensuring that at most 2 components are
         * non-zero.
         *
         * Ignores alpha channel.
         */
        void saturate();

        
        
		// get component by component weighted 32-bit composite
		// (returns alpha unweighted)
		unsigned long getWeightedComposite( float weight );		// from this color
		unsigned long getWeightedComposite(unsigned long c1, float weight );	// from composite
		
		unsigned long sumComposite(unsigned long c1, unsigned long c2);
		
		// access this color as a three vector
		float &operator[](int rgbIndex);
        
	};
	
	
inline Color::Color() {
	r = 0;
	g = 0;
	b = 0;
	a = 0;

    mCompositeBuilt = false;
	composite = 0;
	}



inline Color::Color(float red, float green, float blue, float alpha,
                    char inBuildComposite ) {
	r = red;
	g = green;
	b = blue;
	a = alpha;

    if( inBuildComposite ) {
        rebuildComposite();
        }
    else {
        composite = 0;
        mCompositeBuilt = false;
        }
	}



inline Color *Color::makeColorFromHSV(
    float inHue, float inSaturation, float inValue,
    float inAlpha, char inBuildComposite ) {

    // based on pseudocode from http://www.easyrgb.com/math.php
    float r, g, b;
    
    if ( inSaturation == 0 ) {
        r = inValue;                      
        g = inValue;
        b = inValue;
        }
    else {
        float var_h = inHue * 6;
        float var_i = int( var_h );             // Or var_i = floor( var_h )
        float var_1 = inValue * ( 1 - inSaturation );
        float var_2 = inValue * ( 1 - inSaturation * ( var_h - var_i ) );
        float var_3 =
            inValue * ( 1 - inSaturation * ( 1 - ( var_h - var_i ) ) );
        
        if( var_i == 0 ) {
            r = inValue;
            g = var_3;
            b = var_1;
            }
        else if( var_i == 1 ) {
            r = var_2;
            g = inValue;
            b = var_1;
            }
        else if( var_i == 2 ) {
            r = var_1;
            g = inValue;
            b = var_3;
            }
        else if( var_i == 3 ) {
            r = var_1;
            g = var_2;
            b = inValue;
            }
        else if( var_i == 4 ) {
            r = var_3;
            g = var_1;
            b = inValue;
            }
        else {
            r = inValue;
            g = var_1;
            b = var_2;
            }
        }

    return new Color( r, g, b, inAlpha, inBuildComposite );
    }



inline Color *Color::copy() {
	Color *copyColor = new Color(r,g,b,a, mCompositeBuilt );
	return copyColor;
	}



inline void Color::setValues( float red, float green,
                              float blue, float alpha ) {
    r = red;
    g = green;
    b = blue;
    a = alpha;
    
    if( mCompositeBuilt ) {
        rebuildComposite();
        }
    }



inline void Color::setValues( Color *inOtherColor ) {
    setValues( inOtherColor->r,
               inOtherColor->g,
               inOtherColor->b,
               inOtherColor->a );
    }



inline char Color::equals( Color *inOtherColor ) {
    if( r == inOtherColor->r &&
        g == inOtherColor->g &&
        b == inOtherColor->b &&
        a == inOtherColor->a ) {
        return true;
        }
    else {
        return false;
        }
    }

inline void Color::print() {
	printf( "(%f, %f, %f)", r, g, b );
	}



inline Color *Color::linearSum( Color *inFirst, Color *inSecond,
	float inFirstWeight ) {
	
	float secondWeight = 1 - inFirstWeight;
	float r = inFirstWeight * inFirst->r + secondWeight * inSecond->r;
	float g = inFirstWeight * inFirst->g + secondWeight * inSecond->g;
	float b = inFirstWeight * inFirst->b + secondWeight * inSecond->b;
    float a = inFirstWeight * inFirst->a + secondWeight * inSecond->a;
	
	return new Color( r, g, b, a,
                      inFirst->mCompositeBuilt || inSecond->mCompositeBuilt );
	}



inline unsigned long Color::rebuildComposite() {
	composite = ((int)(b*255)) | ((int)(g*255)) << 8 | ((int)(r*255)) << 16 |
        ((int)(a*255)) << 24;
    mCompositeBuilt = true;
	return composite;
	}

inline float Color::getMax() {
	float max = -FLT_MAX;
	if( r > max ) max = r;
	if( g > max ) max = g;
	if( b > max ) max = b;
	return max;
	}



inline void Color::weightColor( float weight ) {
	r = r * weight;
	g = g * weight;
	b = b * weight;
	// for now, don't touch alpha

    if( mCompositeBuilt ) {
        rebuildComposite();
        }
	}	



inline void Color::invert() {
    r = 1 - r;
    g = 1 - g;
    b = 1 - b;
    }



inline void Color::saturate() {
    if( r < g && r < b ) {
        r = 0;
        }
    else if( g < r && g < b ) {
        g = 0;
        }
    else if( b < r && b < g ) {
        b = 0;
        }
    else if( r != 0 ) {
        // they are all equal, but non-zero

        // default to dropping red
        r = 0;
        }
    //else
    // they are all 0
    // leave as black
    }



inline void Color::weightColor( Color *inWeightColor ) {
    r *= inWeightColor->r;
    g *= inWeightColor->g;
    b *= inWeightColor->b;
    a *= inWeightColor->a;

    if( mCompositeBuilt ) {
        rebuildComposite();
        }
    }



inline float &Color::operator[](int rgbIndex) {
	if( rgbIndex == 0) return r;
	else if( rgbIndex == 1) return g;
	else if( rgbIndex == 2) return b;
	else if( rgbIndex == 3) return a;
	return r;		// default, return r reference
	}
	
	
inline unsigned long Color::getWeightedComposite( float weight ) {
	return ((int)(b*255*weight)) | ((int)(g*255*weight)) << 8 | ((int)(r*255*weight)) << 16 | ((int)(a*255)) << 24;
	}


inline unsigned long Color::getWeightedComposite( unsigned long c1, float weight ) {
	int b = c1 & 0xFF;
	
	int g = (c1 >> 8) & 0xFF;
	
	int r = (c1 >> 16) & 0xFF;

	int a = c1 >> 24;
	
	return ((int)(b*weight)) | (((int)(g*weight)) << 8) | (((int)(r*weight)) << 16) | (((int)(a*weight)) << 24);
	}

inline unsigned long Color::sumComposite(unsigned long c1, unsigned long c2) {
	int b = (c1 & 0xFF) + (c2 & 0xFF);
	if( b > 255) b = 255;
	
	int g = ((c1 >> 8) & 0xFF) + ((c2 >> 8) & 0xFF);
	if( g > 255) g = 255;
	
	int r = ((c1 >> 16) & 0xFF) + ((c2 >> 16) & 0xFF);
	if( r > 255) r = 255;

	int a = (c1 >> 24) + (c2 >> 24);
	if( a > 255) a = 255;

	return b | (g << 8) | (r << 16) | (a << 24);
	}
#endif