cmsAllNCol.c revision 1ab64890
1/* $Xorg: cmsAllNCol.c,v 1.3 2000/08/17 19:45:09 cpqbld Exp $ */
2
3/*
4 * Code and supporting documentation (c) Copyright 1990 1991 Tektronix, Inc.
5 * 	All Rights Reserved
6 *
7 * This file is a component of an X Window System-specific implementation
8 * of Xcms based on the TekColor Color Management System.  Permission is
9 * hereby granted to use, copy, modify, sell, and otherwise distribute this
10 * software and its documentation for any purpose and without fee, provided
11 * that this copyright, permission, and disclaimer notice is reproduced in
12 * all copies of this software and in supporting documentation.  TekColor
13 * is a trademark of Tektronix, Inc.
14 *
15 * Tektronix makes no representation about the suitability of this software
16 * for any purpose.  It is provided "as is" and with all faults.
17 *
18 * TEKTRONIX DISCLAIMS ALL WARRANTIES APPLICABLE TO THIS SOFTWARE,
19 * INCLUDING THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE.  IN NO EVENT SHALL TEKTRONIX BE LIABLE FOR ANY
21 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
22 * RESULTING FROM LOSS OF USE, DATA, OR PROFITS, WHETHER IN AN ACTION OF
23 * CONTRACT, NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
24 * CONNECTION WITH THE USE OR THE PERFORMANCE OF THIS SOFTWARE.
25 *
26 *
27 *	NAME
28 *		XcmsAlNCol.c
29 *
30 *	DESCRIPTION
31 *		Source for XcmsAllocNamedColor
32 *
33 *
34 */
35/* $XFree86: xc/lib/X11/cmsAllNCol.c,v 1.4 2003/04/13 19:22:20 dawes Exp $ */
36
37#define NEED_REPLIES
38#ifdef HAVE_CONFIG_H
39#include <config.h>
40#endif
41#include <stdio.h>
42#include "Xlibint.h"
43#include "Xcmsint.h"
44#include "Cv.h"
45
46
47/*
48 *	NAME
49 *		XcmsAllocNamedColor -
50 *
51 *	SYNOPSIS
52 */
53Status
54XcmsAllocNamedColor (
55    Display *dpy,
56    Colormap cmap,
57    _Xconst char *colorname,
58    XcmsColor *pColor_scrn_return,
59    XcmsColor *pColor_exact_return,
60    XcmsColorFormat result_format)
61/*
62 *	DESCRIPTION
63 *		Finds the color specification associated with the color
64 *		name in the Device-Independent Color Name Database, then
65 *		converts that color specification to an RGB format.  This
66 *		RGB value is then used in a call to XAllocColor to allocate
67 *		a read-only color cell.
68 *
69 *	RETURNS
70 *		0 if failed to parse string or find any entry in the database.
71 *		1 if succeeded in converting color name to XcmsColor.
72 *		2 if succeeded in converting color name to another color name.
73 *
74 */
75{
76    long nbytes;
77    xAllocNamedColorReply rep;
78    xAllocNamedColorReq *req;
79    XColor hard_def;
80    XColor exact_def;
81    Status retval1 = 1;
82    Status retval2 = XcmsSuccess;
83    XcmsColor tmpColor;
84    XColor XColor_in_out;
85    XcmsCCC ccc;
86
87    /*
88     * 0. Check for invalid arguments.
89     */
90    if (dpy == NULL || colorname[0] == '\0' || pColor_scrn_return == 0
91	    || pColor_exact_return == NULL) {
92	return(XcmsFailure);
93    }
94
95    if ((ccc = XcmsCCCOfColormap(dpy, cmap)) == (XcmsCCC)NULL) {
96	return(XcmsFailure);
97    }
98
99    /*
100     * 1. Convert string to a XcmsColor using Xcms and i18n mechanism
101     */
102    if ((retval1 = _XcmsResolveColorString(ccc, &colorname,
103	    &tmpColor, result_format)) == XcmsFailure) {
104	return(XcmsFailure);
105    }
106    if (retval1 == _XCMS_NEWNAME) {
107	goto PassToServer;
108    }
109    memcpy((char *)pColor_exact_return, (char *)&tmpColor, sizeof(XcmsColor));
110
111    /*
112     * 2. Convert tmpColor to RGB
113     *	Assume pColor_exact_return is now adjusted to Client White Point
114     */
115    if ((retval2 = XcmsConvertColors(ccc, &tmpColor,
116	    1, XcmsRGBFormat, (Bool *) NULL)) == XcmsFailure) {
117	return(XcmsFailure);
118    }
119
120    /*
121     * 3. Convert to XColor and call XAllocColor
122     */
123    _XcmsRGB_to_XColor(&tmpColor, &XColor_in_out, 1);
124    if (XAllocColor(ccc->dpy, cmap, &XColor_in_out) == 0) {
125	return(XcmsFailure);
126    }
127
128    /*
129     * 4. pColor_scrn_return
130     *
131     * Now convert to the target format.
132     *    We can ignore the return value because we're already in a
133     *    device-dependent format.
134     */
135    _XColor_to_XcmsRGB(ccc, &XColor_in_out, pColor_scrn_return, 1);
136    if (result_format != XcmsRGBFormat) {
137	if (result_format == XcmsUndefinedFormat) {
138	    result_format = pColor_exact_return->format;
139	}
140	if (XcmsConvertColors(ccc, pColor_scrn_return, 1, result_format,
141		(Bool *) NULL) == XcmsFailure) {
142	    return(XcmsFailure);
143	}
144    }
145
146    return(retval1 > retval2 ? retval1 : retval2);
147
148PassToServer:
149    /*
150     * All previous methods failed, so lets pass it to the server
151     * for parsing.
152     */
153    dpy = ccc->dpy;
154    LockDisplay(dpy);
155    GetReq(AllocNamedColor, req);
156
157    req->cmap = cmap;
158    nbytes = req->nbytes = strlen(colorname);
159    req->length += (nbytes + 3) >> 2; /* round up to mult of 4 */
160
161    _XSend(dpy, colorname, nbytes);
162       /* _XSend is more efficient that Data, since _XReply follows */
163
164    if (!_XReply (dpy, (xReply *) &rep, 0, xTrue)) {
165	UnlockDisplay(dpy);
166        SyncHandle();
167        return (0);
168    }
169
170    exact_def.red = rep.exactRed;
171    exact_def.green = rep.exactGreen;
172    exact_def.blue = rep.exactBlue;
173
174    hard_def.red = rep.screenRed;
175    hard_def.green = rep.screenGreen;
176    hard_def.blue = rep.screenBlue;
177
178    exact_def.pixel = hard_def.pixel = rep.pixel;
179
180    UnlockDisplay(dpy);
181    SyncHandle();
182
183    /*
184     * Now convert to the target format.
185     */
186    _XColor_to_XcmsRGB(ccc, &exact_def, pColor_exact_return, 1);
187    _XColor_to_XcmsRGB(ccc, &hard_def, pColor_scrn_return, 1);
188    if (result_format != XcmsRGBFormat
189	    && result_format != XcmsUndefinedFormat) {
190	if (XcmsConvertColors(ccc, pColor_exact_return, 1, result_format,
191		(Bool *) NULL) == XcmsFailure) {
192	    return(XcmsFailure);
193	}
194	if (XcmsConvertColors(ccc, pColor_scrn_return, 1, result_format,
195		(Bool *) NULL) == XcmsFailure) {
196	    return(XcmsFailure);
197	}
198    }
199
200    return(XcmsSuccess);
201}
202