]> Dogcows Code - chaz/openbox/blob - render/color.c
add strict ansi compliance
[chaz/openbox] / render / color.c
1 #include <X11/Xlib.h>
2 #include <X11/Xutil.h>
3 #include "render.h"
4 #include "color.h"
5 #include "../kernel/openbox.h"
6 void color_allocate_gc(color_rgb *in)
7 {
8 XGCValues gcv;
9
10 gcv.foreground = in->pixel;
11 gcv.cap_style = CapProjecting;
12 in->gc = XCreateGC(ob_display, ob_root, GCForeground | GCCapStyle, &gcv);
13 }
14
15 color_rgb *color_parse(char *colorname)
16 {
17 XColor xcol;
18
19 g_assert(colorname != NULL);
20 /* get rgb values from colorname */
21
22 xcol.red = 0;
23 xcol.green = 0;
24 xcol.blue = 0;
25 xcol.pixel = 0;
26 if (!XParseColor(ob_display, render_colormap, colorname, &xcol)) {
27 g_warning("unable to parse color '%s'", colorname);
28 return NULL;
29 }
30 return color_new(xcol.red >> 8, xcol.green >> 8, xcol.blue >> 8);
31 }
32
33 color_rgb *color_new(int r, int g, int b)
34 {
35 /* this should be replaced with something far cooler */
36 color_rgb *out;
37 XColor xcol;
38 xcol.red = (r << 8) | r;
39 xcol.green = (g << 8) | g;
40 xcol.blue = (b << 8) | b;
41 if (XAllocColor(ob_display, render_colormap, &xcol)) {
42 out = g_new(color_rgb, 1);
43 out->r = xcol.red >> 8;
44 out->g = xcol.green >> 8;
45 out->b = xcol.blue >> 8;
46 out->gc = None;
47 out->pixel = xcol.pixel;
48 return out;
49 }
50 return NULL;
51 }
52
53 /*XXX same color could be pointed to twice, this might have to be a refcount*/
54
55 void color_free(color_rgb *c)
56 {
57 if (c->gc != None)
58 XFreeGC(ob_display, c->gc);
59 g_free(c);
60 }
61
62 void reduce_depth(pixel32 *data, XImage *im)
63 {
64 /* since pixel32 is the largest possible pixel size, we can share the
65 array*/
66 int r, g, b;
67 int x,y;
68 pixel16 *p = (pixel16*) data;
69 switch (im->bits_per_pixel) {
70 case 32:
71 if ((render_red_offset != default_red_shift) ||
72 (render_blue_offset != default_blue_shift) ||
73 (render_green_offset != default_green_shift)) {
74 for (y = 0; y < im->height; y++) {
75 for (x = 0; x < im->width; x++) {
76 r = (data[x] >> default_red_shift) & 0xFF;
77 g = (data[x] >> default_green_shift) & 0xFF;
78 b = (data[x] >> default_blue_shift) & 0xFF;
79 data[x] = (r << render_red_offset) + (g << render_green_offset) +
80 (b << render_blue_offset);
81 }
82 data += im->width;
83 }
84 }
85 break;
86 case 16:
87 for (y = 0; y < im->height; y++) {
88 for (x = 0; x < im->width; x++) {
89 r = (data[x] >> default_red_shift) & 0xFF;
90 r = r >> render_red_shift;
91 g = (data[x] >> default_green_shift) & 0xFF;
92 g = g >> render_green_shift;
93 b = (data[x] >> default_blue_shift) & 0xFF;
94 b = b >> render_blue_shift;
95 p[x] = (r << render_red_offset)
96 + (g << render_green_offset)
97 + (b << render_blue_offset);
98 }
99 data += im->width;
100 p += im->bytes_per_line/2;
101 }
102 break;
103 default:
104 g_message("your bit depth is currently unhandled\n");
105 }
106 }
This page took 0.037424 seconds and 4 git commands to generate.