From 95c723cdcfe9a574608d9d85288588b9ea982bc9 Mon Sep 17 00:00:00 2001 From: "jan.nijtmans" Date: Mon, 16 Jul 2012 15:17:27 +0000 Subject: better wrapper, with one less roundtrip through X11 server --- generic/tkColor.c | 14 ++++---------- generic/tkInt.h | 2 ++ unix/tkUnixColor.c | 23 +++++++++++++++++++++-- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/generic/tkColor.c b/generic/tkColor.c index f1258f5..ea03e28 100644 --- a/generic/tkColor.c +++ b/generic/tkColor.c @@ -832,7 +832,7 @@ TkDebugColor( #undef XParseColor -static CONST char *CONST webColors[] = { +const char *const tkWebColors[20] = { /* 'a' */ "qua\0#0000ffffffff", /* 'b' */ NULL, /* 'c' */ "rimson\0#dcdc14143c3c", @@ -852,13 +852,7 @@ static CONST char *CONST webColors[] = { /* 'q' */ NULL, /* 'r' */ NULL, /* 's' */ "ilver\0#c0c0c0c0c0c0", - /* 't' */ "eal\0#000080808080", - /* 'u' */ NULL, - /* 'v' */ NULL, - /* 'w' */ NULL, - /* 'x' */ NULL, - /* 'y' */ NULL, - /* 'z' */ NULL + /* 't' */ "eal\0#000080808080" }; Status @@ -914,14 +908,14 @@ TkParseColor( name -= 13; } goto done; - } else if (((*name - 'A') & 0xdf) < sizeof(webColors)/sizeof(webColors[0])) { + } else if (((*name - 'A') & 0xdf) < sizeof(tkWebColors)/sizeof(tkWebColors[0])) { if (!((name[0] - 'G') & 0xdf) && !((name[1] - 'R') & 0xdf) && !((name[2] - 'A') & 0xdb) && !((name[3] - 'Y') & 0xdf) && !name[4]) { name = "#808080808080"; goto done; } else { - const char *p = webColors[((*name - 'A') & 0x1f)]; + const char *p = tkWebColors[((*name - 'A') & 0x1f)]; if (p) { const char *q = name; while (!((*p - *(++q)) & 0xdf)) { diff --git a/generic/tkInt.h b/generic/tkInt.h index 88e0c25..01cc9cb 100644 --- a/generic/tkInt.h +++ b/generic/tkInt.h @@ -944,6 +944,8 @@ MODULE_SCOPE TkMainInfo *tkMainWindowList; MODULE_SCOPE Tk_ImageType tkPhotoImageType; MODULE_SCOPE Tcl_HashTable tkPredefBitmapTable; +MODULE_SCOPE const char *const tkWebColors[20]; + /* * The definition of pi, at least from the perspective of double-precision * floats. diff --git a/unix/tkUnixColor.c b/unix/tkUnixColor.c index aa01f24..43500ad 100644 --- a/unix/tkUnixColor.c +++ b/unix/tkUnixColor.c @@ -133,10 +133,28 @@ TkpGetColor( * names. */ - /* TODO: better wrapper for XAllocNamedColor, for now always use TkParseColor */ - if (0 && *name != '#') { + if (*name != '#') { XColor screen; + if (((*name - 'A') & 0xdf) < sizeof(tkWebColors)/sizeof(tkWebColors[0])) { + if (!((name[0] - 'G') & 0xdf) && !((name[1] - 'R') & 0xdf) + && !((name[2] - 'A') & 0xdb) && !((name[3] - 'Y') & 0xdf) + && !name[4]) { + name = "#808080808080"; + goto gotWebColor; + } else { + const char *p = tkWebColors[((*name - 'A') & 0x1f)]; + if (p) { + const char *q = name; + while (!((*p - *(++q)) & 0xdf)) { + if (!*p++) { + name = p; + goto gotWebColor; + } + } + } + } + } if (strlen(name) > 99) { /* Don't bother to parse this. [Bug 2809525]*/ return (TkColor *) NULL; @@ -156,6 +174,7 @@ TkpGetColor( FindClosestColor(tkwin, &screen, &color); } } else { + gotWebColor: if (TkParseColor(display, colormap, name, &color) == 0) { return NULL; } -- cgit v0.12