15 #include "util/color.h"
16 #include "util/conv.h"
17 #include "util/fastfind.h"
18 #include "util/string.h"
25 static struct color_spec color_specs
[] = {
26 #include "util/color_s.inc"
28 #include "util/color.inc"
35 static struct color_spec
*internal_pointer
;
38 colors_list_reset(void)
40 internal_pointer
= color_specs
;
43 /* Returns a pointer to a struct that contains
44 * current key and data pointers and increment
46 * It returns NULL when key is NULL. */
47 static struct fastfind_key_value
*
48 colors_list_next(void)
50 static struct fastfind_key_value kv
;
52 if (!internal_pointer
->name
) return NULL
;
54 kv
.key
= (unsigned char *) internal_pointer
->name
;
55 kv
.data
= internal_pointer
;
62 static struct fastfind_index ff_colors_index
63 = INIT_FASTFIND_INDEX("colors_lookup", colors_list_reset
, colors_list_next
);
65 #endif /* USE_FASTFIND */
68 init_colors_lookup(void)
71 fastfind_index(&ff_colors_index
, FF_COMPRESS
);
76 free_colors_lookup(void)
79 fastfind_done(&ff_colors_index
);
84 decode_color(unsigned char *str
, int slen
, color_T
*color
)
86 if (*str
== '#' && (slen
== 7 || slen
== 4)) {
87 unsigned char buffer
[7];
95 /* Expand the short hex color format */
96 buffer
[0] = buffer
[1] = str
[0];
97 buffer
[2] = buffer
[3] = str
[1];
98 buffer
[4] = buffer
[5] = str
[2];
104 string_color
= strtoul(str
, (char **) &end
, 16);
105 if (!errno
&& (end
== str
+ 6) && string_color
<= 0xFFFFFF) {
106 *color
= string_color
;
110 struct color_spec
*cs
;
113 for (cs
= color_specs
; cs
->name
; cs
++)
114 if (!strlcasecmp(cs
->name
, -1, str
, slen
))
117 cs
= fastfind_search(&ff_colors_index
, str
, slen
);
119 if (cs
&& cs
->name
) {
123 } else if (slen
== 6 || slen
== 3) {
124 /* Check if the string is just the hexadecimal rgb
125 * color notation with the leading '#' missing and
126 * treat it as such. */
129 while (len
< slen
&& isxdigit(str
[len
])) len
++;
131 if (len
== slen
) goto decode_hex_color
;
135 return -1; /* Not found */
139 get_color_string(color_T color
, unsigned char hexcolor
[8])
141 struct color_spec
*cs
;
143 for (cs
= color_specs
; cs
->name
; cs
++)
144 if (cs
->rgb
== color
)
147 color_to_string(color
, hexcolor
);
152 color_to_string(color_T color
, unsigned char str
[8])
155 elinks_ulongcat(&str
[1], NULL
, (unsigned long) color
, 6, '0', 16, 0);