]> Dogcows Code - chaz/openbox/commitdiff
allocate colors in pseudocolor from the map we allocate in the rendercontrol
authorDana Jansens <danakj@orodu.net>
Fri, 14 Feb 2003 08:01:44 +0000 (08:01 +0000)
committerDana Jansens <danakj@orodu.net>
Fri, 14 Feb 2003 08:01:44 +0000 (08:01 +0000)
otk/pseudorendercontrol.cc
otk/pseudorendercontrol.hh
otk/rendercolor.cc
otk/rendercontrol.hh
otk/truerendercontrol.cc
otk/truerendercontrol.hh

index 89e95ab6eec20b327ba4841b66f39eb59d576da4..29636ee9ac21fc4b7e5e4e91a971890ddfb9279c 100644 (file)
@@ -93,9 +93,8 @@ int tr, tg, tb;
       _colors[i].pixel = icolors[close].pixel;
 
       // try alloc this closest color, it had better succeed!
-      if (XAllocColor(**display, info->colormap(), &_colors[i])) {
+      if (XAllocColor(**display, info->colormap(), &_colors[i]))
         _colors[i].flags = DoRed|DoGreen|DoBlue; // mark as alloced
-      }
       else
         assert(false); // wtf has gone wrong, its already alloced for chissake!
     }
@@ -135,4 +134,44 @@ void PseudoRenderControl::reduceDepth(Surface &sf, XImage *im) const
 
 }
 
+void PseudoRenderControl::allocateColor(XColor *color) const
+{
+  const ScreenInfo *info = display->screenInfo(_screen);
+  int depth = info->depth();
+
+  // get the allocated values from the X server (only the first 256 XXX why!?)
+  XColor icolors[256];
+  int incolors = (((1 << depth) > 256) ? 256 : (1 << depth));
+  for (int i = 0; i < incolors; i++)
+    icolors[i].pixel = i;
+  XQueryColors(**display, info->colormap(), icolors, incolors);
+
+  unsigned long closest = 0xffffffff, close = 0;
+  for (int ii = 0; ii < incolors; ii++) {
+    // find deviations
+    int r = (color->red - icolors[ii].red) & 0xff;
+    int g = (color->green - icolors[ii].green) & 0xff;
+    int b = (color->blue - icolors[ii].blue) & 0xff;
+    // find a weighted absolute deviation
+    unsigned long dev = (r * r) + (g * g) + (b * b);
+
+    if (dev < closest) {
+      closest = dev;
+      close = ii;
+    }
+  }
+
+  color->red = icolors[close].red;
+  color->green = icolors[close].green;
+  color->blue = icolors[close].blue;
+  color->pixel = icolors[close].pixel;
+
+  // try alloc this closest color, it had better succeed!
+  if (XAllocColor(**display, info->colormap(), color)) {
+    color->flags = DoRed|DoGreen|DoBlue; // mark as alloced
+  }
+  else
+    assert(false); // wtf has gone wrong, its already alloced for chissake!
+}
+
 }
index a52e7cc3fb5fc4d174e410fc0b69fd926d8b4747..402190c53d385bf33eb985fa75175e1d91159ef7 100644 (file)
@@ -18,6 +18,7 @@ public:
   PseudoRenderControl(int screen);
   virtual ~PseudoRenderControl();
 
+  virtual void allocateColor(XColor *color) const;
 };
 
 }
index 448b3e109610c3fb00f8dc322b0f9d383a98a7d3..e5cbb7cba45d8b99a55387adde6a59b27c5729f9 100644 (file)
@@ -5,6 +5,7 @@
 #include "rendercolor.hh"
 #include "display.hh"
 #include "screeninfo.hh"
+#include "rendercontrol.hh"
 
 #include <cstdio>
 
@@ -64,14 +65,9 @@ void RenderColor::create() const
     xcol.red = (_red << 8) | _red;
     xcol.green = (_green << 8) | _green;
     xcol.blue = (_blue << 8) | _blue;
-    xcol.pixel = 0;
-
-    if (!XAllocColor(**display, info->colormap(), &xcol)) {
-      fprintf(stderr, "RenderColor: color alloc error: rgb:%x/%x/%x\n",
-             _red, _green, _blue);
-      xcol.pixel = 0;
-    } else
-      _allocated = true;
+
+    display->renderControl(_screen)->allocateColor(&xcol);
+    _allocated = true;
 
     _pixel = xcol.pixel;
     gcv.foreground = _pixel;
index 72ad176fa1b6b87459279e8c078df9e547b8f102..01642824b55afc5487fded4a7b0b35a0f5982e50 100644 (file)
@@ -68,6 +68,8 @@ public:
   //! Draws a PixmapMask with a specified color onto a Surface
   virtual void drawMask(Surface &sf, const RenderColor &color,
                         const PixmapMask &mask) const;
+
+  virtual void allocateColor(XColor *color) const = 0;
 };
 
 }
index 1d5b8dc44bc37b3e327a699e3fda5a89c54aebaa..2f1b710e1a7ef92416cd157baa5ee19bca2656a2 100644 (file)
@@ -99,4 +99,14 @@ void TrueRenderControl::reduceDepth(Surface &sf, XImage *im) const
   }
 }
 
+void TrueRenderControl::allocateColor(XColor *color) const
+{
+  const ScreenInfo *info = display->screenInfo(_screen);
+  if (!XAllocColor(**display, info->colormap(), color)) {
+    fprintf(stderr, "TrueRenderControl: color alloc error: rgb:%x/%x/%x\n",
+            color->red & 0xff, color->green & 0xff, color->blue & 0xff);
+    color->pixel = 0;
+  }
+}
+
 }
index 1bfe841540bd2415ec203c057ddb9d409a7dda7d..36dbe2441165b8946a79cb3a3e459b7cd0b8535f 100644 (file)
@@ -26,6 +26,8 @@ private:
 public:
   TrueRenderControl(int screen);
   virtual ~TrueRenderControl();
+
+  virtual void allocateColor(XColor *color) const;
 };
 
 }
This page took 0.026598 seconds and 4 git commands to generate.