ParseCol.c revision 9c019ec5
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 <stdio.h> 31#include "Xlibint.h" 32#include "Xcmsint.h" 33 34 35Status 36XParseColor ( 37 register Display *dpy, 38 Colormap cmap, 39 _Xconst char *spec, 40 XColor *def) 41{ 42 register int n, i; 43 int r, g, b; 44 char c; 45 XcmsCCC ccc; 46 XcmsColor cmsColor; 47 48 if (!spec) return(0); 49 n = (int) strlen (spec); 50 if (*spec == '#') { 51 /* 52 * RGB 53 */ 54 spec++; 55 n--; 56 if (n != 3 && n != 6 && n != 9 && n != 12) 57 return (0); 58 n /= 3; 59 g = b = 0; 60 do { 61 r = g; 62 g = b; 63 b = 0; 64 for (i = n; --i >= 0; ) { 65 c = *spec++; 66 b <<= 4; 67 if (c >= '0' && c <= '9') 68 b |= c - '0'; 69 else if (c >= 'A' && c <= 'F') 70 b |= c - ('A' - 10); 71 else if (c >= 'a' && c <= 'f') 72 b |= c - ('a' - 10); 73 else return (0); 74 } 75 } while (*spec != '\0'); 76 n <<= 2; 77 n = 16 - n; 78 def->red = r << n; 79 def->green = g << n; 80 def->blue = b << n; 81 def->flags = DoRed | DoGreen | DoBlue; 82 return (1); 83 } 84 85 86#ifdef XCMS 87 /* 88 * Let's Attempt to use Xcms and i18n approach to Parse Color 89 */ 90 if ((ccc = XcmsCCCOfColormap(dpy, cmap)) != (XcmsCCC)NULL) { 91 const char *tmpName = spec; 92 93 switch (_XcmsResolveColorString(ccc, &tmpName, &cmsColor, 94 XcmsRGBFormat)) { 95 case XcmsSuccess: 96 case XcmsSuccessWithCompression: 97 cmsColor.pixel = def->pixel; 98 _XcmsRGB_to_XColor(&cmsColor, def, 1); 99 return(1); 100 case XcmsFailure: 101 case _XCMS_NEWNAME: 102 /* 103 * if the result was _XCMS_NEWNAME tmpName points to 104 * a string in cmsColNm.c:pairs table, for example, 105 * gray70 would become tekhvc:0.0/70.0/0.0 106 */ 107 break; 108 } 109 } 110#endif 111 112 /* 113 * Xcms and i18n methods failed, so lets pass it to the server 114 * for parsing. 115 */ 116 { 117 xLookupColorReply reply; 118 register xLookupColorReq *req; 119 LockDisplay(dpy); 120 GetReq (LookupColor, req); 121 req->cmap = cmap; 122 req->nbytes = (CARD16) (n = (int) strlen(spec)); 123 req->length += (n + 3) >> 2; 124 Data (dpy, spec, (long)n); 125 if (!_XReply (dpy, (xReply *) &reply, 0, xTrue)) { 126 UnlockDisplay(dpy); 127 SyncHandle(); 128 return (0); 129 } 130 def->red = reply.exactRed; 131 def->green = reply.exactGreen; 132 def->blue = reply.exactBlue; 133 def->flags = DoRed | DoGreen | DoBlue; 134 UnlockDisplay(dpy); 135 SyncHandle(); 136 return (1); 137 } 138} 139