1 #ifndef __YCOLOR_H 2 #define __YCOLOR_H 3 4 class YPixel; 5 class YColorName; 6 7 class YColor { 8 public: YColor()9 YColor() : fPixel(nullptr) { } YColor(const char * s)10 explicit YColor(const char* s) : fPixel(nullptr) { if (s) alloc(s, 0); } 11 YColor(unsigned char r, unsigned char g, unsigned char b, unsigned char a = 0); YColor(const YColor & c)12 YColor(const YColor& c) : fPixel(c.fPixel) { } 13 YColor& operator=(const YColor& c) { fPixel = c.fPixel; return *this; } 14 15 void alloc(const char* name, int opacity); 16 unsigned long pixel(); 17 18 YColor darker(); 19 YColor brighter(); 20 YColor& reverse(); 21 22 bool operator==(YColor& c); 23 bool operator!=(YColor& c); 24 operator bool() { return fPixel; } 25 operator bool() const { return fPixel; } release()26 void release() { fPixel = nullptr; } 27 28 unsigned char red(); 29 unsigned char green(); 30 unsigned char blue(); 31 unsigned char alpha(); 32 33 #ifdef CONFIG_XFREETYPE 34 struct _XftColor* xftColor(); 35 #endif 36 37 static YColorName black; 38 static YColorName white; 39 40 private: YColor(YPixel * pixel)41 YColor(YPixel* pixel) : fPixel(pixel) { } 42 void alloc(const char* name); 43 44 YPixel* fPixel; 45 46 friend class YColorName; 47 friend class YPixel; 48 }; 49 50 class YColorName { 51 public: fName(clr)52 YColorName(const char* clr = nullptr) : fName(clr), fNamePtr(&fName) { } YColorName(const char ** clrp)53 YColorName(const char** clrp) : fName(nullptr), fNamePtr(clrp) { } 54 name()55 const char* name() const { return *fNamePtr; } color()56 YColor color() { if (!fColor) alloc(); return fColor; } darker()57 YColor darker() { return color().darker(); } brighter()58 YColor brighter() { return color().brighter(); } pixel()59 unsigned long pixel() { return color().pixel(); } release()60 void release() { fColor.release(); } reverse()61 void reverse() { if (!fColor) alloc(); fColor.reverse(); } 62 63 operator bool() { return name() && *name(); } YColor()64 operator YColor() { return color(); } 65 YColor* operator->() { if (!fColor) alloc(); return &fColor; } 66 67 void operator=(const char* clr) { fName = clr; fNamePtr = &fName; release(); } 68 void operator=(const char** clrp) { fName = nullptr; fNamePtr = clrp; release(); } 69 70 private: alloc()71 void alloc() { fColor.alloc(name(), 0); } 72 73 const char* fName; 74 const char** fNamePtr; 75 YColor fColor; 76 }; 77 78 #endif 79 80 // vim: set sw=4 ts=4 et: 81