]> Dogcows Code - chaz/rasterize/blob - color.h
initial commit
[chaz/rasterize] / color.h
1
2 /*
3 * CS5600 University of Utah
4 * Charles McGarvey
5 * mcgarvey@eng.utah.edu
6 */
7
8 #ifndef __COLOR_H__
9 #define __COLOR_H__
10
11 #include "common.h"
12
13
14 /*
15 * A color channel will be the same as a scalar.
16 */
17 typedef scal_t colorchan_t;
18
19 /*
20 * A color class.
21 * Colors are represented by RGBA values between 0.0 and 1.0.
22 */
23 struct color
24 {
25 colorchan_t r;
26 colorchan_t g;
27 colorchan_t b;
28 colorchan_t a;
29 };
30 typedef struct color color_t;
31
32 /*
33 * Initialize a color.
34 */
35 __fast__
36 void color_init(color_t* c, colorchan_t r, colorchan_t g, colorchan_t b, colorchan_t a)
37 {
38 c->r = r;
39 c->g = g;
40 c->b = b;
41 c->a = a;
42 }
43
44
45 /*
46 * Create a new color, copied by value.
47 */
48 __fast__
49 color_t color_new(colorchan_t r, colorchan_t g, colorchan_t b, colorchan_t a)
50 {
51 color_t c;
52 color_init(&c, r, g, b, a);
53 return c;
54 }
55
56 #define COLOR_CLEAR color_new(S(0.0), S(0.0), S(0.0), S(0.0))
57 #define COLOR_BLACK color_new(S(0.0), S(0.0), S(0.0), S(1.0))
58 #define COLOR_RED color_new(S(1.0), S(0.0), S(0.0), S(1.0))
59 #define COLOR_GREEN color_new(S(0.0), S(1.0), S(0.0), S(1.0))
60 #define COLOR_BLUE color_new(S(0.0), S(0.0), S(1.0), S(1.0))
61 #define COLOR_YELLOW color_new(S(1.0), S(1.0), S(0.0), S(1.0))
62 #define COLOR_MAGENTA color_new(S(1.0), S(0.0), S(1.0), S(1.0))
63 #define COLOR_CYAN color_new(S(0.0), S(1.0), S(1.0), S(1.0))
64 #define COLOR_WHITE color_new(S(1.0), S(1.0), S(1.0), S(1.0))
65
66
67 /*
68 * Define integer types for a 32-bit RGBA color representation.
69 */
70 typedef uint32_t rgba_t;
71 typedef uint8_t rgbachan_t;
72
73 /*
74 * Create a new color from a 32-bit RGBA value.
75 */
76 __fast__
77 color_t color_from_rgba(rgba_t n)
78 {
79 colorchan_t r = (colorchan_t)UNPACK(n, 3) / S(255.0);
80 colorchan_t g = (colorchan_t)UNPACK(n, 2) / S(255.0);
81 colorchan_t b = (colorchan_t)UNPACK(n, 1) / S(255.0);
82 colorchan_t a = (colorchan_t)UNPACK(n, 0) / S(255.0);
83 return color_new(r, g, b, a);
84 }
85
86 /*
87 * Split a color into 8-bit RGBA channels.
88 */
89 __fast__
90 void color_split(color_t c, rgbachan_t* r, rgbachan_t* g, rgbachan_t* b, rgbachan_t* a)
91 {
92 if (r) *r = (rgbachan_t)(c.r * S(255.0));
93 if (g) *g = (rgbachan_t)(c.g * S(255.0));
94 if (b) *b = (rgbachan_t)(c.b * S(255.0));
95 if (a) *a = (rgbachan_t)(c.a * S(255.0));
96 }
97
98 /*
99 * Convert a color to a 32-bit RGBA value.
100 */
101 __fast__
102 rgba_t rgba_from_color(color_t c)
103 {
104 rgbachan_t r, g, b, a;
105 color_split(c, &r, &g, &b, &a);
106 return ((rgba_t)r << 24) | ((rgba_t)g << 16) | ((rgba_t)b << 8) | (rgba_t)a;
107 }
108
109
110 #endif // __COLOR_H__
111
This page took 0.037466 seconds and 4 git commands to generate.