#define COLOR_WHITE color_new(S(1.0), S(1.0), S(1.0), S(1.0))
+/*
+ * Get a pointer to the color data.
+ */
+INLINE_MAYBE
+const scal_t* color_data(const color_t* c)
+{
+ return &c->r;
+}
+
+
/*
* Print the color to stdout.
*/
typedef uint8_t rgbachan_t;
typedef uint32_t rgba_t;
-/*
- * Create a new color from a 32-bit RGBA value.
- */
-INLINE_MAYBE
-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.
*/
if (a) *a = (rgbachan_t)(c.a * S(255.0));
}
+/*
+ * Create a new color from a 32-bit RGBA value.
+ */
+INLINE_MAYBE
+color_t color_from_rgba(rgba_t n)
+{
+ union {
+ rgba_t rgba;
+ struct {
+ rgbachan_t r, g, b, a;
+ } chan;
+ } u;
+ u.rgba = n;
+ colorchan_t r = (colorchan_t)u.chan.r / S(255.0);
+ colorchan_t g = (colorchan_t)u.chan.g / S(255.0);
+ colorchan_t b = (colorchan_t)u.chan.b / S(255.0);
+ colorchan_t a = (colorchan_t)u.chan.a / S(255.0);
+ return color_new(r, g, b, a);
+}
+
/*
* Convert a color to a 32-bit RGBA value.
*/
INLINE_MAYBE
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;
+ union {
+ rgba_t rgba;
+ struct {
+ rgbachan_t r, g, b, a;
+ } chan;
+ } u;
+ color_split(c, &u.chan.r, &u.chan.g, &u.chan.b, &u.chan.a);
+ return u.rgba;
}