Revert "Floating point color config formats"

This reverts commit 6b28d57730a390264b4d0daea2d765f841ad90d8.
This commit is contained in:
Nikita Zlobin 2020-05-26 22:27:31 +05:00
parent 98debc663a
commit d110ba93e6

View File

@ -59,61 +59,37 @@ static struct color hex_to_color(uintmax_t hexValue, int dpc)
return ret; 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) static struct color string_to_color(const char *str)
{ {
if (str[0] == '#') { uintmax_t val;
uintmax_t val; unsigned clen;
unsigned clen;
{
int cn;
/* accept 3 or 4 equal components */ {
char *end;
val = strtoumax(str+1, &end, 16);
if (errno == ERANGE || (end[0] != '\0' && end[1] != '\0'))
goto err;
const int len = (end - (str+1));
if (len % 3 == 0) cn = 3;
else if (len % 4 == 0) cn = 4;
else goto err;
clen = len / cn;
}
/* component length must be 2^n */ {
unsigned mask = 1;
while(mask < clen) mask <<= 1;
if (mask != clen) goto err;
}
/* turn 3-component to opaque 4-components */ {
const unsigned csize = clen * 4;
if (cn == 3)
val = (val << csize) | UINT_MAX_N(csize);
}
}
return hex_to_color(val, clen);
}
/* rgba(fp, fp, fp, fp) */
{ {
struct color col; int cn;
if (sscanf(str, "rgba ( %lf , %lf , %lf , %lf )", &col.r, &col.g, &col.b, &col.a) == 4) {
color_clip(&col); /* accept 3 or 4 equal components */ {
return col; char *end;
val = strtoumax(str+1, &end, 16);
if (errno == ERANGE || (end[0] != '\0' && end[1] != '\0'))
goto err;
const int len = (end - (str+1));
if (len % 3 == 0) cn = 3;
else if (len % 4 == 0) cn = 4;
else goto err;
clen = len / cn;
} }
if (sscanf(str, "rgb ( %lf , %lf , %lf )", &col.r, &col.g, &col.b) == 3) { /* component length must be 2^n */ {
color_clip(&col); unsigned mask = 1;
col.a = 1.0; while(mask < clen) mask <<= 1;
return col; if (mask != clen) goto err;
}
/* turn 3-component to opaque 4-components */ {
const unsigned csize = clen * 4;
if (cn == 3)
val = (val << csize) | UINT_MAX_N(csize);
} }
} }
return hex_to_color(val, clen);
/* return black on error */ /* return black on error */
err: err: