upload tizen2.0 source
[framework/uifw/xorg/lib/libx11.git] / src / xcms / cmsAllNCol.c
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 #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 \f
44 /*
45  *      NAME
46  *              XcmsAllocNamedColor -
47  *
48  *      SYNOPSIS
49  */
50 Status
51 XcmsAllocNamedColor (
52     Display *dpy,
53     Colormap cmap,
54     _Xconst char *colorname,
55     XcmsColor *pColor_scrn_return,
56     XcmsColor *pColor_exact_return,
57     XcmsColorFormat result_format)
58 /*
59  *      DESCRIPTION
60  *              Finds the color specification associated with the color
61  *              name in the Device-Independent Color Name Database, then
62  *              converts that color specification to an RGB format.  This
63  *              RGB value is then used in a call to XAllocColor to allocate
64  *              a read-only color cell.
65  *
66  *      RETURNS
67  *              0 if failed to parse string or find any entry in the database.
68  *              1 if succeeded in converting color name to XcmsColor.
69  *              2 if succeeded in converting color name to another color name.
70  *
71  */
72 {
73     long nbytes;
74     xAllocNamedColorReply rep;
75     xAllocNamedColorReq *req;
76     XColor hard_def;
77     XColor exact_def;
78     Status retval1 = 1;
79     Status retval2 = XcmsSuccess;
80     XcmsColor tmpColor;
81     XColor XColor_in_out;
82     XcmsCCC ccc;
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 using Xcms and i18n mechanism
98      */
99     if ((retval1 = _XcmsResolveColorString(ccc, &colorname,
100             &tmpColor, result_format)) == XcmsFailure) {
101         return(XcmsFailure);
102     }
103     if (retval1 == _XCMS_NEWNAME) {
104         goto PassToServer;
105     }
106     memcpy((char *)pColor_exact_return, (char *)&tmpColor, sizeof(XcmsColor));
107
108     /*
109      * 2. Convert tmpColor to RGB
110      *  Assume pColor_exact_return is now adjusted to Client White Point
111      */
112     if ((retval2 = XcmsConvertColors(ccc, &tmpColor,
113             1, XcmsRGBFormat, (Bool *) NULL)) == XcmsFailure) {
114         return(XcmsFailure);
115     }
116
117     /*
118      * 3. Convert to XColor and call XAllocColor
119      */
120     _XcmsRGB_to_XColor(&tmpColor, &XColor_in_out, 1);
121     if (XAllocColor(ccc->dpy, cmap, &XColor_in_out) == 0) {
122         return(XcmsFailure);
123     }
124
125     /*
126      * 4. pColor_scrn_return
127      *
128      * Now convert to the target format.
129      *    We can ignore the return value because we're already in a
130      *    device-dependent format.
131      */
132     _XColor_to_XcmsRGB(ccc, &XColor_in_out, pColor_scrn_return, 1);
133     if (result_format != XcmsRGBFormat) {
134         if (result_format == XcmsUndefinedFormat) {
135             result_format = pColor_exact_return->format;
136         }
137         if (XcmsConvertColors(ccc, pColor_scrn_return, 1, result_format,
138                 (Bool *) NULL) == XcmsFailure) {
139             return(XcmsFailure);
140         }
141     }
142
143     return(retval1 > retval2 ? retval1 : retval2);
144
145 PassToServer:
146     /*
147      * All previous methods failed, so lets pass it to the server
148      * for parsing.
149      */
150     dpy = ccc->dpy;
151     LockDisplay(dpy);
152     GetReq(AllocNamedColor, req);
153
154     req->cmap = cmap;
155     nbytes = req->nbytes = strlen(colorname);
156     req->length += (nbytes + 3) >> 2; /* round up to mult of 4 */
157
158     _XSend(dpy, colorname, nbytes);
159        /* _XSend is more efficient that Data, since _XReply follows */
160
161     if (!_XReply (dpy, (xReply *) &rep, 0, xTrue)) {
162         UnlockDisplay(dpy);
163         SyncHandle();
164         return (0);
165     }
166
167     exact_def.red = rep.exactRed;
168     exact_def.green = rep.exactGreen;
169     exact_def.blue = rep.exactBlue;
170
171     hard_def.red = rep.screenRed;
172     hard_def.green = rep.screenGreen;
173     hard_def.blue = rep.screenBlue;
174
175     exact_def.pixel = hard_def.pixel = rep.pixel;
176
177     UnlockDisplay(dpy);
178     SyncHandle();
179
180     /*
181      * Now convert to the target format.
182      */
183     _XColor_to_XcmsRGB(ccc, &exact_def, pColor_exact_return, 1);
184     _XColor_to_XcmsRGB(ccc, &hard_def, pColor_scrn_return, 1);
185     if (result_format != XcmsRGBFormat
186             && result_format != XcmsUndefinedFormat) {
187         if (XcmsConvertColors(ccc, pColor_exact_return, 1, result_format,
188                 (Bool *) NULL) == XcmsFailure) {
189             return(XcmsFailure);
190         }
191         if (XcmsConvertColors(ccc, pColor_scrn_return, 1, result_format,
192                 (Bool *) NULL) == XcmsFailure) {
193             return(XcmsFailure);
194         }
195     }
196
197     return(XcmsSuccess);
198 }