| 1 |
/* |
| 2 |
|
| 3 |
Copyright 1986, 1998 The Open Group |
| 4 |
|
| 5 |
Permission to use, copy, modify, distribute, and sell this software and its |
| 6 |
documentation for any purpose is hereby granted without fee, provided that |
| 7 |
the above copyright notice appear in all copies and that both that |
| 8 |
copyright notice and this permission notice appear in supporting |
| 9 |
documentation. |
| 10 |
|
| 11 |
The above copyright notice and this permission notice shall be included in |
| 12 |
all copies or substantial portions of the Software. |
| 13 |
|
| 14 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
| 18 |
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 19 |
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 20 |
|
| 21 |
Except as contained in this notice, the name of The Open Group shall not be |
| 22 |
used in advertising or otherwise to promote the sale, use or other dealings |
| 23 |
in this Software without prior written authorization from The Open Group. |
| 24 |
|
| 25 |
*/ |
| 26 |
|
| 27 |
#ifdef HAVE_CONFIG_H |
| 28 |
#include <config.h> |
| 29 |
#endif |
| 30 |
#include <limits.h> |
| 31 |
#include <stdio.h> |
| 32 |
#include "Xlibint.h" |
| 33 |
#include "Xcmsint.h" |
| 34 |
|
| 35 |
Status |
| 36 |
XAllocNamedColor( |
| 37 |
register Display *dpy, |
| 38 |
Colormap cmap, |
| 39 |
_Xconst char *colorname, /* STRING8 */ |
| 40 |
XColor *hard_def, /* RETURN */ |
| 41 |
XColor *exact_def) /* RETURN */ |
| 42 |
{ |
| 43 |
|
| 44 |
long nbytes; |
| 45 |
xAllocNamedColorReply rep; |
| 46 |
xAllocNamedColorReq *req; |
| 47 |
|
| 48 |
XcmsCCC ccc; |
| 49 |
XcmsColor cmsColor_exact; |
| 50 |
Status ret; |
| 51 |
|
| 52 |
if (colorname != NULL && strlen(colorname) >= USHRT_MAX) |
| 53 |
return (0); |
| 54 |
|
| 55 |
#ifdef XCMS |
| 56 |
/* |
| 57 |
* Let's Attempt to use Xcms and i18n approach to Parse Color |
| 58 |
*/ |
| 59 |
if ((ccc = XcmsCCCOfColormap(dpy, cmap)) != (XcmsCCC)NULL) { |
| 60 |
const char *tmpName = colorname; |
| 61 |
|
| 62 |
switch (_XcmsResolveColorString(ccc, &tmpName, &cmsColor_exact, |
| 63 |
XcmsRGBFormat)) { |
| 64 |
case XcmsSuccess: |
| 65 |
case XcmsSuccessWithCompression: |
| 66 |
_XcmsRGB_to_XColor(&cmsColor_exact, exact_def, 1); |
| 67 |
memcpy((char *)hard_def, (char *)exact_def, sizeof(XColor)); |
| 68 |
ret = XAllocColor(dpy, cmap, hard_def); |
| 69 |
exact_def->pixel = hard_def->pixel; |
| 70 |
return(ret); |
| 71 |
case XcmsFailure: |
| 72 |
case _XCMS_NEWNAME: |
| 73 |
/* |
| 74 |
* if the result was _XCMS_NEWNAME tmpName points to |
| 75 |
* a string in cmsColNm.c:pairs table, for example, |
| 76 |
* gray70 would become tekhvc:0.0/70.0/0.0 |
| 77 |
*/ |
| 78 |
break; |
| 79 |
} |
| 80 |
} |
| 81 |
#endif |
| 82 |
|
| 83 |
/* |
| 84 |
* Xcms and i18n approach failed. |
| 85 |
*/ |
| 86 |
LockDisplay(dpy); |
| 87 |
GetReq(AllocNamedColor, req); |
| 88 |
|
| 89 |
req->cmap = cmap; |
| 90 |
nbytes = req->nbytes = (CARD16) strlen(colorname); |
| 91 |
req->length += (nbytes + 3) >> 2; /* round up to mult of 4 */ |
| 92 |
|
| 93 |
_XSend(dpy, colorname, nbytes); |
| 94 |
/* _XSend is more efficient that Data, since _XReply follows */ |
| 95 |
|
| 96 |
if (!_XReply (dpy, (xReply *) &rep, 0, xTrue)) { |
| 97 |
UnlockDisplay(dpy); |
| 98 |
SyncHandle(); |
| 99 |
return (0); |
| 100 |
} |
| 101 |
|
| 102 |
exact_def->red = rep.exactRed; |
| 103 |
exact_def->green = rep.exactGreen; |
| 104 |
exact_def->blue = rep.exactBlue; |
| 105 |
|
| 106 |
hard_def->red = rep.screenRed; |
| 107 |
hard_def->green = rep.screenGreen; |
| 108 |
hard_def->blue = rep.screenBlue; |
| 109 |
|
| 110 |
exact_def->pixel = hard_def->pixel = rep.pixel; |
| 111 |
|
| 112 |
UnlockDisplay(dpy); |
| 113 |
SyncHandle(); |
| 114 |
return (1); |
| 115 |
} |