diff --git a/color.c b/color.c index cab537ca..e8ededa0 100644 --- a/color.c +++ b/color.c @@ -25,6 +25,7 @@ #include #define RGB_8TO16(i) (0xffff * ((i) & 0xff) / 0xff) +#define RGB_16TO8(i) (0xff * ((i) & 0xffff) / 0xffff) /** Parse an hexadecimal color string to its component. * \param colstr The color string. @@ -255,4 +256,27 @@ xcolor_init_reply(xcolor_init_request_t req) return false; } +/** Convert a xcolor struct to a color one. + * \param xcol The X color. + * \param col The color. + * \return True if everything has been converted. + */ +bool +xcolor_to_color(const xcolor_t *xcol, color_t *col) +{ + if (!xcol->initialized) + { + col->initialized = false; + return false; + } + + col->initialized = true; + col->red = RGB_16TO8(xcol->red); + col->green = RGB_16TO8(xcol->green); + col->blue = RGB_16TO8(xcol->blue); + col->alpha = RGB_16TO8(xcol->alpha); + + return true; +} + // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80 diff --git a/color.h b/color.h index 378656c7..7fdc3a68 100644 --- a/color.h +++ b/color.h @@ -72,6 +72,8 @@ bool color_init_reply(color_init_cookie_t); xcolor_init_request_t xcolor_init_unchecked(xcolor_t *, const char *, ssize_t); bool xcolor_init_reply(xcolor_init_request_t); +bool xcolor_to_color(const xcolor_t *, color_t *); + #endif // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80