Floating point color config formats

This commit is contained in:
Nikita Zlobin 2020-05-23 02:06:07 +05:00
parent 1e36a6b4ac
commit 6b28d57730

View File

@ -59,8 +59,17 @@ static struct color hex_to_color(uintmax_t hexValue, int dpc)
return ret;
}
static inline void color_clip(struct color * c)
{
if (c->r > 1.0) c->r = 1.0;
if (c->g > 1.0) c->g = 1.0;
if (c->b > 1.0) c->b = 1.0;
if (c->a > 1.0) c->a = 1.0;
}
static struct color string_to_color(const char *str)
{
if (str[0] == '#') {
uintmax_t val;
unsigned clen;
{
@ -90,6 +99,21 @@ static struct color string_to_color(const char *str)
}
}
return hex_to_color(val, clen);
}
/* rgba(fp, fp, fp, fp) */
{
struct color col;
if (sscanf(str, "rgba ( %lf , %lf , %lf , %lf )", &col.r, &col.g, &col.b, &col.a) == 4) {
color_clip(&col);
return col;
}
if (sscanf(str, "rgb ( %lf , %lf , %lf )", &col.r, &col.g, &col.b) == 3) {
color_clip(&col);
col.a = 1.0;
return col;
}
}
/* return black on error */
err: