11ab64890Smrg/*
21ab64890Smrg
31ab64890SmrgCopyright 1985, 1998  The Open Group
41ab64890Smrg
51ab64890SmrgPermission to use, copy, modify, distribute, and sell this software and its
61ab64890Smrgdocumentation for any purpose is hereby granted without fee, provided that
71ab64890Smrgthe above copyright notice appear in all copies and that both that
81ab64890Smrgcopyright notice and this permission notice appear in supporting
91ab64890Smrgdocumentation.
101ab64890Smrg
111ab64890SmrgThe above copyright notice and this permission notice shall be included in
121ab64890Smrgall copies or substantial portions of the Software.
131ab64890Smrg
141ab64890SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
151ab64890SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
161ab64890SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
171ab64890SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
181ab64890SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
191ab64890SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
201ab64890Smrg
211ab64890SmrgExcept as contained in this notice, the name of The Open Group shall not be
221ab64890Smrgused in advertising or otherwise to promote the sale, use or other dealings
231ab64890Smrgin this Software without prior written authorization from The Open Group.
241ab64890Smrg
251ab64890Smrg*/
261ab64890Smrg
271ab64890Smrg#ifdef HAVE_CONFIG_H
281ab64890Smrg#include <config.h>
291ab64890Smrg#endif
309c927599Smrg#include <limits.h>
311ab64890Smrg#include <stdio.h>
321ab64890Smrg#include "Xlibint.h"
331ab64890Smrg#include "Xcmsint.h"
341ab64890Smrg
351ab64890Smrg
361ab64890SmrgStatus
371ab64890SmrgXParseColor (
381ab64890Smrg	register Display *dpy,
391ab64890Smrg        Colormap cmap,
401ab64890Smrg	_Xconst char *spec,
411ab64890Smrg	XColor *def)
421ab64890Smrg{
4303ea5dc6Smrg	register size_t n, i;
441ab64890Smrg	int r, g, b;
451ab64890Smrg	char c;
461ab64890Smrg	XcmsCCC ccc;
471ab64890Smrg	XcmsColor cmsColor;
481ab64890Smrg
491ab64890Smrg        if (!spec) return(0);
5003ea5dc6Smrg	n = strlen (spec);
519c927599Smrg	if (n >= USHRT_MAX)
529c927599Smrg            return(0);
531ab64890Smrg	if (*spec == '#') {
541ab64890Smrg	    /*
551ab64890Smrg	     * RGB
561ab64890Smrg	     */
571ab64890Smrg	    spec++;
581ab64890Smrg	    n--;
591ab64890Smrg	    if (n != 3 && n != 6 && n != 9 && n != 12)
601ab64890Smrg		return (0);
611ab64890Smrg	    n /= 3;
621ab64890Smrg	    g = b = 0;
631ab64890Smrg	    do {
641ab64890Smrg		r = g;
651ab64890Smrg		g = b;
661ab64890Smrg		b = 0;
6703ea5dc6Smrg		for (i = 0; i < n; i++) {
681ab64890Smrg		    c = *spec++;
691ab64890Smrg		    b <<= 4;
701ab64890Smrg		    if (c >= '0' && c <= '9')
711ab64890Smrg			b |= c - '0';
721ab64890Smrg		    else if (c >= 'A' && c <= 'F')
731ab64890Smrg			b |= c - ('A' - 10);
741ab64890Smrg		    else if (c >= 'a' && c <= 'f')
751ab64890Smrg			b |= c - ('a' - 10);
761ab64890Smrg		    else return (0);
771ab64890Smrg		}
781ab64890Smrg	    } while (*spec != '\0');
791ab64890Smrg	    n <<= 2;
801ab64890Smrg	    n = 16 - n;
811ab64890Smrg	    def->red = r << n;
821ab64890Smrg	    def->green = g << n;
831ab64890Smrg	    def->blue = b << n;
841ab64890Smrg	    def->flags = DoRed | DoGreen | DoBlue;
851ab64890Smrg	    return (1);
861ab64890Smrg	}
871ab64890Smrg
881ab64890Smrg
891ab64890Smrg#ifdef XCMS
901ab64890Smrg	/*
911ab64890Smrg	 * Let's Attempt to use Xcms and i18n approach to Parse Color
921ab64890Smrg	 */
931ab64890Smrg	if ((ccc = XcmsCCCOfColormap(dpy, cmap)) != (XcmsCCC)NULL) {
941ab64890Smrg	    const char *tmpName = spec;
951ab64890Smrg
961ab64890Smrg	    switch (_XcmsResolveColorString(ccc, &tmpName, &cmsColor,
971ab64890Smrg					    XcmsRGBFormat)) {
981ab64890Smrg	    case XcmsSuccess:
991ab64890Smrg	    case XcmsSuccessWithCompression:
1001ab64890Smrg		cmsColor.pixel = def->pixel;
1011ab64890Smrg		_XcmsRGB_to_XColor(&cmsColor, def, 1);
1021ab64890Smrg		return(1);
1031ab64890Smrg	    case XcmsFailure:
1041ab64890Smrg	    case _XCMS_NEWNAME:
1051ab64890Smrg		/*
1061ab64890Smrg		 * if the result was _XCMS_NEWNAME tmpName points to
1071ab64890Smrg		 * a string in cmsColNm.c:pairs table, for example,
1081ab64890Smrg		 * gray70 would become tekhvc:0.0/70.0/0.0
1091ab64890Smrg		 */
1101ab64890Smrg		break;
1111ab64890Smrg	    }
1121ab64890Smrg	}
1131ab64890Smrg#endif
1141ab64890Smrg
1151ab64890Smrg	/*
1161ab64890Smrg	 * Xcms and i18n methods failed, so lets pass it to the server
1171ab64890Smrg	 * for parsing.
1181ab64890Smrg	 */
1191ab64890Smrg	{
1201ab64890Smrg	    xLookupColorReply reply;
1211ab64890Smrg	    register xLookupColorReq *req;
1221ab64890Smrg	    LockDisplay(dpy);
1231ab64890Smrg	    GetReq (LookupColor, req);
1241ab64890Smrg	    req->cmap = cmap;
12503ea5dc6Smrg	    req->nbytes = (CARD16) (n = strlen(spec));
1261ab64890Smrg	    req->length += (n + 3) >> 2;
1271ab64890Smrg	    Data (dpy, spec, (long)n);
1281ab64890Smrg	    if (!_XReply (dpy, (xReply *) &reply, 0, xTrue)) {
1291ab64890Smrg		UnlockDisplay(dpy);
1301ab64890Smrg		SyncHandle();
1311ab64890Smrg		return (0);
1321ab64890Smrg		}
1331ab64890Smrg	    def->red = reply.exactRed;
1341ab64890Smrg	    def->green = reply.exactGreen;
1351ab64890Smrg	    def->blue = reply.exactBlue;
1361ab64890Smrg	    def->flags = DoRed | DoGreen | DoBlue;
1371ab64890Smrg	    UnlockDisplay(dpy);
1381ab64890Smrg	    SyncHandle();
1391ab64890Smrg	    return (1);
1401ab64890Smrg	}
1411ab64890Smrg}
142