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