otk_c/color.c (raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
// -*- mode: C; indent-tabs-mode: nil; c-basic-offset: 2; -*- #include "../config.h" #include "color.h" #include "display.h" #include "screeninfo.h" #ifdef HAVE_STDLIB_H # include <stdlib.h> #endif static Bool cleancache = False; static PyObject *colorcache = NULL; static void parseColorName(OtkColor *self, const char *name) { XColor xcol; // get rgb values from colorname xcol.red = 0; xcol.green = 0; xcol.blue = 0; xcol.pixel = 0; if (!XParseColor(OBDisplay->display, OtkDisplay_ScreenInfo(OBDisplay, self->screen)->colormap, name, &xcol)) { fprintf(stderr, "OtkColor: color parse error: \"%s\"\n", name); self->red = self->green = self->blue = 0; } else { self->red = xcol.red >> 8; self->green = xcol.green >> 8; self->blue = xcol.blue >> 8; } } static void doCacheCleanup() { unsigned long *pixels; int i, ppos; unsigned int count; PyObject *key; // this is a color too, but i dont need to use it as such OtkColor *color; // ### TODO - support multiple displays! if (!PyDict_Size(colorcache)) return; // nothing to do pixels = malloc(sizeof(unsigned long) * PyDict_Size(colorcache)); for (i = 0; i < ScreenCount(OBDisplay->display); i++) { count = 0; ppos = 0; while (PyDict_Next(colorcache, &ppos, &key, (PyObject**)&color)) { // get the screen from the hash if (color->screen != i) continue; // wrong screen // does someone other than the cache have a reference? (the cache gets 2) if (color->ob_refcnt > 2) continue; pixels[count++] = color->pixel; PyDict_DelItem(colorcache, key); --ppos; // back up one in the iteration } if (count > 0) XFreeColors(OBDisplay->display, OtkDisplay_ScreenInfo(OBDisplay, i)->colormap, pixels, count, 0); } free(pixels); cleancache = False; } static void allocate(OtkColor *self) { XColor xcol; // allocate color from rgb values xcol.red = self->red | self->red << 8; xcol.green = self->green | self->green << 8; xcol.blue = self->blue | self->blue << 8; xcol.pixel = 0; if (!XAllocColor(OBDisplay->display, OtkDisplay_ScreenInfo(OBDisplay, self->screen)->colormap, &xcol)) { fprintf(stderr, "OtkColor: color alloc error: rgb:%x/%x/%x\n", self->red, self->green, self->blue); xcol.pixel = 0; } self->pixel = xcol.pixel; if (cleancache) doCacheCleanup(); } PyObject *OtkColor_FromRGB(int r, int g, int b, int screen) { OtkColor *self = PyObject_New(OtkColor, &OtkColor_Type); PyObject *cached; assert(screen >= 0); assert(r >= 0); assert(g >= 0); assert(b >= 0); assert(r <= 0xff); assert(g <= 0xff); assert(b <= 0xff); if (!colorcache) colorcache = PyDict_New(); self->red = r; self->green = g; self->blue = b; self->screen = screen; // does this color already exist in the cache? cached = PyDict_GetItem(colorcache, (PyObject*)self); if (cached) { Py_INCREF(cached); return cached; } // add it to the cache PyDict_SetItem(colorcache, (PyObject*)self, (PyObject*)self); allocate(self); return (PyObject*)self; } PyObject *OtkColor_FromName(const char *name, int screen) { OtkColor *self = PyObject_New(OtkColor, &OtkColor_Type); PyObject *cached; assert(screen >= 0); assert(name); if (!colorcache) colorcache = PyDict_New(); self->red = -1; self->green = -1; self->blue = -1; self->screen = screen; parseColorName(self, name); // does this color already exist in the cache? cached = PyDict_GetItem(colorcache, (PyObject*)self); if (cached) { Py_INCREF(cached); return cached; } // add it to the cache PyDict_SetItem(colorcache, (PyObject*)self, (PyObject*)self); allocate(self); return (PyObject*)self; } void OtkColor_CleanupColorCache() { cleancache = True; } static void otkcolor_dealloc(OtkColor* self) { // when this is called, the color has already been cleaned out of the cache PyObject_Del((PyObject*)self); } static int otkcolor_compare(OtkColor *c1, OtkColor *c2) { long result; unsigned long p1, p2; p1 = c1->red << 16 | c1->green << 8 | c1->blue; p2 = c2->red << 16 | c2->green << 8 | c2->blue; if (p1 < p2) result = -1; else if (p1 > p2) result = 1; else result = 0; return result; } static PyObject *otkcolor_repr(OtkColor *self) { return PyString_FromFormat("rgb:%x/%x/%x", self->red, self->green, self->blue); } static long otkcolor_hash(OtkColor *self) { return self->screen << 24 | self->red << 16 | self->green << 8 | self->blue; } PyTypeObject OtkColor_Type = { PyObject_HEAD_INIT(NULL) 0, "OtkColor", sizeof(OtkColor), 0, (destructor)otkcolor_dealloc, /*tp_dealloc*/ 0, /*tp_print*/ 0, /*tp_getattr*/ 0, /*tp_setattr*/ (cmpfunc)otkcolor_compare, /*tp_compare*/ (reprfunc)otkcolor_repr, /*tp_repr*/ 0, /*tp_as_number*/ 0, /*tp_as_sequence*/ 0, /*tp_as_mapping*/ (hashfunc)otkcolor_hash, /*tp_hash */ }; |