LookupCol.c revision 03ea5dc6
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
37XLookupColor (
38	register Display *dpy,
39        Colormap cmap,
40	_Xconst char *spec,
41	XColor *def,
42	XColor *scr)
43{
44	register size_t n;
45	xLookupColorReply reply;
46	register xLookupColorReq *req;
47	XcmsCCC ccc;
48	XcmsColor cmsColor_exact;
49
50	if (spec == NULL)
51		return 0;
52	n = strlen (spec);
53	if (n >= USHRT_MAX)
54            return 0;
55#ifdef XCMS
56	/*
57	 * Let's Attempt to use Xcms and i18n approach to Parse Color
58	 */
59	if ((ccc = XcmsCCCOfColormap(dpy, cmap)) != (XcmsCCC)NULL) {
60	    const char *tmpName = spec;
61
62	    switch (_XcmsResolveColorString(ccc, &tmpName, &cmsColor_exact,
63					    XcmsRGBFormat)) {
64	    case XcmsSuccess:
65	    case XcmsSuccessWithCompression:
66		_XcmsRGB_to_XColor(&cmsColor_exact, def, 1);
67		memcpy((char *)scr, (char *)def, sizeof(XColor));
68		_XUnresolveColor(ccc, scr);
69		return(1);
70	    case XcmsFailure:
71	    case _XCMS_NEWNAME:
72		/*
73		 * if the result was _XCMS_NEWNAME tmpName points to
74		 * a string in cmsColNm.c:pairs table, for example,
75		 * gray70 would become tekhvc:0.0/70.0/0.0
76		 */
77		break;
78	    }
79	}
80#endif
81
82	/*
83	 * Xcms and i18n methods failed, so lets pass it to the server
84	 * for parsing.
85	 */
86	LockDisplay(dpy);
87	GetReq (LookupColor, req);
88	req->cmap = cmap;
89	req->nbytes = n;
90	req->length += (n + 3) >> 2;
91	Data (dpy, spec, (long)n);
92	if (!_XReply (dpy, (xReply *) &reply, 0, xTrue)) {
93	    UnlockDisplay(dpy);
94	    SyncHandle();
95	    return (0);
96	    }
97	def->red   = reply.exactRed;
98	def->green = reply.exactGreen;
99	def->blue  = reply.exactBlue;
100
101	scr->red   = reply.screenRed;
102	scr->green = reply.screenGreen;
103	scr->blue  = reply.screenBlue;
104
105	UnlockDisplay(dpy);
106	SyncHandle();
107	return (1);
108}
109