Release 0.4.7
[wine/multimedia.git] / objects / color.c
blob0215d47c9acd09bf799ad6afc5033f49d0632d2c
1 /*
2 * Color functions
4 * Copyright 1993 Alexandre Julliard
5 */
7 static char Copyright[] = "Copyright Alexandre Julliard, 1993";
9 #include <stdlib.h>
10 #include <X11/Xlib.h>
12 #include "windows.h"
14 extern Display * XT_display;
15 extern Screen * XT_screen;
18 /*
19 * We try to use a private color map if possible, because Windows programs
20 * assume that palette(0) == Black and palette(max-1) == White.
22 #define USE_PRIVATE_MAP
24 Colormap COLOR_WinColormap = 0;
27 /* System colors */
29 static const char * SysColors[] =
31 /* Low pixel values (0..7) */
33 "black", "red4", "green4", "yellow4",
34 "blue4", "magenta4", "cyan4", "gray50",
36 /* High pixel values (max-7..max) */
38 "gray75", "red", "green", "yellow",
39 "blue", "magenta", "cyan", "white"
42 #define NB_SYS_COLORS (sizeof(SysColors) / sizeof(SysColors[0]))
45 /***********************************************************************
46 * COLOR_FillDefaultMap
48 * Try to allocate colors from default screen map (used when we
49 * don't want to or can't use a private map).
51 static int COLOR_FillDefaultMap()
53 XColor color;
54 int i, total = 0;
56 for (i = 0; i < NB_SYS_COLORS; i++)
58 if (XParseColor( XT_display, DefaultColormapOfScreen( XT_screen ),
59 SysColors[i], &color ))
61 if (XAllocColor( XT_display, DefaultColormapOfScreen( XT_screen ),
62 &color ))
63 total++;
66 return total;
70 /***********************************************************************
71 * COLOR_BuildMap
73 * Fill the private colormap.
75 #ifdef USE_PRIVATE_MAP
77 static BOOL COLOR_BuildMap( Colormap map, int depth, int size )
79 XColor color;
80 int i;
82 /* Fill the whole map with a range of colors */
84 if ((1 << depth) > NB_SYS_COLORS)
86 int red_incr, green_incr, blue_incr;
87 int r, g, b;
89 blue_incr = 0x10000 >> (depth / 3);
90 red_incr = 0x10000 >> ((depth + 1) / 3);
91 green_incr = 0x10000 >> ((depth + 2) / 3);
93 for (i = 0, r = red_incr - 1; r < 0x10000; r += red_incr)
94 for (g = green_incr - 1; g < 0x10000; g += green_incr)
95 for (b = blue_incr - 1; b < 0x10000; b += blue_incr)
97 if (i >= size) break;
98 color.pixel = i++;
99 color.red = r;
100 color.green = g;
101 color.blue = b;
102 XStoreColor( XT_display, map, &color );
106 /* Store the system palette colors */
108 for (i = 0; i < NB_SYS_COLORS; i++)
110 if (!XParseColor( XT_display, map, SysColors[i], &color ))
111 color.red = color.green = color.blue = color.flags = 0;
112 if (i < NB_SYS_COLORS/2) color.pixel = i;
113 else color.pixel = (1 << depth) - NB_SYS_COLORS + i;
114 if (color.pixel < size) XStoreColor( XT_display, map, &color );
116 return TRUE;
118 #endif /* USE_PRIVATE_MAP */
120 /***********************************************************************
121 * COLOR_Init
123 BOOL COLOR_Init()
125 Visual * visual = DefaultVisual( XT_display, DefaultScreen(XT_display) );
127 switch(visual->class)
129 case GrayScale:
130 case PseudoColor:
131 case DirectColor:
133 #ifdef USE_PRIVATE_MAP
134 COLOR_WinColormap = XCreateColormap( XT_display,
135 DefaultRootWindow(XT_display),
136 visual, AllocAll );
137 if (COLOR_WinColormap)
138 COLOR_BuildMap(COLOR_WinColormap,
139 DefaultDepth(XT_display, DefaultScreen(XT_display)),
140 visual->map_entries );
141 else COLOR_FillDefaultMap();
142 break;
143 #endif /* USE_PRIVATE_MAP */
145 case StaticGray:
146 case StaticColor:
147 case TrueColor:
148 COLOR_FillDefaultMap();
149 COLOR_WinColormap = DefaultColormapOfScreen( XT_screen );
150 break;
152 return TRUE;