1/*
2 *
3 * Copyright © 2002 Keith Packard
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, and that the name of Keith Packard not be used in
10 * advertising or publicity pertaining to distribution of the software without
11 * specific, written prior permission.  Keith Packard makes no
12 * representations about the suitability of this software for any purpose.  It
13 * is provided "as is" without express or implied warranty.
14 *
15 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
21 * PERFORMANCE OF THIS SOFTWARE.
22 */
23
24#ifdef HAVE_CONFIG_H
25#include <config.h>
26#endif
27#include "Xrenderint.h"
28
29Status
30XRenderParseColor(Display *dpy, char *spec, XRenderColor *def)
31{
32
33    if (!strncmp (spec, "rgba:", 5))
34    {
35	unsigned short	elements[4];
36	unsigned short	*pShort;
37	int		i;
38
39	spec += 5;
40	/*
41	 * Attempt to parse the value portion.
42	 */
43	pShort = elements;
44	for (i = 0; i < 4; i++, pShort++, spec++) {
45	    int n = 0;
46
47	    *pShort = 0;
48	    while (*spec != '/' && *spec != '\0') {
49		char	c;
50
51		if (++n > 4) {
52		    return 0;
53		}
54		c = *spec++;
55		*pShort = (unsigned short) (*pShort << 4);
56		if (c >= '0' && c <= '9')
57		    *pShort = (unsigned short) (*pShort | (c - '0'));
58		/* assume string in lowercase
59		else if (c >= 'A' && c <= 'F')
60		    *pShort |= c - ('A' - 10);
61		*/
62		else if (c >= 'a' && c <= 'f')
63		    *pShort = (unsigned short) (*pShort | (c - ('a' - 10)));
64		else return 0;
65	    }
66	    if (n == 0)
67		return 0;
68	    if (n < 4) {
69		*pShort = (unsigned short) (((unsigned long)*pShort * 0xFFFF) / (unsigned long) ((1 << n*4) - 1));
70	    }
71	}
72	def->red = elements[0];
73	def->green = elements[1];
74	def->blue = elements[2];
75	def->alpha = elements[3];
76    }
77    else
78    {
79	XColor	    coreColor;
80	Colormap    colormap;
81
82	colormap = DefaultColormap (dpy, DefaultScreen (dpy));
83	if (!XParseColor (dpy, colormap, spec, &coreColor))
84	    return 0;
85	def->red = coreColor.red;
86	def->green = coreColor.green;
87	def->blue = coreColor.blue;
88	def->alpha = 0xffff;
89    }
90    def->red   = (unsigned short) ((unsigned) (def->red   * def->alpha) / 0xffffU);
91    def->green = (unsigned short) ((unsigned) (def->green * def->alpha) / 0xffffU);
92    def->blue  = (unsigned short) ((unsigned) (def->blue  * def->alpha) / 0xffffU);
93    return 1;
94}
95