cmsAllNCol.c revision 1ab64890
11ab64890Smrg/* $Xorg: cmsAllNCol.c,v 1.3 2000/08/17 19:45:09 cpqbld Exp $ */
21ab64890Smrg
31ab64890Smrg/*
41ab64890Smrg * Code and supporting documentation (c) Copyright 1990 1991 Tektronix, Inc.
51ab64890Smrg * 	All Rights Reserved
61ab64890Smrg *
71ab64890Smrg * This file is a component of an X Window System-specific implementation
81ab64890Smrg * of Xcms based on the TekColor Color Management System.  Permission is
91ab64890Smrg * hereby granted to use, copy, modify, sell, and otherwise distribute this
101ab64890Smrg * software and its documentation for any purpose and without fee, provided
111ab64890Smrg * that this copyright, permission, and disclaimer notice is reproduced in
121ab64890Smrg * all copies of this software and in supporting documentation.  TekColor
131ab64890Smrg * is a trademark of Tektronix, Inc.
141ab64890Smrg *
151ab64890Smrg * Tektronix makes no representation about the suitability of this software
161ab64890Smrg * for any purpose.  It is provided "as is" and with all faults.
171ab64890Smrg *
181ab64890Smrg * TEKTRONIX DISCLAIMS ALL WARRANTIES APPLICABLE TO THIS SOFTWARE,
191ab64890Smrg * INCLUDING THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
201ab64890Smrg * PARTICULAR PURPOSE.  IN NO EVENT SHALL TEKTRONIX BE LIABLE FOR ANY
211ab64890Smrg * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
221ab64890Smrg * RESULTING FROM LOSS OF USE, DATA, OR PROFITS, WHETHER IN AN ACTION OF
231ab64890Smrg * CONTRACT, NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
241ab64890Smrg * CONNECTION WITH THE USE OR THE PERFORMANCE OF THIS SOFTWARE.
251ab64890Smrg *
261ab64890Smrg *
271ab64890Smrg *	NAME
281ab64890Smrg *		XcmsAlNCol.c
291ab64890Smrg *
301ab64890Smrg *	DESCRIPTION
311ab64890Smrg *		Source for XcmsAllocNamedColor
321ab64890Smrg *
331ab64890Smrg *
341ab64890Smrg */
351ab64890Smrg/* $XFree86: xc/lib/X11/cmsAllNCol.c,v 1.4 2003/04/13 19:22:20 dawes Exp $ */
361ab64890Smrg
371ab64890Smrg#define NEED_REPLIES
381ab64890Smrg#ifdef HAVE_CONFIG_H
391ab64890Smrg#include <config.h>
401ab64890Smrg#endif
411ab64890Smrg#include <stdio.h>
421ab64890Smrg#include "Xlibint.h"
431ab64890Smrg#include "Xcmsint.h"
441ab64890Smrg#include "Cv.h"
451ab64890Smrg
461ab64890Smrg
471ab64890Smrg/*
481ab64890Smrg *	NAME
491ab64890Smrg *		XcmsAllocNamedColor -
501ab64890Smrg *
511ab64890Smrg *	SYNOPSIS
521ab64890Smrg */
531ab64890SmrgStatus
541ab64890SmrgXcmsAllocNamedColor (
551ab64890Smrg    Display *dpy,
561ab64890Smrg    Colormap cmap,
571ab64890Smrg    _Xconst char *colorname,
581ab64890Smrg    XcmsColor *pColor_scrn_return,
591ab64890Smrg    XcmsColor *pColor_exact_return,
601ab64890Smrg    XcmsColorFormat result_format)
611ab64890Smrg/*
621ab64890Smrg *	DESCRIPTION
631ab64890Smrg *		Finds the color specification associated with the color
641ab64890Smrg *		name in the Device-Independent Color Name Database, then
651ab64890Smrg *		converts that color specification to an RGB format.  This
661ab64890Smrg *		RGB value is then used in a call to XAllocColor to allocate
671ab64890Smrg *		a read-only color cell.
681ab64890Smrg *
691ab64890Smrg *	RETURNS
701ab64890Smrg *		0 if failed to parse string or find any entry in the database.
711ab64890Smrg *		1 if succeeded in converting color name to XcmsColor.
721ab64890Smrg *		2 if succeeded in converting color name to another color name.
731ab64890Smrg *
741ab64890Smrg */
751ab64890Smrg{
761ab64890Smrg    long nbytes;
771ab64890Smrg    xAllocNamedColorReply rep;
781ab64890Smrg    xAllocNamedColorReq *req;
791ab64890Smrg    XColor hard_def;
801ab64890Smrg    XColor exact_def;
811ab64890Smrg    Status retval1 = 1;
821ab64890Smrg    Status retval2 = XcmsSuccess;
831ab64890Smrg    XcmsColor tmpColor;
841ab64890Smrg    XColor XColor_in_out;
851ab64890Smrg    XcmsCCC ccc;
861ab64890Smrg
871ab64890Smrg    /*
881ab64890Smrg     * 0. Check for invalid arguments.
891ab64890Smrg     */
901ab64890Smrg    if (dpy == NULL || colorname[0] == '\0' || pColor_scrn_return == 0
911ab64890Smrg	    || pColor_exact_return == NULL) {
921ab64890Smrg	return(XcmsFailure);
931ab64890Smrg    }
941ab64890Smrg
951ab64890Smrg    if ((ccc = XcmsCCCOfColormap(dpy, cmap)) == (XcmsCCC)NULL) {
961ab64890Smrg	return(XcmsFailure);
971ab64890Smrg    }
981ab64890Smrg
991ab64890Smrg    /*
1001ab64890Smrg     * 1. Convert string to a XcmsColor using Xcms and i18n mechanism
1011ab64890Smrg     */
1021ab64890Smrg    if ((retval1 = _XcmsResolveColorString(ccc, &colorname,
1031ab64890Smrg	    &tmpColor, result_format)) == XcmsFailure) {
1041ab64890Smrg	return(XcmsFailure);
1051ab64890Smrg    }
1061ab64890Smrg    if (retval1 == _XCMS_NEWNAME) {
1071ab64890Smrg	goto PassToServer;
1081ab64890Smrg    }
1091ab64890Smrg    memcpy((char *)pColor_exact_return, (char *)&tmpColor, sizeof(XcmsColor));
1101ab64890Smrg
1111ab64890Smrg    /*
1121ab64890Smrg     * 2. Convert tmpColor to RGB
1131ab64890Smrg     *	Assume pColor_exact_return is now adjusted to Client White Point
1141ab64890Smrg     */
1151ab64890Smrg    if ((retval2 = XcmsConvertColors(ccc, &tmpColor,
1161ab64890Smrg	    1, XcmsRGBFormat, (Bool *) NULL)) == XcmsFailure) {
1171ab64890Smrg	return(XcmsFailure);
1181ab64890Smrg    }
1191ab64890Smrg
1201ab64890Smrg    /*
1211ab64890Smrg     * 3. Convert to XColor and call XAllocColor
1221ab64890Smrg     */
1231ab64890Smrg    _XcmsRGB_to_XColor(&tmpColor, &XColor_in_out, 1);
1241ab64890Smrg    if (XAllocColor(ccc->dpy, cmap, &XColor_in_out) == 0) {
1251ab64890Smrg	return(XcmsFailure);
1261ab64890Smrg    }
1271ab64890Smrg
1281ab64890Smrg    /*
1291ab64890Smrg     * 4. pColor_scrn_return
1301ab64890Smrg     *
1311ab64890Smrg     * Now convert to the target format.
1321ab64890Smrg     *    We can ignore the return value because we're already in a
1331ab64890Smrg     *    device-dependent format.
1341ab64890Smrg     */
1351ab64890Smrg    _XColor_to_XcmsRGB(ccc, &XColor_in_out, pColor_scrn_return, 1);
1361ab64890Smrg    if (result_format != XcmsRGBFormat) {
1371ab64890Smrg	if (result_format == XcmsUndefinedFormat) {
1381ab64890Smrg	    result_format = pColor_exact_return->format;
1391ab64890Smrg	}
1401ab64890Smrg	if (XcmsConvertColors(ccc, pColor_scrn_return, 1, result_format,
1411ab64890Smrg		(Bool *) NULL) == XcmsFailure) {
1421ab64890Smrg	    return(XcmsFailure);
1431ab64890Smrg	}
1441ab64890Smrg    }
1451ab64890Smrg
1461ab64890Smrg    return(retval1 > retval2 ? retval1 : retval2);
1471ab64890Smrg
1481ab64890SmrgPassToServer:
1491ab64890Smrg    /*
1501ab64890Smrg     * All previous methods failed, so lets pass it to the server
1511ab64890Smrg     * for parsing.
1521ab64890Smrg     */
1531ab64890Smrg    dpy = ccc->dpy;
1541ab64890Smrg    LockDisplay(dpy);
1551ab64890Smrg    GetReq(AllocNamedColor, req);
1561ab64890Smrg
1571ab64890Smrg    req->cmap = cmap;
1581ab64890Smrg    nbytes = req->nbytes = strlen(colorname);
1591ab64890Smrg    req->length += (nbytes + 3) >> 2; /* round up to mult of 4 */
1601ab64890Smrg
1611ab64890Smrg    _XSend(dpy, colorname, nbytes);
1621ab64890Smrg       /* _XSend is more efficient that Data, since _XReply follows */
1631ab64890Smrg
1641ab64890Smrg    if (!_XReply (dpy, (xReply *) &rep, 0, xTrue)) {
1651ab64890Smrg	UnlockDisplay(dpy);
1661ab64890Smrg        SyncHandle();
1671ab64890Smrg        return (0);
1681ab64890Smrg    }
1691ab64890Smrg
1701ab64890Smrg    exact_def.red = rep.exactRed;
1711ab64890Smrg    exact_def.green = rep.exactGreen;
1721ab64890Smrg    exact_def.blue = rep.exactBlue;
1731ab64890Smrg
1741ab64890Smrg    hard_def.red = rep.screenRed;
1751ab64890Smrg    hard_def.green = rep.screenGreen;
1761ab64890Smrg    hard_def.blue = rep.screenBlue;
1771ab64890Smrg
1781ab64890Smrg    exact_def.pixel = hard_def.pixel = rep.pixel;
1791ab64890Smrg
1801ab64890Smrg    UnlockDisplay(dpy);
1811ab64890Smrg    SyncHandle();
1821ab64890Smrg
1831ab64890Smrg    /*
1841ab64890Smrg     * Now convert to the target format.
1851ab64890Smrg     */
1861ab64890Smrg    _XColor_to_XcmsRGB(ccc, &exact_def, pColor_exact_return, 1);
1871ab64890Smrg    _XColor_to_XcmsRGB(ccc, &hard_def, pColor_scrn_return, 1);
1881ab64890Smrg    if (result_format != XcmsRGBFormat
1891ab64890Smrg	    && result_format != XcmsUndefinedFormat) {
1901ab64890Smrg	if (XcmsConvertColors(ccc, pColor_exact_return, 1, result_format,
1911ab64890Smrg		(Bool *) NULL) == XcmsFailure) {
1921ab64890Smrg	    return(XcmsFailure);
1931ab64890Smrg	}
1941ab64890Smrg	if (XcmsConvertColors(ccc, pColor_scrn_return, 1, result_format,
1951ab64890Smrg		(Bool *) NULL) == XcmsFailure) {
1961ab64890Smrg	    return(XcmsFailure);
1971ab64890Smrg	}
1981ab64890Smrg    }
1991ab64890Smrg
2001ab64890Smrg    return(XcmsSuccess);
2011ab64890Smrg}
202