LookupCol.c revision 1ab64890
11ab64890Smrg/* $Xorg: LookupCol.c,v 1.4 2001/02/09 02:03:34 xorgcvs Exp $ */
21ab64890Smrg/*
31ab64890Smrg
41ab64890SmrgCopyright 1985, 1998  The Open Group
51ab64890Smrg
61ab64890SmrgPermission to use, copy, modify, distribute, and sell this software and its
71ab64890Smrgdocumentation for any purpose is hereby granted without fee, provided that
81ab64890Smrgthe above copyright notice appear in all copies and that both that
91ab64890Smrgcopyright notice and this permission notice appear in supporting
101ab64890Smrgdocumentation.
111ab64890Smrg
121ab64890SmrgThe above copyright notice and this permission notice shall be included in
131ab64890Smrgall copies or substantial portions of the Software.
141ab64890Smrg
151ab64890SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
161ab64890SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
171ab64890SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
181ab64890SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
191ab64890SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
201ab64890SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
211ab64890Smrg
221ab64890SmrgExcept as contained in this notice, the name of The Open Group shall not be
231ab64890Smrgused in advertising or otherwise to promote the sale, use or other dealings
241ab64890Smrgin this Software without prior written authorization from The Open Group.
251ab64890Smrg
261ab64890Smrg*/
271ab64890Smrg/* $XFree86: xc/lib/X11/LookupCol.c,v 1.6 2003/04/13 19:22:16 dawes Exp $ */
281ab64890Smrg
291ab64890Smrg#define NEED_REPLIES
301ab64890Smrg#ifdef HAVE_CONFIG_H
311ab64890Smrg#include <config.h>
321ab64890Smrg#endif
331ab64890Smrg#include <stdio.h>
341ab64890Smrg#include "Xlibint.h"
351ab64890Smrg#include "Xcmsint.h"
361ab64890Smrg
371ab64890Smrg
381ab64890SmrgStatus
391ab64890SmrgXLookupColor (
401ab64890Smrg	register Display *dpy,
411ab64890Smrg        Colormap cmap,
421ab64890Smrg	_Xconst char *spec,
431ab64890Smrg	XColor *def,
441ab64890Smrg	XColor *scr)
451ab64890Smrg{
461ab64890Smrg	register int n;
471ab64890Smrg	xLookupColorReply reply;
481ab64890Smrg	register xLookupColorReq *req;
491ab64890Smrg	XcmsCCC ccc;
501ab64890Smrg	XcmsColor cmsColor_exact;
511ab64890Smrg
521ab64890Smrg#ifdef XCMS
531ab64890Smrg	/*
541ab64890Smrg	 * Let's Attempt to use Xcms and i18n approach to Parse Color
551ab64890Smrg	 */
561ab64890Smrg	if ((ccc = XcmsCCCOfColormap(dpy, cmap)) != (XcmsCCC)NULL) {
571ab64890Smrg	    const char *tmpName = spec;
581ab64890Smrg
591ab64890Smrg	    switch (_XcmsResolveColorString(ccc, &tmpName, &cmsColor_exact,
601ab64890Smrg					    XcmsRGBFormat)) {
611ab64890Smrg	    case XcmsSuccess:
621ab64890Smrg	    case XcmsSuccessWithCompression:
631ab64890Smrg		_XcmsRGB_to_XColor(&cmsColor_exact, def, 1);
641ab64890Smrg		memcpy((char *)scr, (char *)def, sizeof(XColor));
651ab64890Smrg		_XUnresolveColor(ccc, scr);
661ab64890Smrg		return(1);
671ab64890Smrg	    case XcmsFailure:
681ab64890Smrg	    case _XCMS_NEWNAME:
691ab64890Smrg		/*
701ab64890Smrg		 * if the result was _XCMS_NEWNAME tmpName points to
711ab64890Smrg		 * a string in cmsColNm.c:pairs table, for example,
721ab64890Smrg		 * gray70 would become tekhvc:0.0/70.0/0.0
731ab64890Smrg		 */
741ab64890Smrg		break;
751ab64890Smrg	    }
761ab64890Smrg	}
771ab64890Smrg#endif
781ab64890Smrg
791ab64890Smrg	/*
801ab64890Smrg	 * Xcms and i18n methods failed, so lets pass it to the server
811ab64890Smrg	 * for parsing.
821ab64890Smrg	 */
831ab64890Smrg
841ab64890Smrg	n = strlen (spec);
851ab64890Smrg	LockDisplay(dpy);
861ab64890Smrg	GetReq (LookupColor, req);
871ab64890Smrg	req->cmap = cmap;
881ab64890Smrg	req->nbytes = n;
891ab64890Smrg	req->length += (n + 3) >> 2;
901ab64890Smrg	Data (dpy, spec, (long)n);
911ab64890Smrg	if (!_XReply (dpy, (xReply *) &reply, 0, xTrue)) {
921ab64890Smrg	    UnlockDisplay(dpy);
931ab64890Smrg	    SyncHandle();
941ab64890Smrg	    return (0);
951ab64890Smrg	    }
961ab64890Smrg	def->red   = reply.exactRed;
971ab64890Smrg	def->green = reply.exactGreen;
981ab64890Smrg	def->blue  = reply.exactBlue;
991ab64890Smrg
1001ab64890Smrg	scr->red   = reply.screenRed;
1011ab64890Smrg	scr->green = reply.screenGreen;
1021ab64890Smrg	scr->blue  = reply.screenBlue;
1031ab64890Smrg
1041ab64890Smrg	UnlockDisplay(dpy);
1051ab64890Smrg	SyncHandle();
1061ab64890Smrg	return (1);
1071ab64890Smrg}
108