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