all repos — openbox @ 104c1a164b1e4e881e141d14263895401779d453

openbox fork - make it a bit more like ryudo

otk/color.hh (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
// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
#ifndef __color_hh
#define __color_hh

extern "C" {
#include <X11/Xlib.h>
}

#include <map>
#include <string>

namespace otk {

class Color {
public:
  Color(unsigned int _screen = ~(0u));
  Color(int _r, int _g, int _b, unsigned int _screen = ~(0u));
  Color(const std::string &_name, unsigned int _screen = ~(0u));
  ~Color(void);

  inline const std::string &name(void) const { return colorname; }

  inline int   red(void) const { return r; }
  inline int green(void) const { return g; }
  inline int  blue(void) const { return b; }
  void setRGB(int _r, int _g, int _b) {
    deallocate();
    r = _r;
    g = _g;
    b = _b;
  }

  inline unsigned int screen(void) const { return scrn; }
  void setScreen(unsigned int _screen = ~(0u));

  inline bool isAllocated(void) const { return allocated; }

  inline bool isValid(void) const { return r != -1 && g != -1 && b != -1; }

  unsigned long pixel(void) const;

  // operators
#ifndef SWIG
  Color &operator=(const Color &c);
#endif
  inline bool operator==(const Color &c) const
  { return (r == c.r && b == c.b && b == c.b); }
  inline bool operator!=(const Color &c) const
  { return (! operator==(c)); }

  static void cleanupColorCache(void);

private:
  void parseColorName(void);
  void allocate(void);
  void deallocate(void);

  bool allocated;
  int r, g, b;
  unsigned long p;
  unsigned int scrn;
  std::string colorname;

  // global color allocator/deallocator
  struct RGB {
    const int screen;
    const int r, g, b;

    RGB(void) : screen(~(0u)), r(-1), g(-1), b(-1) { }
    RGB(const int b, const int x, const int y, const int z)
      : screen(b), r(x), g(y), b(z) {}
    RGB(const RGB &x)
      : screen(x.screen), r(x.r), g(x.g), b(x.b) {}

    inline bool operator==(const RGB &x) const {
      return screen == x.screen &&
             r == x.r && g == x.g && b == x.b;
    }

    inline bool operator<(const RGB &x) const {
      unsigned long p1, p2;
      p1 = (screen << 24 | r << 16 | g << 8 | b) & 0x00ffffff;
      p2 = (x.screen << 24 | x.r << 16 | x.g << 8 | x.b) & 0x00ffffff;
      return p1 < p2;
    }
  };
  struct PixelRef {
    const unsigned long p;
    unsigned int count;
    inline PixelRef(void) : p(0), count(0) { }
    inline PixelRef(const unsigned long x) : p(x), count(1) { }
  };
  typedef std::map<RGB,PixelRef> ColorCache;
  typedef ColorCache::value_type ColorCacheItem;
  static ColorCache colorcache;
  static bool cleancache;
  static void doCacheCleanup(void);
};

}

#endif // __color_hh