Home | History | Annotate | Line # | Download | only in src
      1 /*
      2  * Copyright  2000 Keith Packard
      3  *
      4  * Permission to use, copy, modify, distribute, and sell this software and its
      5  * documentation for any purpose is hereby granted without fee, provided that
      6  * the above copyright notice appear in all copies and that both that
      7  * copyright notice and this permission notice appear in supporting
      8  * documentation, and that the name of Keith Packard not be used in
      9  * advertising or publicity pertaining to distribution of the software without
     10  * specific, written prior permission.  Keith Packard makes no
     11  * representations about the suitability of this software for any purpose.  It
     12  * is provided "as is" without express or implied warranty.
     13  *
     14  * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
     15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
     16  * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
     17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
     18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
     19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
     20  * PERFORMANCE OF THIS SOFTWARE.
     21  */
     22 
     23 #include "xftint.h"
     24 
     25 _X_EXPORT Bool
     26 XftColorAllocName (Display          *dpy,
     27 		   _Xconst Visual   *visual _X_UNUSED,
     28 		   Colormap          cmap,
     29 		   _Xconst char	    *name,
     30 		   XftColor         *result)
     31 {
     32     XColor  screen, exact;
     33 
     34     if (!XAllocNamedColor (dpy, cmap, name, &screen, &exact))
     35     {
     36 	/* XXX stick standard colormap stuff here */
     37 	return False;
     38     }
     39 
     40     result->pixel = screen.pixel;
     41     result->color.red = exact.red;
     42     result->color.green = exact.green;
     43     result->color.blue = exact.blue;
     44     result->color.alpha = 0xffff;
     45     return True;
     46 }
     47 
     48 static short
     49 maskbase (unsigned long m)
     50 {
     51     short        i;
     52 
     53     if (!m)
     54 	return 0;
     55     i = 0;
     56     while (!(m&1))
     57     {
     58 	m>>=1;
     59 	i++;
     60     }
     61     return i;
     62 }
     63 
     64 static short
     65 masklen (unsigned long m)
     66 {
     67     unsigned long y;
     68 
     69     y = (m >> 1) &033333333333;
     70     y = m - y - ((y >>1) & 033333333333);
     71     return (short) (((y + (y >> 3)) & 030707070707) % 077);
     72 }
     73 
     74 _X_EXPORT Bool
     75 XftColorAllocValue (Display	    *dpy,
     76 		    Visual	    *visual,
     77 		    Colormap	    cmap,
     78 		    _Xconst XRenderColor    *color,
     79 		    XftColor	    *result)
     80 {
     81     if (visual->class == TrueColor)
     82     {
     83 	int	    red_shift, red_len;
     84 	int	    green_shift, green_len;
     85 	int	    blue_shift, blue_len;
     86 
     87 	red_shift = maskbase (visual->red_mask);
     88 	red_len = masklen (visual->red_mask);
     89 	green_shift = maskbase (visual->green_mask);
     90 	green_len = masklen (visual->green_mask);
     91 	blue_shift = maskbase (visual->blue_mask);
     92 	blue_len = masklen (visual->blue_mask);
     93 	result->pixel = (unsigned long)(((color->red   >> (16 - red_len))   << red_shift) |
     94 					((color->green >> (16 - green_len)) << green_shift) |
     95 					((color->blue  >> (16 - blue_len))  << blue_shift));
     96     }
     97     else
     98     {
     99 	XColor  xcolor;
    100 
    101 	xcolor.red = color->red;
    102 	xcolor.green = color->green;
    103 	xcolor.blue = color->blue;
    104 	if (!XAllocColor (dpy, cmap, &xcolor))
    105 	    return False;
    106 	result->pixel = xcolor.pixel;
    107     }
    108     result->color.red = color->red;
    109     result->color.green = color->green;
    110     result->color.blue = color->blue;
    111     result->color.alpha = color->alpha;
    112     return True;
    113 }
    114 
    115 _X_EXPORT void
    116 XftColorFree (Display	*dpy,
    117 	      Visual	*visual,
    118 	      Colormap	cmap,
    119 	      XftColor	*color)
    120 {
    121     if (visual->class != TrueColor)
    122 	XFreeColors (dpy, cmap, &color->pixel, 1, 0);
    123 }
    124