ParseCol.c revision 9c019ec5
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
301ab64890Smrg#include <stdio.h>
311ab64890Smrg#include "Xlibint.h"
321ab64890Smrg#include "Xcmsint.h"
331ab64890Smrg
341ab64890Smrg
351ab64890SmrgStatus
361ab64890SmrgXParseColor (
371ab64890Smrg	register Display *dpy,
381ab64890Smrg        Colormap cmap,
391ab64890Smrg	_Xconst char *spec,
401ab64890Smrg	XColor *def)
411ab64890Smrg{
421ab64890Smrg	register int n, i;
431ab64890Smrg	int r, g, b;
441ab64890Smrg	char c;
451ab64890Smrg	XcmsCCC ccc;
461ab64890Smrg	XcmsColor cmsColor;
471ab64890Smrg
481ab64890Smrg        if (!spec) return(0);
499c019ec5Smaya	n = (int) strlen (spec);
501ab64890Smrg	if (*spec == '#') {
511ab64890Smrg	    /*
521ab64890Smrg	     * RGB
531ab64890Smrg	     */
541ab64890Smrg	    spec++;
551ab64890Smrg	    n--;
561ab64890Smrg	    if (n != 3 && n != 6 && n != 9 && n != 12)
571ab64890Smrg		return (0);
581ab64890Smrg	    n /= 3;
591ab64890Smrg	    g = b = 0;
601ab64890Smrg	    do {
611ab64890Smrg		r = g;
621ab64890Smrg		g = b;
631ab64890Smrg		b = 0;
641ab64890Smrg		for (i = n; --i >= 0; ) {
651ab64890Smrg		    c = *spec++;
661ab64890Smrg		    b <<= 4;
671ab64890Smrg		    if (c >= '0' && c <= '9')
681ab64890Smrg			b |= c - '0';
691ab64890Smrg		    else if (c >= 'A' && c <= 'F')
701ab64890Smrg			b |= c - ('A' - 10);
711ab64890Smrg		    else if (c >= 'a' && c <= 'f')
721ab64890Smrg			b |= c - ('a' - 10);
731ab64890Smrg		    else return (0);
741ab64890Smrg		}
751ab64890Smrg	    } while (*spec != '\0');
761ab64890Smrg	    n <<= 2;
771ab64890Smrg	    n = 16 - n;
781ab64890Smrg	    def->red = r << n;
791ab64890Smrg	    def->green = g << n;
801ab64890Smrg	    def->blue = b << n;
811ab64890Smrg	    def->flags = DoRed | DoGreen | DoBlue;
821ab64890Smrg	    return (1);
831ab64890Smrg	}
841ab64890Smrg
851ab64890Smrg
861ab64890Smrg#ifdef XCMS
871ab64890Smrg	/*
881ab64890Smrg	 * Let's Attempt to use Xcms and i18n approach to Parse Color
891ab64890Smrg	 */
901ab64890Smrg	if ((ccc = XcmsCCCOfColormap(dpy, cmap)) != (XcmsCCC)NULL) {
911ab64890Smrg	    const char *tmpName = spec;
921ab64890Smrg
931ab64890Smrg	    switch (_XcmsResolveColorString(ccc, &tmpName, &cmsColor,
941ab64890Smrg					    XcmsRGBFormat)) {
951ab64890Smrg	    case XcmsSuccess:
961ab64890Smrg	    case XcmsSuccessWithCompression:
971ab64890Smrg		cmsColor.pixel = def->pixel;
981ab64890Smrg		_XcmsRGB_to_XColor(&cmsColor, def, 1);
991ab64890Smrg		return(1);
1001ab64890Smrg	    case XcmsFailure:
1011ab64890Smrg	    case _XCMS_NEWNAME:
1021ab64890Smrg		/*
1031ab64890Smrg		 * if the result was _XCMS_NEWNAME tmpName points to
1041ab64890Smrg		 * a string in cmsColNm.c:pairs table, for example,
1051ab64890Smrg		 * gray70 would become tekhvc:0.0/70.0/0.0
1061ab64890Smrg		 */
1071ab64890Smrg		break;
1081ab64890Smrg	    }
1091ab64890Smrg	}
1101ab64890Smrg#endif
1111ab64890Smrg
1121ab64890Smrg	/*
1131ab64890Smrg	 * Xcms and i18n methods failed, so lets pass it to the server
1141ab64890Smrg	 * for parsing.
1151ab64890Smrg	 */
1161ab64890Smrg	{
1171ab64890Smrg	    xLookupColorReply reply;
1181ab64890Smrg	    register xLookupColorReq *req;
1191ab64890Smrg	    LockDisplay(dpy);
1201ab64890Smrg	    GetReq (LookupColor, req);
1211ab64890Smrg	    req->cmap = cmap;
1229c019ec5Smaya	    req->nbytes = (CARD16) (n = (int) strlen(spec));
1231ab64890Smrg	    req->length += (n + 3) >> 2;
1241ab64890Smrg	    Data (dpy, spec, (long)n);
1251ab64890Smrg	    if (!_XReply (dpy, (xReply *) &reply, 0, xTrue)) {
1261ab64890Smrg		UnlockDisplay(dpy);
1271ab64890Smrg		SyncHandle();
1281ab64890Smrg		return (0);
1291ab64890Smrg		}
1301ab64890Smrg	    def->red = reply.exactRed;
1311ab64890Smrg	    def->green = reply.exactGreen;
1321ab64890Smrg	    def->blue = reply.exactBlue;
1331ab64890Smrg	    def->flags = DoRed | DoGreen | DoBlue;
1341ab64890Smrg	    UnlockDisplay(dpy);
1351ab64890Smrg	    SyncHandle();
1361ab64890Smrg	    return (1);
1371ab64890Smrg	}
1381ab64890Smrg}
139