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); }
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); }
/* * NAME * XcmsSetColor - * * SYNOPSIS */ Status _XcmsSetGetColor( Status (*xColorProc)( Display* /* display */, Colormap /* colormap */, XColor* /* screen_in_out */), Display *dpy, Colormap cmap, XcmsColor *pColors_in_out, XcmsColorFormat result_format, Bool *pCompressed) /* * DESCRIPTION * Routine containing code common to: * XcmsAllocColor * XcmsQueryColor * XcmsStoreColor * * RETURNS * XcmsFailure if failed; * XcmsSuccess if it succeeded without gamut compression; * XcmsSuccessWithCompression if it succeeded with gamut * compression; */ { XcmsCCC ccc; XColor XColors_in_out; Status retval = XcmsSuccess; /* * Argument Checking * 1. Assume xColorProc is correct * 2. Insure ccc not NULL * 3. Assume cmap correct (should be checked by Server) * 4. Insure pColors_in_out valid * 5. Assume method_in is valid (should be checked by Server) */ if (dpy == NULL) { return(XcmsFailure); } if (result_format == XcmsUndefinedFormat) { return(XcmsFailure); } if ( !((*xColorProc == XAllocColor) || (*xColorProc == XStoreColor) || (*xColorProc == XQueryColor)) ) { return(XcmsFailure); } if ((ccc = XcmsCCCOfColormap(dpy, cmap)) == (XcmsCCC)NULL) { return(XcmsFailure); } if (*xColorProc == XQueryColor) { goto Query; } /* * Convert to RGB, adjusting for white point differences if necessary. */ if ((retval = XcmsConvertColors(ccc, pColors_in_out, 1, XcmsRGBFormat, pCompressed)) == XcmsFailure) { return(XcmsFailure); } Query: /* * Convert XcmsColor to XColor structures */ _XcmsRGB_to_XColor(pColors_in_out, &XColors_in_out, 1); /* * Now make appropriate X Call */ if (*xColorProc == XAllocColor) { if ((*xColorProc)(ccc->dpy, cmap, &XColors_in_out) == 0) { return(XcmsFailure); } } else if ((*xColorProc == XQueryColor) || (*xColorProc == XStoreColor)) { /* Note: XQueryColor and XStoreColor do not return any Status */ (*xColorProc)(ccc->dpy, cmap, &XColors_in_out); } else { return(XcmsFailure); } if ((*xColorProc == XStoreColor)) { return(retval); } /* * Now, convert the returned XColor (i.e., rgb) to XcmsColor structures */ _XColor_to_XcmsRGB(ccc, &XColors_in_out, pColors_in_out, 1); /* * Then, convert XcmsColor structures to the original specification * format. Note that we must use NULL instead of passing * pCompressed. */ if (result_format != XcmsRGBFormat) { if (XcmsConvertColors(ccc, pColors_in_out, 1, result_format, (Bool *) NULL) == XcmsFailure) { return(XcmsFailure); } } return(retval); }
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); }