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