int XStoreNamedColor( register Display *dpy, Colormap cmap, _Xconst char *name, /* STRING8 */ unsigned long pixel, /* CARD32 */ int flags) /* DoRed, DoGreen, DoBlue */ { unsigned int nbytes; register xStoreNamedColorReq *req; XcmsCCC ccc; XcmsColor cmsColor_exact; XColor scr_def; #ifdef XCMS /* * Let's Attempt to use Xcms approach to Parse Color */ if ((ccc = XcmsCCCOfColormap(dpy, cmap)) != (XcmsCCC)NULL) { if (_XcmsResolveColorString(ccc, &name, &cmsColor_exact, XcmsRGBFormat) >= XcmsSuccess) { _XcmsRGB_to_XColor(&cmsColor_exact, &scr_def, 1); scr_def.pixel = pixel; scr_def.flags = flags; return XStoreColor(dpy, cmap, &scr_def); } /* * Otherwise we failed; or name was changed with yet another * name. Thus pass name to the X Server. */ } #endif /* * The Xcms and i18n methods failed, so lets pass it to the server * for parsing. */ LockDisplay(dpy); GetReq(StoreNamedColor, req); req->cmap = cmap; req->flags = flags; req->pixel = pixel; req->nbytes = nbytes = strlen(name); req->length += (nbytes + 3) >> 2; /* round up to multiple of 4 */ Data(dpy, name, (long)nbytes); UnlockDisplay(dpy); SyncHandle(); return 0; }
Status XParseColor ( register Display *dpy, Colormap cmap, _Xconst char *spec, XColor *def) { register int n, i; int r, g, b; char c; XcmsCCC ccc; XcmsColor cmsColor; if (!spec) return(0); n = strlen (spec); if (*spec == '#') { /* * RGB */ spec++; n--; if (n != 3 && n != 6 && n != 9 && n != 12) return (0); n /= 3; g = b = 0; do { r = g; g = b; b = 0; for (i = n; --i >= 0; ) { c = *spec++; b <<= 4; if (c >= '0' && c <= '9') b |= c - '0'; else if (c >= 'A' && c <= 'F') b |= c - ('A' - 10); else if (c >= 'a' && c <= 'f') b |= c - ('a' - 10); else return (0); } } while (*spec != '\0'); n <<= 2; n = 16 - n; def->red = r << n; def->green = g << n; def->blue = b << n; def->flags = DoRed | DoGreen | DoBlue; return (1); } #ifdef XCMS /* * Let's Attempt to use Xcms and i18n approach to Parse Color */ if ((ccc = XcmsCCCOfColormap(dpy, cmap)) != (XcmsCCC)NULL) { const char *tmpName = spec; switch (_XcmsResolveColorString(ccc, &tmpName, &cmsColor, XcmsRGBFormat)) { case XcmsSuccess: case XcmsSuccessWithCompression: cmsColor.pixel = def->pixel; _XcmsRGB_to_XColor(&cmsColor, def, 1); return(1); case XcmsFailure: case _XCMS_NEWNAME: /* * if the result was _XCMS_NEWNAME tmpName points to * a string in cmsColNm.c:pairs table, for example, * gray70 would become tekhvc:0.0/70.0/0.0 */ break; } } #endif /* * Xcms and i18n methods failed, so lets pass it to the server * for parsing. */ { xLookupColorReply reply; register xLookupColorReq *req; LockDisplay(dpy); GetReq (LookupColor, req); req->cmap = cmap; req->nbytes = n = strlen(spec); req->length += (n + 3) >> 2; Data (dpy, spec, (long)n); if (!_XReply (dpy, (xReply *) &reply, 0, xTrue)) { UnlockDisplay(dpy); SyncHandle(); return (0); } def->red = reply.exactRed; def->green = reply.exactGreen; def->blue = reply.exactBlue; def->flags = DoRed | DoGreen | DoBlue; UnlockDisplay(dpy); SyncHandle(); return (1); } }
/* * NAME * XcmsAllocNamedColor - * * SYNOPSIS */ Status XcmsAllocNamedColor ( Display *dpy, Colormap cmap, _Xconst char *colorname, XcmsColor *pColor_scrn_return, XcmsColor *pColor_exact_return, XcmsColorFormat result_format) /* * DESCRIPTION * Finds the color specification associated with the color * name in the Device-Independent Color Name Database, then * converts that color specification to an RGB format. This * RGB value is then used in a call to XAllocColor to allocate * a read-only color cell. * * RETURNS * 0 if failed to parse string or find any entry in the database. * 1 if succeeded in converting color name to XcmsColor. * 2 if succeeded in converting color name to another color name. * */ { long nbytes; xAllocNamedColorReply rep; xAllocNamedColorReq *req; XColor hard_def; XColor exact_def; Status retval1 = 1; Status retval2 = XcmsSuccess; XcmsColor tmpColor; XColor XColor_in_out; XcmsCCC ccc; /* * 0. Check for invalid arguments. */ if (dpy == NULL || colorname[0] == '\0' || pColor_scrn_return == 0 || pColor_exact_return == NULL) { return(XcmsFailure); } if ((ccc = XcmsCCCOfColormap(dpy, cmap)) == (XcmsCCC)NULL) { return(XcmsFailure); } /* * 1. Convert string to a XcmsColor using Xcms and i18n mechanism */ if ((retval1 = _XcmsResolveColorString(ccc, &colorname, &tmpColor, result_format)) == XcmsFailure) { return(XcmsFailure); } if (retval1 == _XCMS_NEWNAME) { goto PassToServer; } memcpy((char *)pColor_exact_return, (char *)&tmpColor, sizeof(XcmsColor)); /* * 2. Convert tmpColor to RGB * Assume pColor_exact_return is now adjusted to Client White Point */ if ((retval2 = XcmsConvertColors(ccc, &tmpColor, 1, XcmsRGBFormat, (Bool *) NULL)) == XcmsFailure) { return(XcmsFailure); } /* * 3. Convert to XColor and call XAllocColor */ _XcmsRGB_to_XColor(&tmpColor, &XColor_in_out, 1); if (XAllocColor(ccc->dpy, cmap, &XColor_in_out) == 0) { return(XcmsFailure); } /* * 4. pColor_scrn_return * * Now convert to the target format. * We can ignore the return value because we're already in a * device-dependent format. */ _XColor_to_XcmsRGB(ccc, &XColor_in_out, pColor_scrn_return, 1); if (result_format != XcmsRGBFormat) { if (result_format == XcmsUndefinedFormat) { result_format = pColor_exact_return->format; } if (XcmsConvertColors(ccc, pColor_scrn_return, 1, result_format, (Bool *) NULL) == XcmsFailure) { return(XcmsFailure); } } return(retval1 > retval2 ? retval1 : retval2); PassToServer: /* * All previous methods failed, so lets pass it to the server * for parsing. */ dpy = ccc->dpy; LockDisplay(dpy); GetReq(AllocNamedColor, req); req->cmap = cmap; nbytes = req->nbytes = strlen(colorname); req->length += (nbytes + 3) >> 2; /* round up to mult of 4 */ _XSend(dpy, colorname, nbytes); /* _XSend is more efficient that Data, since _XReply follows */ if (!_XReply (dpy, (xReply *) &rep, 0, xTrue)) { UnlockDisplay(dpy); SyncHandle(); return (0); } exact_def.red = rep.exactRed; exact_def.green = rep.exactGreen; exact_def.blue = rep.exactBlue; hard_def.red = rep.screenRed; hard_def.green = rep.screenGreen; hard_def.blue = rep.screenBlue; exact_def.pixel = hard_def.pixel = rep.pixel; UnlockDisplay(dpy); SyncHandle(); /* * Now convert to the target format. */ _XColor_to_XcmsRGB(ccc, &exact_def, pColor_exact_return, 1); _XColor_to_XcmsRGB(ccc, &hard_def, pColor_scrn_return, 1); if (result_format != XcmsRGBFormat && result_format != XcmsUndefinedFormat) { if (XcmsConvertColors(ccc, pColor_exact_return, 1, result_format, (Bool *) NULL) == XcmsFailure) { return(XcmsFailure); } if (XcmsConvertColors(ccc, pColor_scrn_return, 1, result_format, (Bool *) NULL) == XcmsFailure) { return(XcmsFailure); } } return(XcmsSuccess); }
/* * NAME * XcmsLookupColor - * * SYNOPSIS */ Status XcmsLookupColor ( Display *dpy, Colormap cmap, _Xconst char *colorname, XcmsColor *pColor_exact_return, XcmsColor *pColor_scrn_return, XcmsColorFormat result_format) /* * DESCRIPTION * The XcmsLookupColor function finds the color specification * associated with a color name in the Device-Independent Color * Name Database. * RETURNS * This function returns both the color specification found in the * database (db specification) and the color specification for the * color displayable by the specified screen (screen * specification). The calling routine sets the format for these * returned specifications in the XcmsColor format component. * If XcmsUndefinedFormat, the specification is returned in the * format used to store the color in the database. */ { Status retval1 = XcmsSuccess; Status retval2 = XcmsSuccess; XcmsCCC ccc; register int n; xLookupColorReply reply; register xLookupColorReq *req; XColor def, scr; /* * 0. Check for invalid arguments. */ if (dpy == NULL || colorname[0] == '\0' || pColor_scrn_return == 0 || pColor_exact_return == NULL) { return(XcmsFailure); } if ((ccc = XcmsCCCOfColormap(dpy, cmap)) == (XcmsCCC)NULL) { return(XcmsFailure); } /* * 1. Convert string to a XcmsColor */ if ((retval1 = _XcmsResolveColorString(ccc, &colorname, pColor_exact_return, result_format)) == XcmsFailure) { return(XcmsFailure); } if (retval1 == _XCMS_NEWNAME) { goto PassToServer; } /* * 2. pColor_scrn_return * Assume the pColor_exact_return has already been adjusted to * the Client White Point. * */ /* * Convert to RGB, adjusting for white point differences if necessary. */ memcpy((char *)pColor_scrn_return, (char *)pColor_exact_return, sizeof(XcmsColor)); if (pColor_scrn_return->format == XcmsRGBFormat) { retval2 = XcmsSuccess; } else if ((retval2 = XcmsConvertColors(ccc, pColor_scrn_return, 1, XcmsRGBFormat, (Bool *)NULL)) == XcmsFailure) { return(XcmsFailure); } /* * Then, convert XcmsColor structure to the target specification * format. Note that we must use NULL instead of passing * pCompressed. */ if (result_format == XcmsUndefinedFormat) { result_format = pColor_exact_return->format; } if (result_format == XcmsRGBFormat) { _XcmsUnresolveColor(ccc, pColor_scrn_return); } else { _XcmsResolveColor(ccc, pColor_scrn_return); if (XcmsConvertColors(ccc, pColor_scrn_return, 1, result_format, (Bool *) NULL) == XcmsFailure) { return(XcmsFailure); } } return(retval1 > retval2 ? retval1 : retval2); PassToServer: /* * Xcms and i18n methods failed, so lets pass it to the server * for parsing. */ LockDisplay(dpy); GetReq (LookupColor, req); req->cmap = cmap; req->nbytes = n = strlen(colorname); req->length += (n + 3) >> 2; Data (dpy, colorname, (long)n); if (!_XReply (dpy, (xReply *) &reply, 0, xTrue)) { UnlockDisplay(dpy); SyncHandle(); return (XcmsFailure); } def.red = reply.exactRed; def.green = reply.exactGreen; def.blue = reply.exactBlue; scr.red = reply.screenRed; scr.green = reply.screenGreen; scr.blue = reply.screenBlue; UnlockDisplay(dpy); SyncHandle(); _XColor_to_XcmsRGB(ccc, &def, pColor_exact_return, 1); _XColor_to_XcmsRGB(ccc, &scr, pColor_scrn_return, 1); /* * Then, convert XcmsColor structure to the target specification * format. Note that we must use NULL instead of passing * pCompressed. */ if (result_format != XcmsRGBFormat && result_format != XcmsUndefinedFormat) { if (XcmsConvertColors(ccc, pColor_exact_return, 1, result_format, (Bool *) NULL) == XcmsFailure) { return(XcmsFailure); } if (XcmsConvertColors(ccc, pColor_scrn_return, 1, result_format, (Bool *) NULL) == XcmsFailure) { return(XcmsFailure); } } return(XcmsSuccess); }
Status XAllocNamedColor( register Display *dpy, Colormap cmap, _Xconst char *colorname, /* STRING8 */ XColor *hard_def, /* RETURN */ XColor *exact_def) /* RETURN */ { long nbytes; xAllocNamedColorReply rep; xAllocNamedColorReq *req; XcmsCCC ccc; XcmsColor cmsColor_exact; Status ret; /* * Let's Attempt to use Xcms and i18n approach to Parse Color */ if ((ccc = XcmsCCCOfColormap(dpy, cmap)) != (XcmsCCC)NULL) { const char *tmpName = colorname; switch (_XcmsResolveColorString(ccc, &tmpName, &cmsColor_exact, XcmsRGBFormat)) { case XcmsSuccess: case XcmsSuccessWithCompression: _XcmsRGB_to_XColor(&cmsColor_exact, exact_def, 1); memcpy((char *)hard_def, (char *)exact_def, sizeof(XColor)); ret = XAllocColor(dpy, cmap, hard_def); exact_def->pixel = hard_def->pixel; return(ret); case XcmsFailure: case _XCMS_NEWNAME: /* * if the result was _XCMS_NEWNAME tmpName points to * a string in cmsColNm.c:pairs table, for example, * gray70 would become tekhvc:0.0/70.0/0.0 */ break; } } /* * Xcms and i18n approach failed. */ LockDisplay(dpy); GetReq(AllocNamedColor, req); req->cmap = cmap; nbytes = req->nbytes = strlen(colorname); req->length += (nbytes + 3) >> 2; /* round up to mult of 4 */ _XSend(dpy, colorname, nbytes); /* _XSend is more efficient that Data, since _XReply follows */ if (!_XReply (dpy, (xReply *) &rep, 0, xTrue)) { UnlockDisplay(dpy); SyncHandle(); return (0); } exact_def->red = rep.exactRed; exact_def->green = rep.exactGreen; exact_def->blue = rep.exactBlue; hard_def->red = rep.screenRed; hard_def->green = rep.screenGreen; hard_def->blue = rep.screenBlue; exact_def->pixel = hard_def->pixel = rep.pixel; UnlockDisplay(dpy); SyncHandle(); return (1); }
Status XLookupColor ( register Display *dpy, Colormap cmap, _Xconst char *spec, XColor *def, XColor *scr) { register int n; xLookupColorReply reply; register xLookupColorReq *req; XcmsCCC ccc; XcmsColor cmsColor_exact; /* * Let's Attempt to use Xcms and i18n approach to Parse Color */ if ((ccc = XcmsCCCOfColormap(dpy, cmap)) != (XcmsCCC)NULL) { const char *tmpName = spec; switch (_XcmsResolveColorString(ccc, &tmpName, &cmsColor_exact, XcmsRGBFormat)) { case XcmsSuccess: case XcmsSuccessWithCompression: _XcmsRGB_to_XColor(&cmsColor_exact, def, 1); memcpy((char *)scr, (char *)def, sizeof(XColor)); _XUnresolveColor(ccc, scr); return(1); case XcmsFailure: case _XCMS_NEWNAME: /* * if the result was _XCMS_NEWNAME tmpName points to * a string in cmsColNm.c:pairs table, for example, * gray70 would become tekhvc:0.0/70.0/0.0 */ break; } } /* * Xcms and i18n methods failed, so lets pass it to the server * for parsing. */ n = strlen (spec); LockDisplay(dpy); GetReq (LookupColor, req); req->cmap = cmap; req->nbytes = n; req->length += (n + 3) >> 2; Data (dpy, spec, (long)n); if (!_XReply (dpy, (xReply *) &reply, 0, xTrue)) { UnlockDisplay(dpy); SyncHandle(); return (0); } def->red = reply.exactRed; def->green = reply.exactGreen; def->blue = reply.exactBlue; scr->red = reply.screenRed; scr->green = reply.screenGreen; scr->blue = reply.screenBlue; UnlockDisplay(dpy); SyncHandle(); return (1); }