summaryrefslogtreecommitdiff
path: root/src/xfaces.c
diff options
context:
space:
mode:
authorMattias EngdegÄrd <mattiase@acm.org>2020-06-12 18:12:37 +0200
committerMattias EngdegÄrd <mattiase@acm.org>2020-06-21 21:22:26 +0200
commit9fe2bdb88a4ebd4b2286c1c2a2a2ba7411af01b6 (patch)
tree0979ec4f38172e25a0420eca5b22650c249a80f4 /src/xfaces.c
parent0792f8e4f0de2328c57d552a5845bdf77265a971 (diff)
downloademacs-9fe2bdb88a4ebd4b2286c1c2a2a2ba7411af01b6.tar.gz
emacs-9fe2bdb88a4ebd4b2286c1c2a2a2ba7411af01b6.tar.bz2
emacs-9fe2bdb88a4ebd4b2286c1c2a2a2ba7411af01b6.zip
Consolidate #RGB string parsers
Use a single parser of color strings in the #RGB, rgb:R/G/B and rgbi:R/G/B formats, replacing four existing ones. Previously, error-checking was spotty, handling of the rgbi: format not always present, and normalization of the result was sometimes incorrect. * src/dispextern.h: New prototype. * src/xfaces.c (parse_hex_color_comp, parse_float_color_comp) (parse_color_spec, Finternal-color_values_from_color_spec): New functions. * test/src/xfaces-tests.el (xfaces-internal-color-values-from-color-spec): New test. * lisp/term/tty-colors.el (tty-color-standard-values): Use internal-color-values-from-color-spec, replacing old parser. * src/nsterm.m (ns_get_color): * src/w32fns.c (x_to_w32_color): * src/xterm.c (x_parse_color): Use parse_color_spec, replacing old parsers. (HEX_COLOR_NAME_LENGTH): Remove #define.
Diffstat (limited to 'src/xfaces.c')
-rw-r--r--src/xfaces.c116
1 files changed, 116 insertions, 0 deletions
diff --git a/src/xfaces.c b/src/xfaces.c
index cf155288bd1..308509a0267 100644
--- a/src/xfaces.c
+++ b/src/xfaces.c
@@ -220,6 +220,7 @@ along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
#include "sysstdio.h"
#include <sys/types.h>
#include <sys/stat.h>
+#include <math.h>
#include "lisp.h"
#include "character.h"
@@ -819,6 +820,120 @@ load_pixmap (struct frame *f, Lisp_Object name)
Color Handling
***********************************************************************/
+/* Parse hex color component at S ending right before E.
+ Set *DST to the value normalized so that the maximum for the
+ number of digits given becomes 65535, and return true on success,
+ false otherwise. */
+static bool
+parse_hex_color_comp (const char *s, const char *e, unsigned short *dst)
+{
+ int n = e - s;
+ if (n <= 0 || n > 4)
+ return false;
+ int val = 0;
+ for (; s < e; s++)
+ {
+ int digit;
+ if (*s >= '0' && *s <= '9')
+ digit = *s - '0';
+ else if (*s >= 'A' && *s <= 'F')
+ digit = *s - 'A' + 10;
+ else if (*s >= 'a' && *s <= 'f')
+ digit = *s - 'a' + 10;
+ else
+ return false;
+ val = (val << 4) | digit;
+ }
+ int maxval = (1 << (n * 4)) - 1;
+ *dst = (unsigned)val * 65535 / maxval;
+ return true;
+}
+
+/* Parse floating-point color component at S ending right before E.
+ Return the number if in the range [0,1]; otherwise -1. */
+static double
+parse_float_color_comp (const char *s, const char *e)
+{
+ char *end;
+ double x = strtod (s, &end);
+ return (end == e && x >= 0 && x <= 1) ? x : -1;
+}
+
+/* Parse S as a numeric color specification and set *R, *G and *B.
+ Return true on success, false on failure.
+ Recognized formats:
+
+ "#RGB", with R, G and B hex strings of equal length, 1-4 digits each
+ "rgb:R/G/B", with R, G and B hex strings, 1-4 digits each
+ "rgbi:R/G/B", with R, G and B numbers in [0,1]
+
+ The result is normalized to a maximum value of 65535 per component. */
+bool
+parse_color_spec (const char *s,
+ unsigned short *r, unsigned short *g, unsigned short *b)
+{
+ int len = strlen (s);
+ if (s[0] == '#')
+ {
+ if ((len - 1) % 3 == 0)
+ {
+ int n = (len - 1) / 3;
+ return ( parse_hex_color_comp (s + 1 + 0 * n, s + 1 + 1 * n, r)
+ && parse_hex_color_comp (s + 1 + 1 * n, s + 1 + 2 * n, g)
+ && parse_hex_color_comp (s + 1 + 2 * n, s + 1 + 3 * n, b));
+ }
+ }
+ else if (strncmp (s, "rgb:", 4) == 0)
+ {
+ char *sep1, *sep2;
+ return ((sep1 = strchr (s + 4, '/')) != NULL
+ && (sep2 = strchr (sep1 + 1, '/')) != NULL
+ && parse_hex_color_comp (s + 4, sep1, r)
+ && parse_hex_color_comp (sep1 + 1, sep2, g)
+ && parse_hex_color_comp (sep2 + 1, s + len, b));
+ }
+ else if (strncmp (s, "rgbi:", 5) == 0)
+ {
+ char *sep1, *sep2;
+ double red, green, blue;
+ if ((sep1 = strchr (s + 5, '/')) != NULL
+ && (sep2 = strchr (sep1 + 1, '/')) != NULL
+ && (red = parse_float_color_comp (s + 5, sep1)) >= 0
+ && (green = parse_float_color_comp (sep1 + 1, sep2)) >= 0
+ && (blue = parse_float_color_comp (sep2 + 1, s + len)) >= 0)
+ {
+ *r = lrint (red * 65535);
+ *g = lrint (green * 65535);
+ *b = lrint (blue * 65535);
+ return true;
+ }
+ }
+ return false;
+}
+
+DEFUN ("internal-color-values-from-color-spec",
+ Finternal_color_values_from_color_spec,
+ Sinternal_color_values_from_color_spec,
+ 1, 1, 0,
+ doc: /* Parse STRING as a numeric color and return (RED GREEN BLUE).
+Recognised formats for STRING are:
+
+ #RGB, where R, G and B are hex numbers of equal length, 1-4 digits each
+ rgb:R/G/B, where R, G, and B are hex numbers, 1-4 digits each
+ rgbi:R/G/B, where R, G and B are floating-point numbers in [0,1]
+
+The result is normalized to a maximum value of 65535 per component,
+forming a list of three integers in [0,65535].
+If STRING is not in one of the above forms, return nil. */)
+ (Lisp_Object string)
+{
+ CHECK_STRING (string);
+ unsigned short r, g, b;
+ return (parse_color_spec (SSDATA (string), &r, &g, &b)
+ ? list3i (r, g, b)
+ : Qnil);
+}
+
/* Parse RGB_LIST, and fill in the RGB fields of COLOR.
RGB_LIST should contain (at least) 3 lisp integers.
Return true iff RGB_LIST is OK. */
@@ -7018,4 +7133,5 @@ clear the face cache, see `clear-face-cache'. */);
defsubr (&Sinternal_face_x_get_resource);
defsubr (&Sx_family_fonts);
#endif
+ defsubr (&Sinternal_color_values_from_color_spec);
}