ParseCol.c revision 1ab64890
1/* $Xorg: ParseCol.c,v 1.4 2001/02/09 02:03:35 xorgcvs Exp $ */ 2/* 3 4Copyright 1985, 1998 The Open Group 5 6Permission to use, copy, modify, distribute, and sell this software and its 7documentation for any purpose is hereby granted without fee, provided that 8the above copyright notice appear in all copies and that both that 9copyright notice and this permission notice appear in supporting 10documentation. 11 12The above copyright notice and this permission notice shall be included in 13all copies or substantial portions of the Software. 14 15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 19AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22Except as contained in this notice, the name of The Open Group shall not be 23used in advertising or otherwise to promote the sale, use or other dealings 24in this Software without prior written authorization from The Open Group. 25 26*/ 27/* $XFree86: xc/lib/X11/ParseCol.c,v 1.6 2003/04/13 19:22:17 dawes Exp $ */ 28 29#define NEED_REPLIES 30#ifdef HAVE_CONFIG_H 31#include <config.h> 32#endif 33#include <stdio.h> 34#include "Xlibint.h" 35#include "Xcmsint.h" 36 37 38Status 39XParseColor ( 40 register Display *dpy, 41 Colormap cmap, 42 _Xconst char *spec, 43 XColor *def) 44{ 45 register int n, i; 46 int r, g, b; 47 char c; 48 XcmsCCC ccc; 49 XcmsColor cmsColor; 50 51 if (!spec) return(0); 52 n = strlen (spec); 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 = n; --i >= 0; ) { 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 = 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