+#include"abc_html.h"
+
+typedef struct css_color_s
+{
+ char** names;
+ double r;
+ double g;
+ double b;
+} css_color_t;
+
+static char* css_red[] = {"red", "红", NULL};
+static char* css_green[] = {"green", "绿", NULL};
+static char* css_blue[] = {"blue", "蓝", NULL};
+
+static char* css_white[] = {"white", "白", NULL};
+static char* css_black[] = {"black", "黑", NULL};
+
+static css_color_t css_colors[] =
+{
+ {css_red, 1.0, 0.0, 0.0},
+ {css_green, 0.0, 1.0, 0.0},
+ {css_blue, 0.0, 0.0, 1.0},
+
+ {css_white, 1.0, 1.0, 1.0},
+ {css_black, 0.0, 0.0, 0.0},
+};
+
+int abc_css_color(double* r, double* g, double* b, const uint8_t* str)
+{
+ *r = 0.0;
+ *g = 0.0;
+ *b = 0.0;
+
+ if ('#' == *str) {
+ uint64_t value = 0;
+
+ while (*++str) {
+ int c = *str;
+
+ if ('0' <= c && '9' >= c) {
+ value <<= 4;
+ value += c - '0';
+ } else {
+ c |= 0x20;
+
+ if ('a' <= c && 'f' >= c) {
+ value <<= 4;
+ value += c - 'a' + 10;
+ }
+ }
+ }
+
+ *r = ((value >> 16) & 0xff) / 255.0;
+ *g = ((value >> 8) & 0xff) / 255.0;
+ *b = ( value & 0xff) / 255.0;
+
+ } else if (!strncmp(str, "rgb(", 4)) {
+ const uint8_t* p = str + 4;
+
+ str = p;
+ while (*str && ',' != *str)
+ str++;
+ *r = atof(p) / 255.0;
+
+ if (*str) {
+ p = ++str;
+ while (*str && ',' != *str)
+ str++;
+ *g = atof(p) / 255.0;
+
+ if (*str) {
+ p = ++str;
+ while (*str && ')' != *str)
+ str++;
+ *b = atof(p) / 255.0;
+ }
+ }
+ } else {
+ int i;
+ for (i = 0; i < sizeof(css_colors) / sizeof(css_colors[0]); i++) {
+ css_color_t* c = &css_colors[i];
+
+ int j;
+ for (j = 0; c->names[j]; j++) {
+ if (!strcmp(c->names[j], str)) {
+ *r = c->r;
+ *g = c->g;
+ *b = c->b;
+ return 0;
+ }
+ }
+ }
+
+ return -1;
+ }
+
+ return 0;
+}