]> Dogcows Code - chaz/openbox/blob - render/color.c
dont fux self in color_free if the color is NULL
[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
7 XColor *pseudo_colors;
8 int pseudo_bpc;
9
10 void color_allocate_gc(color_rgb *in)
11 {
12 XGCValues gcv;
13
14 gcv.foreground = in->pixel;
15 gcv.cap_style = CapProjecting;
16 in->gc = XCreateGC(ob_display, ob_root, GCForeground | GCCapStyle, &gcv);
17 }
18
19 color_rgb *color_parse(char *colorname)
20 {
21 XColor xcol;
22
23 g_assert(colorname != NULL);
24 /* get rgb values from colorname */
25
26 xcol.red = 0;
27 xcol.green = 0;
28 xcol.blue = 0;
29 xcol.pixel = 0;
30 if (!XParseColor(ob_display, render_colormap, colorname, &xcol)) {
31 g_warning("unable to parse color '%s'", colorname);
32 return NULL;
33 }
34 return color_new(xcol.red >> 8, xcol.green >> 8, xcol.blue >> 8);
35 }
36
37 color_rgb *color_new(int r, int g, int b)
38 {
39 /* this should be replaced with something far cooler */
40 color_rgb *out;
41 XColor xcol;
42 xcol.red = (r << 8) | r;
43 xcol.green = (g << 8) | g;
44 xcol.blue = (b << 8) | b;
45 if (XAllocColor(ob_display, render_colormap, &xcol)) {
46 out = g_new(color_rgb, 1);
47 out->r = xcol.red >> 8;
48 out->g = xcol.green >> 8;
49 out->b = xcol.blue >> 8;
50 out->gc = None;
51 out->pixel = xcol.pixel;
52 return out;
53 }
54 return NULL;
55 }
56
57 /*XXX same color could be pointed to twice, this might have to be a refcount*/
58
59 void color_free(color_rgb *c)
60 {
61 if (c != NULL) {
62 if (c->gc != None)
63 XFreeGC(ob_display, c->gc);
64 g_free(c);
65 }
66 }
67
68 void reduce_depth(pixel32 *data, XImage *im)
69 {
70 int r, g, b;
71 int x,y;
72 pixel32 *p32 = (pixel32 *) im->data;
73 pixel16 *p16 = (pixel16 *) im->data;
74 unsigned char *p8 = (unsigned char *)im->data;
75 switch (im->bits_per_pixel) {
76 case 32:
77 if ((render_red_offset != default_red_shift) ||
78 (render_blue_offset != default_blue_shift) ||
79 (render_green_offset != default_green_shift)) {
80 for (y = 0; y < im->height; y++) {
81 for (x = 0; x < im->width; x++) {
82 r = (data[x] >> default_red_shift) & 0xFF;
83 g = (data[x] >> default_green_shift) & 0xFF;
84 b = (data[x] >> default_blue_shift) & 0xFF;
85 p32[x] = (r << render_red_offset)
86 + (g << render_green_offset)
87 + (b << render_blue_offset);
88 }
89 data += im->width;
90 p32 += im->width;
91 }
92 } else im->data = (char*) data;
93 break;
94 case 16:
95 for (y = 0; y < im->height; y++) {
96 for (x = 0; x < im->width; x++) {
97 r = (data[x] >> default_red_shift) & 0xFF;
98 r = r >> render_red_shift;
99 g = (data[x] >> default_green_shift) & 0xFF;
100 g = g >> render_green_shift;
101 b = (data[x] >> default_blue_shift) & 0xFF;
102 b = b >> render_blue_shift;
103 p16[x] = (r << render_red_offset)
104 + (g << render_green_offset)
105 + (b << render_blue_offset);
106 }
107 data += im->width;
108 p16 += im->bytes_per_line/2;
109 }
110 break;
111 case 8:
112 g_assert(render_visual->class != TrueColor);
113 for (y = 0; y < im->height; y++) {
114 for (x = 0; x < im->width; x++) {
115 p8[x] = pickColor(data[x] >> default_red_shift,
116 data[x] >> default_green_shift,
117 data[x] >> default_blue_shift)->pixel;
118 }
119 data += im->width;
120 p8 += im->bytes_per_line;
121 }
122
123 break;
124 default:
125 g_message("your bit depth is currently unhandled\n");
126 }
127 }
128 XColor *pickColor(int r, int g, int b)
129 {
130 r = (r & 0xff) >> (8-pseudo_bpc);
131 g = (g & 0xff) >> (8-pseudo_bpc);
132 b = (b & 0xff) >> (8-pseudo_bpc);
133 return &pseudo_colors[(r << (2*pseudo_bpc)) + (g << (1*pseudo_bpc)) + b];
134 }
This page took 0.041391 seconds and 5 git commands to generate.