1/* 2 3Copyright 1985, 1998 The Open Group 4 5Permission to use, copy, modify, distribute, and sell this software and its 6documentation for any purpose is hereby granted without fee, provided that 7the above copyright notice appear in all copies and that both that 8copyright notice and this permission notice appear in supporting 9documentation. 10 11The above copyright notice and this permission notice shall be included in 12all copies or substantial portions of the Software. 13 14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 18AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 19CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 21Except as contained in this notice, the name of The Open Group shall not be 22used in advertising or otherwise to promote the sale, use or other dealings 23in this Software without prior written authorization from The Open Group. 24 25*/ 26 27#ifdef HAVE_CONFIG_H 28#include <config.h> 29#endif 30#include <limits.h> 31#include <stdio.h> 32#include "Xlibint.h" 33#include "Xcmsint.h" 34 35 36Status 37XParseColor ( 38 register Display *dpy, 39 Colormap cmap, 40 _Xconst char *spec, 41 XColor *def) 42{ 43 register size_t n, i; 44 int r, g, b; 45 char c; 46 XcmsCCC ccc; 47 XcmsColor cmsColor; 48 49 if (!spec) return(0); 50 n = strlen (spec); 51 if (n >= USHRT_MAX) 52 return(0); 53 if (*spec == '#') { 54 /* 55 * RGB 56 */ 57 spec++; 58 n--; 59 if (n != 3 && n != 6 && n != 9 && n != 12) 60 return (0); 61 n /= 3; 62 g = b = 0; 63 do { 64 r = g; 65 g = b; 66 b = 0; 67 for (i = 0; i < n; i++) { 68 c = *spec++; 69 b <<= 4; 70 if (c >= '0' && c <= '9') 71 b |= c - '0'; 72 else if (c >= 'A' && c <= 'F') 73 b |= c - ('A' - 10); 74 else if (c >= 'a' && c <= 'f') 75 b |= c - ('a' - 10); 76 else return (0); 77 } 78 } while (*spec != '\0'); 79 n <<= 2; 80 n = 16 - n; 81 def->red = r << n; 82 def->green = g << n; 83 def->blue = b << n; 84 def->flags = DoRed | DoGreen | DoBlue; 85 return (1); 86 } 87 88 89#ifdef XCMS 90 /* 91 * Let's Attempt to use Xcms and i18n approach to Parse Color 92 */ 93 if ((ccc = XcmsCCCOfColormap(dpy, cmap)) != (XcmsCCC)NULL) { 94 const char *tmpName = spec; 95 96 switch (_XcmsResolveColorString(ccc, &tmpName, &cmsColor, 97 XcmsRGBFormat)) { 98 case XcmsSuccess: 99 case XcmsSuccessWithCompression: 100 cmsColor.pixel = def->pixel; 101 _XcmsRGB_to_XColor(&cmsColor, def, 1); 102 return(1); 103 case XcmsFailure: 104 case _XCMS_NEWNAME: 105 /* 106 * if the result was _XCMS_NEWNAME tmpName points to 107 * a string in cmsColNm.c:pairs table, for example, 108 * gray70 would become tekhvc:0.0/70.0/0.0 109 */ 110 break; 111 } 112 } 113#endif 114 115 /* 116 * Xcms and i18n methods failed, so lets pass it to the server 117 * for parsing. 118 */ 119 { 120 xLookupColorReply reply; 121 register xLookupColorReq *req; 122 LockDisplay(dpy); 123 GetReq (LookupColor, req); 124 req->cmap = cmap; 125 req->nbytes = (CARD16) (n = strlen(spec)); 126 req->length += (n + 3) >> 2; 127 Data (dpy, spec, (long)n); 128 if (!_XReply (dpy, (xReply *) &reply, 0, xTrue)) { 129 UnlockDisplay(dpy); 130 SyncHandle(); 131 return (0); 132 } 133 def->red = reply.exactRed; 134 def->green = reply.exactGreen; 135 def->blue = reply.exactBlue; 136 def->flags = DoRed | DoGreen | DoBlue; 137 UnlockDisplay(dpy); 138 SyncHandle(); 139 return (1); 140 } 141} 142