diff options
Diffstat (limited to 'generic/tkColor.c')
-rw-r--r-- | generic/tkColor.c | 130 |
1 files changed, 127 insertions, 3 deletions
diff --git a/generic/tkColor.c b/generic/tkColor.c index f1a8680..ea03e28 100644 --- a/generic/tkColor.c +++ b/generic/tkColor.c @@ -10,8 +10,6 @@ * * See the file "license.terms" for information on usage and redistribution of * this file, and for a DISCLAIMER OF ALL WARRANTIES. - * - * RCS: @(#) $Id: tkColor.c,v 1.22 2009/12/09 22:41:52 dkf Exp $ */ #include "tkInt.h" @@ -373,6 +371,23 @@ Tk_NameOfColor( sprintf(tsdPtr->rgbString, "#%04x%04x%04x", colorPtr->red, colorPtr->green, colorPtr->blue); + + /* If the string has the form #RSRSTUTUVWVW (where equal + * letters denote equal hexdigits) then this is + * equivalent to #RSTUVW. Then output the shorter form. + */ + if ((tsdPtr->rgbString[1] == tsdPtr->rgbString[3]) + && (tsdPtr->rgbString[2] == tsdPtr->rgbString[4]) + && (tsdPtr->rgbString[5] == tsdPtr->rgbString[7]) + && (tsdPtr->rgbString[6] == tsdPtr->rgbString[8]) + && (tsdPtr->rgbString[9] == tsdPtr->rgbString[11]) + && (tsdPtr->rgbString[10] == tsdPtr->rgbString[12])) { + tsdPtr->rgbString[3] = tsdPtr->rgbString[5]; + tsdPtr->rgbString[4] = tsdPtr->rgbString[6]; + tsdPtr->rgbString[5] = tsdPtr->rgbString[9]; + tsdPtr->rgbString[6] = tsdPtr->rgbString[10]; + tsdPtr->rgbString[7] = '\0'; + } return tsdPtr->rgbString; } } @@ -809,7 +824,116 @@ TkDebugColor( } return resultPtr; } - + +#ifndef __WIN32__ + +/* This function is not necessary for Win32, + * since XParseColor already does the right thing */ + +#undef XParseColor + +const char *const tkWebColors[20] = { + /* 'a' */ "qua\0#0000ffffffff", + /* 'b' */ NULL, + /* 'c' */ "rimson\0#dcdc14143c3c", + /* 'd' */ NULL, + /* 'e' */ NULL, + /* 'f' */ "uchsia\0#ffff0000ffff", + /* 'g' */ "reen\0#000080800000", + /* 'h' */ NULL, + /* 'i' */ "ndigo\0#4b4b00008282", + /* 'j' */ NULL, + /* 'k' */ NULL, + /* 'l' */ "ime\0#0000ffff0000", + /* 'm' */ "aroon\0#808000000000", + /* 'n' */ NULL, + /* 'o' */ "live\0#808080800000", + /* 'p' */ "urple\0#808000008080", + /* 'q' */ NULL, + /* 'r' */ NULL, + /* 's' */ "ilver\0#c0c0c0c0c0c0", + /* 't' */ "eal\0#000080808080" +}; + +Status +TkParseColor( + Display *display, /* The display */ + Colormap map, /* Color map */ + const char *name, /* String to be parsed */ + XColor *color) +{ + char buf[14]; + if (*name == '#') { + buf[0] = '#'; buf[13] = '\0'; + if (!*(++name) || !*(++name) || !*(++name)) { + /* Not at least 3 hex digits, so invalid */ + return 0; + } else if (!*(++name)) { + /* Exactly 3 hex digits */ + buf[9] = buf[10] = buf[11] = buf[12] = *(--name); + buf[5] = buf[6] = buf[7] = buf[8] = *(--name); + buf[1] = buf[2] = buf[3] = buf[4] = *(--name); + name = buf; + } else if (!*(++name) || !*(++name)) { + /* Not at least 6 hex digits, so invalid */ + return 0; + } else if (!*(++name)) { + /* Exactly 6 hex digits */ + buf[10] = buf[12] = *(--name); + buf[9] = buf[11] = *(--name); + buf[6] = buf[8] = *(--name); + buf[5] = buf[7] = *(--name); + buf[2] = buf[4] = *(--name); + buf[1] = buf[3] = *(--name); + name = buf; + } else if (!*(++name) || !*(++name)) { + /* Not at least 9 hex digits, so invalid */ + return 0; + } else if (!*(++name)) { + /* Exactly 9 hex digits */ + buf[11] = *(--name); + buf[10] = *(--name); + buf[9] = buf[12] = *(--name); + buf[7] = *(--name); + buf[6] = *(--name); + buf[5] = buf[8] = *(--name); + buf[3] = *(--name); + buf[2] = *(--name); + buf[1] = buf[4] = *(--name); + name = buf; + } else if (!*(++name) || !*(++name) || *(++name)) { + /* Not exactly 12 hex digits, so invalid */ + return 0; + } else { + name -= 13; + } + goto done; + } 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 = tkWebColors[((*name - 'A') & 0x1f)]; + if (p) { + const char *q = name; + while (!((*p - *(++q)) & 0xdf)) { + if (!*p++) { + name = p; + goto done; + } + } + } + } + } + if (strlen(name) > 99) { + return 0; + } +done: + return XParseColor(display, map, name, color); +} +#endif /* __WIN32__ */ /* * Local Variables: * mode: c |