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 *		XcmsLkCol.c
28 *
29 *	DESCRIPTION
30 *		Source for XcmsLookupColor
31 *
32 *
33 */
34
35#ifdef HAVE_CONFIG_H
36#include <config.h>
37#endif
38#include <stdio.h>
39#include "Xlibint.h"
40#include "Xcmsint.h"
41#include "Cv.h"
42
43
44/*
45 *	NAME
46 *		XcmsLookupColor -
47 *
48 *	SYNOPSIS
49 */
50Status
51XcmsLookupColor (
52    Display *dpy,
53    Colormap cmap,
54    _Xconst char *colorname,
55    XcmsColor *pColor_exact_return,
56    XcmsColor *pColor_scrn_return,
57    XcmsColorFormat result_format)
58/*
59 *	DESCRIPTION
60 *		The XcmsLookupColor function finds the color specification
61 *		associated with a color name in the Device-Independent Color
62 *		Name Database.
63 *	RETURNS
64 *		This function returns both the color specification found in the
65 *		database (db specification) and the color specification for the
66 *		color displayable by the specified screen (screen
67 *		specification).  The calling routine sets the format for these
68 *		returned specifications in the XcmsColor format component.
69 *		If XcmsUndefinedFormat, the specification is returned in the
70 *		format used to store the color in the database.
71 */
72{
73    Status retval1 = XcmsSuccess;
74    Status retval2 = XcmsSuccess;
75    XcmsCCC ccc;
76    register int n;
77    xLookupColorReply reply;
78    register xLookupColorReq *req;
79    XColor def = {0,};
80    XColor scr = {0,};
81
82/*
83 * 0. Check for invalid arguments.
84 */
85    if (dpy == NULL || colorname[0] == '\0' || pColor_scrn_return == 0
86	    || pColor_exact_return == NULL) {
87	return(XcmsFailure);
88    }
89
90    if ((ccc = XcmsCCCOfColormap(dpy, cmap)) == (XcmsCCC)NULL) {
91	return(XcmsFailure);
92    }
93
94/*
95 * 1. Convert string to a XcmsColor
96 */
97    if ((retval1 = _XcmsResolveColorString(ccc, &colorname,
98	    pColor_exact_return, result_format)) == XcmsFailure) {
99	return(XcmsFailure);
100    }
101    if (retval1 == _XCMS_NEWNAME) {
102	goto PassToServer;
103    }
104
105/*
106 * 2. pColor_scrn_return
107 *	    Assume the pColor_exact_return has already been adjusted to
108 *	    the Client White Point.
109 *
110 */
111    /*
112     * Convert to RGB, adjusting for white point differences if necessary.
113     */
114    memcpy((char *)pColor_scrn_return, (char *)pColor_exact_return,
115	   sizeof(XcmsColor));
116    if (pColor_scrn_return->format == XcmsRGBFormat) {
117	retval2 = XcmsSuccess;
118    } else if ((retval2 = XcmsConvertColors(ccc, pColor_scrn_return, 1,
119	    XcmsRGBFormat, (Bool *)NULL)) == XcmsFailure) {
120	return(XcmsFailure);
121    }
122
123    /*
124     * Then, convert XcmsColor structure to the target specification
125     *    format.  Note that we must use NULL instead of passing
126     *    pCompressed.
127     */
128
129    if (result_format ==  XcmsUndefinedFormat) {
130	result_format = pColor_exact_return->format;
131    }
132    if (result_format == XcmsRGBFormat) {
133	_XcmsUnresolveColor(ccc, pColor_scrn_return);
134    } else {
135	_XcmsResolveColor(ccc, pColor_scrn_return);
136	if (XcmsConvertColors(ccc, pColor_scrn_return, 1, result_format,
137		(Bool *) NULL) == XcmsFailure) {
138	    return(XcmsFailure);
139	}
140    }
141
142    return(retval1 > retval2 ? retval1 : retval2);
143
144PassToServer:
145    /*
146     * Xcms and i18n methods failed, so lets pass it to the server
147     * for parsing.
148     */
149
150    LockDisplay(dpy);
151    GetReq (LookupColor, req);
152    req->cmap = cmap;
153    req->nbytes = (CARD16)(n = (int)strlen(colorname));
154    req->length += (n + 3) >> 2;
155    Data (dpy, colorname, (long)n);
156    if (!_XReply (dpy, (xReply *) &reply, 0, xTrue)) {
157	UnlockDisplay(dpy);
158	SyncHandle();
159	return (XcmsFailure);
160	}
161    def.red   = reply.exactRed;
162    def.green = reply.exactGreen;
163    def.blue  = reply.exactBlue;
164
165    scr.red   = reply.screenRed;
166    scr.green = reply.screenGreen;
167    scr.blue  = reply.screenBlue;
168
169    UnlockDisplay(dpy);
170    SyncHandle();
171
172    _XColor_to_XcmsRGB(ccc, &def, pColor_exact_return, 1);
173    _XColor_to_XcmsRGB(ccc, &scr, pColor_scrn_return, 1);
174
175    /*
176     * Then, convert XcmsColor structure to the target specification
177     *    format.  Note that we must use NULL instead of passing
178     *    pCompressed.
179     */
180
181    if (result_format != XcmsRGBFormat
182	    && result_format != XcmsUndefinedFormat) {
183	if (XcmsConvertColors(ccc, pColor_exact_return, 1, result_format,
184		(Bool *) NULL) == XcmsFailure) {
185	    return(XcmsFailure);
186	}
187	if (XcmsConvertColors(ccc, pColor_scrn_return, 1, result_format,
188		(Bool *) NULL) == XcmsFailure) {
189	    return(XcmsFailure);
190	}
191    }
192
193    return(XcmsSuccess);
194}
195