/* * CS5600 University of Utah * Charles McGarvey * mcgarvey@eng.utah.edu */ #ifndef __COLOR_H__ #define __COLOR_H__ #include "common.h" /* * A color channel will be the same as a scalar. */ typedef scal_t colorchan_t; /* * A color class. * Colors are represented by RGBA values between 0.0 and 1.0. */ struct color { colorchan_t r; colorchan_t g; colorchan_t b; colorchan_t a; }; typedef struct color color_t; /* * Initialize a color. */ __fast__ void color_init(color_t* c, colorchan_t r, colorchan_t g, colorchan_t b, colorchan_t a) { c->r = r; c->g = g; c->b = b; c->a = a; } /* * Create a new color, copied by value. */ __fast__ color_t color_new(colorchan_t r, colorchan_t g, colorchan_t b, colorchan_t a) { color_t c; color_init(&c, r, g, b, a); return c; } #define COLOR_CLEAR color_new(S(0.0), S(0.0), S(0.0), S(0.0)) #define COLOR_BLACK color_new(S(0.0), S(0.0), S(0.0), S(1.0)) #define COLOR_RED color_new(S(1.0), S(0.0), S(0.0), S(1.0)) #define COLOR_GREEN color_new(S(0.0), S(1.0), S(0.0), S(1.0)) #define COLOR_BLUE color_new(S(0.0), S(0.0), S(1.0), S(1.0)) #define COLOR_YELLOW color_new(S(1.0), S(1.0), S(0.0), S(1.0)) #define COLOR_MAGENTA color_new(S(1.0), S(0.0), S(1.0), S(1.0)) #define COLOR_CYAN color_new(S(0.0), S(1.0), S(1.0), S(1.0)) #define COLOR_WHITE color_new(S(1.0), S(1.0), S(1.0), S(1.0)) /* * Define integer types for a 32-bit RGBA color representation. */ typedef uint32_t rgba_t; typedef uint8_t rgbachan_t; /* * Create a new color from a 32-bit RGBA value. */ __fast__ color_t color_from_rgba(rgba_t n) { colorchan_t r = (colorchan_t)UNPACK(n, 3) / S(255.0); colorchan_t g = (colorchan_t)UNPACK(n, 2) / S(255.0); colorchan_t b = (colorchan_t)UNPACK(n, 1) / S(255.0); colorchan_t a = (colorchan_t)UNPACK(n, 0) / S(255.0); return color_new(r, g, b, a); } /* * Split a color into 8-bit RGBA channels. */ __fast__ void color_split(color_t c, rgbachan_t* r, rgbachan_t* g, rgbachan_t* b, rgbachan_t* a) { if (r) *r = (rgbachan_t)(c.r * S(255.0)); if (g) *g = (rgbachan_t)(c.g * S(255.0)); if (b) *b = (rgbachan_t)(c.b * S(255.0)); if (a) *a = (rgbachan_t)(c.a * S(255.0)); } /* * Convert a color to a 32-bit RGBA value. */ __fast__ rgba_t rgba_from_color(color_t c) { rgbachan_t r, g, b, a; color_split(c, &r, &g, &b, &a); return ((rgba_t)r << 24) | ((rgba_t)g << 16) | ((rgba_t)b << 8) | (rgba_t)a; } #endif // __COLOR_H__