diff options
Diffstat (limited to 'src/xfaces.c')
| -rw-r--r-- | src/xfaces.c | 47 |
1 files changed, 25 insertions, 22 deletions
diff --git a/src/xfaces.c b/src/xfaces.c index 83d4c3f1f2c..b0052cdd455 100644 --- a/src/xfaces.c +++ b/src/xfaces.c | |||
| @@ -950,19 +950,25 @@ parse_hex_color_comp (const char *s, const char *e, unsigned short *dst) | |||
| 950 | } | 950 | } |
| 951 | 951 | ||
| 952 | /* Parse floating-point color component specification that starts at S | 952 | /* Parse floating-point color component specification that starts at S |
| 953 | and ends right before E. Return the parsed number if in the range | 953 | and ends right before E. Put the integer near-equivalent of that |
| 954 | [0,1]; otherwise return -1. */ | 954 | into *DST. Return true if successful, false otherwise. */ |
| 955 | static double | 955 | static bool |
| 956 | parse_float_color_comp (const char *s, const char *e) | 956 | parse_float_color_comp (const char *s, const char *e, unsigned short *dst) |
| 957 | { | 957 | { |
| 958 | /* Only allow decimal float literals without whitespace. */ | 958 | /* Only allow decimal float literals without whitespace. */ |
| 959 | for (const char *p = s; p < e; p++) | 959 | for (const char *p = s; p < e; p++) |
| 960 | if (!((*p >= '0' && *p <= '9') | 960 | if (!((*p >= '0' && *p <= '9') |
| 961 | || *p == '.' || *p == '+' || *p == '-' || *p == 'e' || *p == 'E')) | 961 | || *p == '.' || *p == '+' || *p == '-' || *p == 'e' || *p == 'E')) |
| 962 | return -1; | 962 | return false; |
| 963 | char *end; | 963 | char *end; |
| 964 | double x = strtod (s, &end); | 964 | double x = strtod (s, &end); |
| 965 | return (end == e && x >= 0 && x <= 1) ? x : -1; | 965 | if (end == e && 0 <= x && x <= 1) |
| 966 | { | ||
| 967 | *dst = lrint (x * 65535); | ||
| 968 | return true; | ||
| 969 | } | ||
| 970 | else | ||
| 971 | return false; | ||
| 966 | } | 972 | } |
| 967 | 973 | ||
| 968 | /* Parse SPEC as a numeric color specification and set *R, *G and *B. | 974 | /* Parse SPEC as a numeric color specification and set *R, *G and *B. |
| @@ -997,28 +1003,25 @@ parse_color_spec (const char *spec, | |||
| 997 | } | 1003 | } |
| 998 | else if (strncmp (spec, "rgb:", 4) == 0) | 1004 | else if (strncmp (spec, "rgb:", 4) == 0) |
| 999 | { | 1005 | { |
| 1000 | char *sep1, *sep2; | 1006 | char const *sep1 = strchr (spec + 4, '/'); |
| 1001 | return ((sep1 = strchr (spec + 4, '/')) != NULL | 1007 | if (!sep1) |
| 1002 | && (sep2 = strchr (sep1 + 1, '/')) != NULL | 1008 | return false; |
| 1009 | char const *sep2 = strchr (sep1 + 1, '/'); | ||
| 1010 | return (sep2 | ||
| 1003 | && parse_hex_color_comp (spec + 4, sep1, r) | 1011 | && parse_hex_color_comp (spec + 4, sep1, r) |
| 1004 | && parse_hex_color_comp (sep1 + 1, sep2, g) | 1012 | && parse_hex_color_comp (sep1 + 1, sep2, g) |
| 1005 | && parse_hex_color_comp (sep2 + 1, spec + len, b)); | 1013 | && parse_hex_color_comp (sep2 + 1, spec + len, b)); |
| 1006 | } | 1014 | } |
| 1007 | else if (strncmp (spec, "rgbi:", 5) == 0) | 1015 | else if (strncmp (spec, "rgbi:", 5) == 0) |
| 1008 | { | 1016 | { |
| 1009 | char *sep1, *sep2; | 1017 | char const *sep1 = strchr (spec + 5, '/'); |
| 1010 | double red, green, blue; | 1018 | if (!sep1) |
| 1011 | if ((sep1 = strchr (spec + 5, '/')) != NULL | 1019 | return false; |
| 1012 | && (sep2 = strchr (sep1 + 1, '/')) != NULL | 1020 | char const *sep2 = strchr (sep1 + 1, '/'); |
| 1013 | && (red = parse_float_color_comp (spec + 5, sep1)) >= 0 | 1021 | return (sep2 |
| 1014 | && (green = parse_float_color_comp (sep1 + 1, sep2)) >= 0 | 1022 | && parse_float_color_comp (spec + 5, sep1, r) |
| 1015 | && (blue = parse_float_color_comp (sep2 + 1, spec + len)) >= 0) | 1023 | && parse_float_color_comp (sep1 + 1, sep2, g) |
| 1016 | { | 1024 | && parse_float_color_comp (sep2 + 1, spec + len, b)); |
| 1017 | *r = lrint (red * 65535); | ||
| 1018 | *g = lrint (green * 65535); | ||
| 1019 | *b = lrint (blue * 65535); | ||
| 1020 | return true; | ||
| 1021 | } | ||
| 1022 | } | 1025 | } |
| 1023 | return false; | 1026 | return false; |
| 1024 | } | 1027 | } |