11ab64890Smrg
21ab64890Smrg/*
31ab64890Smrg * Code and supporting documentation (c) Copyright 1990 1991 Tektronix, Inc.
41ab64890Smrg * 	All Rights Reserved
561b2299dSmrg *
61ab64890Smrg * This file is a component of an X Window System-specific implementation
71ab64890Smrg * of Xcms based on the TekColor Color Management System.  Permission is
81ab64890Smrg * hereby granted to use, copy, modify, sell, and otherwise distribute this
91ab64890Smrg * software and its documentation for any purpose and without fee, provided
101ab64890Smrg * that this copyright, permission, and disclaimer notice is reproduced in
111ab64890Smrg * all copies of this software and in supporting documentation.  TekColor
121ab64890Smrg * is a trademark of Tektronix, Inc.
1361b2299dSmrg *
141ab64890Smrg * Tektronix makes no representation about the suitability of this software
151ab64890Smrg * for any purpose.  It is provided "as is" and with all faults.
1661b2299dSmrg *
171ab64890Smrg * TEKTRONIX DISCLAIMS ALL WARRANTIES APPLICABLE TO THIS SOFTWARE,
181ab64890Smrg * INCLUDING THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
191ab64890Smrg * PARTICULAR PURPOSE.  IN NO EVENT SHALL TEKTRONIX BE LIABLE FOR ANY
201ab64890Smrg * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
211ab64890Smrg * RESULTING FROM LOSS OF USE, DATA, OR PROFITS, WHETHER IN AN ACTION OF
221ab64890Smrg * CONTRACT, NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
231ab64890Smrg * CONNECTION WITH THE USE OR THE PERFORMANCE OF THIS SOFTWARE.
241ab64890Smrg *
251ab64890Smrg *
261ab64890Smrg *	NAME
271ab64890Smrg *		XcmsAlNCol.c
281ab64890Smrg *
291ab64890Smrg *	DESCRIPTION
301ab64890Smrg *		Source for XcmsAllocNamedColor
311ab64890Smrg *
321ab64890Smrg *
331ab64890Smrg */
341ab64890Smrg
351ab64890Smrg#ifdef HAVE_CONFIG_H
361ab64890Smrg#include <config.h>
371ab64890Smrg#endif
381ab64890Smrg#include <stdio.h>
391ab64890Smrg#include "Xlibint.h"
401ab64890Smrg#include "Xcmsint.h"
411ab64890Smrg#include "Cv.h"
421ab64890Smrg
431ab64890Smrg
441ab64890Smrg/*
451ab64890Smrg *	NAME
4661b2299dSmrg *		XcmsAllocNamedColor -
471ab64890Smrg *
481ab64890Smrg *	SYNOPSIS
491ab64890Smrg */
501ab64890SmrgStatus
511ab64890SmrgXcmsAllocNamedColor (
521ab64890Smrg    Display *dpy,
531ab64890Smrg    Colormap cmap,
541ab64890Smrg    _Xconst char *colorname,
551ab64890Smrg    XcmsColor *pColor_scrn_return,
561ab64890Smrg    XcmsColor *pColor_exact_return,
571ab64890Smrg    XcmsColorFormat result_format)
581ab64890Smrg/*
591ab64890Smrg *	DESCRIPTION
601ab64890Smrg *		Finds the color specification associated with the color
611ab64890Smrg *		name in the Device-Independent Color Name Database, then
621ab64890Smrg *		converts that color specification to an RGB format.  This
631ab64890Smrg *		RGB value is then used in a call to XAllocColor to allocate
641ab64890Smrg *		a read-only color cell.
651ab64890Smrg *
661ab64890Smrg *	RETURNS
671ab64890Smrg *		0 if failed to parse string or find any entry in the database.
681ab64890Smrg *		1 if succeeded in converting color name to XcmsColor.
691ab64890Smrg *		2 if succeeded in converting color name to another color name.
701ab64890Smrg *
711ab64890Smrg */
721ab64890Smrg{
731ab64890Smrg    long nbytes;
741ab64890Smrg    xAllocNamedColorReply rep;
751ab64890Smrg    xAllocNamedColorReq *req;
761ab64890Smrg    XColor hard_def;
771ab64890Smrg    XColor exact_def;
781ab64890Smrg    Status retval1 = 1;
791ab64890Smrg    Status retval2 = XcmsSuccess;
801ab64890Smrg    XcmsColor tmpColor;
811ab64890Smrg    XColor XColor_in_out;
821ab64890Smrg    XcmsCCC ccc;
831ab64890Smrg
841ab64890Smrg    /*
851ab64890Smrg     * 0. Check for invalid arguments.
861ab64890Smrg     */
871ab64890Smrg    if (dpy == NULL || colorname[0] == '\0' || pColor_scrn_return == 0
881ab64890Smrg	    || pColor_exact_return == NULL) {
891ab64890Smrg	return(XcmsFailure);
901ab64890Smrg    }
911ab64890Smrg
921ab64890Smrg    if ((ccc = XcmsCCCOfColormap(dpy, cmap)) == (XcmsCCC)NULL) {
931ab64890Smrg	return(XcmsFailure);
941ab64890Smrg    }
951ab64890Smrg
961ab64890Smrg    /*
971ab64890Smrg     * 1. Convert string to a XcmsColor using Xcms and i18n mechanism
981ab64890Smrg     */
991ab64890Smrg    if ((retval1 = _XcmsResolveColorString(ccc, &colorname,
1001ab64890Smrg	    &tmpColor, result_format)) == XcmsFailure) {
1011ab64890Smrg	return(XcmsFailure);
1021ab64890Smrg    }
1031ab64890Smrg    if (retval1 == _XCMS_NEWNAME) {
1041ab64890Smrg	goto PassToServer;
1051ab64890Smrg    }
1061ab64890Smrg    memcpy((char *)pColor_exact_return, (char *)&tmpColor, sizeof(XcmsColor));
1071ab64890Smrg
1081ab64890Smrg    /*
1091ab64890Smrg     * 2. Convert tmpColor to RGB
1101ab64890Smrg     *	Assume pColor_exact_return is now adjusted to Client White Point
1111ab64890Smrg     */
1121ab64890Smrg    if ((retval2 = XcmsConvertColors(ccc, &tmpColor,
1131ab64890Smrg	    1, XcmsRGBFormat, (Bool *) NULL)) == XcmsFailure) {
1141ab64890Smrg	return(XcmsFailure);
1151ab64890Smrg    }
1161ab64890Smrg
1171ab64890Smrg    /*
1181ab64890Smrg     * 3. Convert to XColor and call XAllocColor
1191ab64890Smrg     */
1201ab64890Smrg    _XcmsRGB_to_XColor(&tmpColor, &XColor_in_out, 1);
1211ab64890Smrg    if (XAllocColor(ccc->dpy, cmap, &XColor_in_out) == 0) {
1221ab64890Smrg	return(XcmsFailure);
1231ab64890Smrg    }
1241ab64890Smrg
1251ab64890Smrg    /*
1261ab64890Smrg     * 4. pColor_scrn_return
1271ab64890Smrg     *
1281ab64890Smrg     * Now convert to the target format.
1291ab64890Smrg     *    We can ignore the return value because we're already in a
1301ab64890Smrg     *    device-dependent format.
1311ab64890Smrg     */
1321ab64890Smrg    _XColor_to_XcmsRGB(ccc, &XColor_in_out, pColor_scrn_return, 1);
1331ab64890Smrg    if (result_format != XcmsRGBFormat) {
1341ab64890Smrg	if (result_format == XcmsUndefinedFormat) {
1351ab64890Smrg	    result_format = pColor_exact_return->format;
1361ab64890Smrg	}
1371ab64890Smrg	if (XcmsConvertColors(ccc, pColor_scrn_return, 1, result_format,
1381ab64890Smrg		(Bool *) NULL) == XcmsFailure) {
1391ab64890Smrg	    return(XcmsFailure);
1401ab64890Smrg	}
1411ab64890Smrg    }
1421ab64890Smrg
1431ab64890Smrg    return(retval1 > retval2 ? retval1 : retval2);
1441ab64890Smrg
1451ab64890SmrgPassToServer:
1461ab64890Smrg    /*
1471ab64890Smrg     * All previous methods failed, so lets pass it to the server
1481ab64890Smrg     * for parsing.
1491ab64890Smrg     */
1501ab64890Smrg    dpy = ccc->dpy;
1511ab64890Smrg    LockDisplay(dpy);
1521ab64890Smrg    GetReq(AllocNamedColor, req);
1531ab64890Smrg
1541ab64890Smrg    req->cmap = cmap;
1559c019ec5Smaya    nbytes = req->nbytes = (CARD16) strlen(colorname);
1561ab64890Smrg    req->length += (nbytes + 3) >> 2; /* round up to mult of 4 */
1571ab64890Smrg
1581ab64890Smrg    _XSend(dpy, colorname, nbytes);
1591ab64890Smrg       /* _XSend is more efficient that Data, since _XReply follows */
1601ab64890Smrg
1611ab64890Smrg    if (!_XReply (dpy, (xReply *) &rep, 0, xTrue)) {
1621ab64890Smrg	UnlockDisplay(dpy);
1631ab64890Smrg        SyncHandle();
1641ab64890Smrg        return (0);
1651ab64890Smrg    }
1661ab64890Smrg
1671ab64890Smrg    exact_def.red = rep.exactRed;
1681ab64890Smrg    exact_def.green = rep.exactGreen;
1691ab64890Smrg    exact_def.blue = rep.exactBlue;
1701ab64890Smrg
1711ab64890Smrg    hard_def.red = rep.screenRed;
1721ab64890Smrg    hard_def.green = rep.screenGreen;
1731ab64890Smrg    hard_def.blue = rep.screenBlue;
1741ab64890Smrg
1751ab64890Smrg    exact_def.pixel = hard_def.pixel = rep.pixel;
1761ab64890Smrg
1771ab64890Smrg    UnlockDisplay(dpy);
1781ab64890Smrg    SyncHandle();
1791ab64890Smrg
1801ab64890Smrg    /*
1811ab64890Smrg     * Now convert to the target format.
1821ab64890Smrg     */
1831ab64890Smrg    _XColor_to_XcmsRGB(ccc, &exact_def, pColor_exact_return, 1);
1841ab64890Smrg    _XColor_to_XcmsRGB(ccc, &hard_def, pColor_scrn_return, 1);
1851ab64890Smrg    if (result_format != XcmsRGBFormat
1861ab64890Smrg	    && result_format != XcmsUndefinedFormat) {
1871ab64890Smrg	if (XcmsConvertColors(ccc, pColor_exact_return, 1, result_format,
1881ab64890Smrg		(Bool *) NULL) == XcmsFailure) {
1891ab64890Smrg	    return(XcmsFailure);
1901ab64890Smrg	}
1911ab64890Smrg	if (XcmsConvertColors(ccc, pColor_scrn_return, 1, result_format,
1921ab64890Smrg		(Bool *) NULL) == XcmsFailure) {
1931ab64890Smrg	    return(XcmsFailure);
1941ab64890Smrg	}
1951ab64890Smrg    }
1961ab64890Smrg
1971ab64890Smrg    return(XcmsSuccess);
1981ab64890Smrg}
199