16 #include "util/color.h"
17 #include "util/conv.h"
18 #include "util/fastfind.h"
19 #include "util/string.h"
26 static const struct color_spec color_specs
[] = {
27 #include "util/color_s.inc"
29 #include "util/color.inc"
36 static const struct color_spec
*internal_pointer
;
39 colors_list_reset(void)
41 internal_pointer
= color_specs
;
44 /** Returns a pointer to a struct that contains
45 * current key and data pointers and increment
47 * It returns NULL when key is NULL. */
48 static struct fastfind_key_value
*
49 colors_list_next(void)
51 static struct fastfind_key_value kv
;
53 if (!internal_pointer
->name
) return NULL
;
55 kv
.key
= (unsigned char *) internal_pointer
->name
;
56 kv
.data
= (void *) internal_pointer
; /* cast away const */
63 static struct fastfind_index ff_colors_index
64 = INIT_FASTFIND_INDEX("colors_lookup", colors_list_reset
, colors_list_next
);
66 #endif /* USE_FASTFIND */
69 init_colors_lookup(void)
72 fastfind_index(&ff_colors_index
, FF_COMPRESS
);
77 free_colors_lookup(void)
80 fastfind_done(&ff_colors_index
);
85 decode_color(unsigned char *str
, int slen
, color_T
*color
)
87 if (*str
== '#' && (slen
== 7 || slen
== 4)) {
88 unsigned char buffer
[7];
96 /* Expand the short hex color format */
97 buffer
[0] = buffer
[1] = str
[0];
98 buffer
[2] = buffer
[3] = str
[1];
99 buffer
[4] = buffer
[5] = str
[2];
105 string_color
= strtoul(str
, (char **) &end
, 16);
106 if (!errno
&& (end
== str
+ 6) && string_color
<= 0xFFFFFF) {
107 *color
= string_color
;
111 const struct color_spec
*cs
;
114 for (cs
= color_specs
; cs
->name
; cs
++)
115 if (!strlcasecmp(cs
->name
, -1, str
, slen
))
118 cs
= fastfind_search(&ff_colors_index
, str
, slen
);
120 if (cs
&& cs
->name
) {
124 } else if (slen
== 6 || slen
== 3) {
125 /* Check if the string is just the hexadecimal rgb
126 * color notation with the leading '#' missing and
127 * treat it as such. */
130 while (len
< slen
&& isxdigit(str
[len
])) len
++;
132 if (len
== slen
) goto decode_hex_color
;
136 return -1; /* Not found */
139 const unsigned char *
140 get_color_string(color_T color
, unsigned char hexcolor
[8])
142 const struct color_spec
*cs
;
144 for (cs
= color_specs
; cs
->name
; cs
++)
145 if (cs
->rgb
== color
)
148 color_to_string(color
, hexcolor
);
153 color_to_string(color_T color
, unsigned char str
[8])
156 elinks_ulongcat(&str
[1], NULL
, (unsigned long) color
, 6, '0', 16, 0);