1 /* AbiSource Program Utilities
2  * Copyright (C) 1998 AbiSource, Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA.
18  */
19 
20 #ifndef UTCOLOR_H
21 #define UTCOLOR_H
22 
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 
27 /* pre-emptive dismissal; ut_types.h is needed by just about everything,
28  * so even if it's commented out in-file that's still a lot of work for
29  * the preprocessor to do...
30  */
31 #ifndef UT_TYPES_H
32 #include "ut_types.h"
33 #endif
34 
35 
36 #include <string>
37 
38 // ----------------------------------------------------------------
39 #define UT_RGBCOLOR_PROXIMITY 45
40 
41 class ABI_EXPORT UT_ColorPatImpl
42 {
43 public:
44     virtual ~UT_ColorPatImpl();
45     virtual UT_ColorPatImpl * clone() const= 0;
46 };
47 
48 class ABI_EXPORT UT_RGBColor
49 {
50 public:
51 	UT_RGBColor();
52 	UT_RGBColor(unsigned char, unsigned char, unsigned char, bool bTransparent = false);
53 	UT_RGBColor(const UT_RGBColor&);
54     // take ownership
55     UT_RGBColor(const UT_ColorPatImpl * pattern);
56     ~UT_RGBColor();
57 	bool operator != (const UT_RGBColor &op1)
58 	{
59 		return (op1.m_red != m_red || op1.m_grn != m_grn || op1.m_blu != m_blu);
60 	}
61 
62 	bool operator == (const UT_RGBColor &op1)
63 	{
64 		return (op1.m_red == m_red && op1.m_grn == m_grn && op1.m_blu == m_blu);
65 	}
66 
67 	// returns true if the two colors are near each other in the RGB space
68 	bool operator %= (const UT_RGBColor &op1)
69 	{
70 		UT_uint32 iDiff = abs(m_red - op1.m_red) + abs(m_grn - op1.m_grn) + abs(m_blu - op1.m_blu);
71 		return (iDiff < UT_RGBCOLOR_PROXIMITY);
72 	}
73 
74 	UT_RGBColor & operator ^= (const UT_RGBColor &op1)
75 	{
76 		m_red ^= op1.m_red;
77 		m_grn ^= op1.m_grn;
78 		m_blu ^= op1.m_blu;
79 		return *this;
80 	}
81 
82 	UT_RGBColor & operator += (const unsigned char inc)
83 	{
84 		m_red += inc;
85 		m_grn += inc;
86 		m_blu += inc;
87 		return *this;
88 	}
89 
90 	UT_RGBColor & operator += (const  UT_RGBColor &inc)
91 	{
92 		m_red += inc.m_red;
93 		m_grn += inc.m_grn;
94 		m_blu += inc.m_blu;
95 		return *this;
96 	}
97 
98 	UT_RGBColor & operator -= (const  UT_RGBColor &inc)
99 	{
100 		m_red -= inc.m_red;
101 		m_grn -= inc.m_grn;
102 		m_blu -= inc.m_blu;
103 		return *this;
104 	}
105 
106     UT_RGBColor & operator=(const  UT_RGBColor &inc);
107 
isTransparent()108 	bool isTransparent() const
109 	{
110 		return m_bIsTransparent;
111 	}
112 	bool setColor(const char * pszColor);
113 
isPattern()114     bool isPattern() const
115     {
116         return m_patImpl != NULL;
117     }
pattern()118     const UT_ColorPatImpl *pattern() const
119     {
120         return m_patImpl;
121     }
122 	// take ownership
setPattern(const UT_ColorPatImpl * p)123 	void setPattern(const UT_ColorPatImpl *p)
124 	{
125 		if(m_patImpl) {
126 			delete m_patImpl;
127 		}
128 		m_patImpl = p;
129 	}
130 
131 	unsigned char m_red;
132 	unsigned char m_grn;
133 	unsigned char m_blu;
134 	bool m_bIsTransparent;
135 private:
136     const UT_ColorPatImpl * m_patImpl;
137 };
138 
139 void UT_setColor(UT_RGBColor & col, unsigned char r, unsigned char g, unsigned char b, bool bTransparent = false);
140 ABI_EXPORT void UT_parseColor(const char*, UT_RGBColor&);
141 ABI_EXPORT std::string UT_colorToHex(const char*, bool bPrefix = false);
142 
143 class ABI_EXPORT UT_HashColor
144 {
145 private:
146 	char m_colorBuffer[8]; // format: "" or "#abc123" (i.e., '#' + 6 lower-case hex digits)
147 
148 public:
149 	UT_HashColor ();
150 	~UT_HashColor ();
151 
c_str()152 	const char * c_str() const
153 	{ return m_colorBuffer; }
154 	/* The following 5 functions return a pointer to m_colorBuffer on success,
155 	 * or 0 on failure (invalid or unknown color).
156 	 */
157 	const char * setColor (unsigned char r, unsigned char g, unsigned char b);
setColor(const UT_RGBColor & color)158 	const char * setColor (const UT_RGBColor & color) { return setColor (color.m_red, color.m_grn, color.m_blu); }
159 	const char * setColor (const char * color); // try match hash (e.g., "#C01bB7") or name (e.g., "turquoise")
160 	const char * lookupNamedColor (const char * color_name); // "Orange" or "blue" or "LightGoldenRodYellow" or...
161 	const char * setHashIfValid (const char * color_hash);   // "ff0013" or "AD5FE6" or... (NOTE: no '#')
162 
163 	const UT_RGBColor rgb (); // Call this *if* setColor () succeeds; otherwise defaults to black.
164 };
165 
166 
167 // Hack so we get AbiNativeWidget with an xp include
168 #ifdef TOOLKIT_GTK_ALL
169 #include "ut_unixColor.h"
170 #else
171 // TODO maintainers please fix their platform
172 typedef void AbiNativeWidget;
173 #endif
174 
175 #endif /* UTCOLOR_H */
176