]>
Dogcows Code - chaz/rasterize/blob - color.h
3 * CS5600 University of Utah
5 * mcgarvey@eng.utah.edu
15 * A color channel will be the same as a scalar.
17 typedef scal_t colorchan_t
;
21 * Colors are represented by RGBA values between 0.0 and 1.0.
30 typedef struct color color_t
;
36 void color_init(color_t
* c
, colorchan_t r
, colorchan_t g
, colorchan_t b
, colorchan_t a
)
46 * Create a new color, copied by value.
49 color_t
color_new(colorchan_t r
, colorchan_t g
, colorchan_t b
, colorchan_t a
)
52 color_init(&c
, r
, g
, b
, a
);
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))
68 * Print the color to stdout.
71 void color_print(color_t c
)
73 #if (SCALAR_SIZE == 8)
74 const char* fmt
= "[ %9.5lf %9.5lf %9.5lf %9.5lf ]";
76 const char* fmt
= "[ %9.5f %9.5f %9.5f %9.5f ]";
78 printf(fmt
, c
.r
, c
.g
, c
.b
, c
.a
);
83 * Add two colors together.
86 color_t
color_add(color_t c1
, color_t c2
)
88 return color_new(c1
.r
+ c2
.r
, c1
.g
+ c2
.g
, c1
.b
+ c2
.b
, c1
.a
+ c2
.a
);
92 * Add three colors together.
95 color_t
color_add2(color_t c1
, color_t c2
, color_t c3
)
97 return color_add(color_add(c1
, c2
), c3
);
101 * Multiply two colors together.
104 color_t
color_mult(color_t c1
, color_t c2
)
114 * Scale a color by some scalar coefficient.
117 color_t
color_scale(color_t c
, scal_t k
)
127 * Scale a color by another color and some scalar coefficient.
130 color_t
color_scale2(color_t c1
, color_t c2
, scal_t k
)
132 return color_scale(color_mult(c1
, c2
), k
);
136 * Clamp a color's channels to the normal range of 0.0 to 1.0.
139 color_t
color_clamp(color_t c
)
141 c
.r
= scal_clamp(c
.r
, S(0.0), S(1.0));
142 c
.g
= scal_clamp(c
.g
, S(0.0), S(1.0));
143 c
.b
= scal_clamp(c
.b
, S(0.0), S(1.0));
144 c
.a
= scal_clamp(c
.a
, S(0.0), S(1.0));
150 * Interpolate smoothly between two colors with an alpha value.
153 color_t
color_interp(color_t c1
, color_t c2
, scal_t a
)
155 return color_new(c1
.r
* (S(1.0) - a
) + c2
.r
* a
,
156 c1
.g
* (S(1.0) - a
) + c2
.g
* a
,
157 c1
.b
* (S(1.0) - a
) + c2
.b
* a
,
158 c1
.a
* (S(1.0) - a
) + c2
.a
* a
);
162 * Interpolate smoothly between three colors with barycentric coordinates.
165 color_t
color_interp2(color_t c1
, color_t c2
, color_t c3
, scal_t b
[3])
167 return color_new(c1
.r
* b
[0] + c2
.r
* b
[1] + c3
.r
* b
[2],
168 c1
.g
* b
[0] + c2
.g
* b
[1] + c3
.g
* b
[2],
169 c1
.b
* b
[0] + c2
.b
* b
[1] + c3
.b
* b
[2],
175 * Define integer types for a 32-bit RGBA color representation.
177 typedef uint8_t rgbachan_t
;
178 typedef uint32_t rgba_t
;
181 * Create a new color from a 32-bit RGBA value.
184 color_t
color_from_rgba(rgba_t n
)
186 colorchan_t r
= (colorchan_t
)UNPACK(n
, 3) / S(255.0);
187 colorchan_t g
= (colorchan_t
)UNPACK(n
, 2) / S(255.0);
188 colorchan_t b
= (colorchan_t
)UNPACK(n
, 1) / S(255.0);
189 colorchan_t a
= (colorchan_t
)UNPACK(n
, 0) / S(255.0);
190 return color_new(r
, g
, b
, a
);
194 * Split a color into 8-bit RGBA channels.
197 void color_split(color_t c
, rgbachan_t
* r
, rgbachan_t
* g
, rgbachan_t
* b
, rgbachan_t
* a
)
199 if (r
) *r
= (rgbachan_t
)(c
.r
* S(255.0));
200 if (g
) *g
= (rgbachan_t
)(c
.g
* S(255.0));
201 if (b
) *b
= (rgbachan_t
)(c
.b
* S(255.0));
202 if (a
) *a
= (rgbachan_t
)(c
.a
* S(255.0));
206 * Convert a color to a 32-bit RGBA value.
209 rgba_t
rgba_from_color(color_t c
)
211 rgbachan_t r
, g
, b
, a
;
212 color_split(c
, &r
, &g
, &b
, &a
);
213 return ((rgba_t
)r
<< 24) | ((rgba_t
)g
<< 16) | ((rgba_t
)b
<< 8) | (rgba_t
)a
;
This page took 0.047984 seconds and 4 git commands to generate.