1 /* BColors.cpp
2  * Copyright (C) 2018, 1019  Sven Jähnichen
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (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, see <https://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef BCOLORS_HPP_
19 #define BCOLORS_HPP_
20 
21 #include <stdint.h>
22 #include <vector>
23 #include <iostream>
24 
25 #ifndef LIMIT
26 #define LIMIT(val, min, max) (val < min ? min : (val < max ? val : max))
27 #endif /* LIMIT */
28 
29 #define CAIRO_RGBA(col) (col).getRed(), (col).getGreen(), (col).getBlue(), (col).getAlpha()
30 
31 namespace BColors
32 {
33 
34 /**
35  * Color states
36  */
37 typedef enum {
38 	NORMAL		= 0,
39 	ACTIVE		= 1,
40 	INACTIVE	= 2,
41 	OFF		= 3,
42 	USER_DEFINED	= 4
43 } State;
44 
45 /**
46  * Class BColors::Color
47  *
48  * Color management class. Uses relative (0.0 .. 1.0) color and alpha values
49  */
50 class Color
51 {
52 public:
53 	Color ();
54 	Color (const double red, const double green, const double blue, const double alpha);
55 	Color (const uint32_t red32, const uint32_t green32, const uint32_t blue32, const uint32_t alpha32);
56 
57 	bool operator== (const Color& that) const;
58 	bool operator!= (const Color& that) const;
59 
60 	/**
61 	 * Sets colors rgb and alpha of a BColors::Color
62 	 * @param red, green, blue, alpha Relative values (0.0 .. 1.0)
63 	 */
64 	void setRGBA (const double red, const double green, const double blue, const double alpha);
65 
66 	/**
67 	 * Sets colors rgb of a BColors::Color
68 	 * @param red, green, blue Relative values (0.0 .. 1.0)
69 	 */
70 	void setRGB (const double red, const double green, const double blue);
71 
72 	/**
73 	 * Sets alpha value of a BColors::Color
74 	 * @param alpha Relative value (0.0 .. 1.0)
75 	 */
76 	void setAlpha (const double alpha);
77 
78 	/**
79 	 * Gets red value of a BColors::Color
80 	 * @return Relative red value (0.0 .. 1.0)
81 	 */
82 	double getRed () const;
83 
84 	/**
85 	 * Gets green value of a BColors::Color
86 	 * @return Relative green value (0.0 .. 1.0)
87 	 */
88 	double getGreen () const;
89 
90 	/**
91 	 * Gets blue value of a BColors::Color
92 	 * @return Relative blue value (0.0 .. 1.0)
93 	 */
94 	double getBlue () const;
95 
96 	/**
97 	 * Gets alpha value of a BColors::Color
98 	 * @return Relative alpha value (0.0 .. 1.0)
99 	 */
100 	double getAlpha () const;
101 
102 	/**
103 	 * Compares the color of this object with the color of the given object by
104 	 * comparison of all RGB and alpha values.
105 	 * @param that Color to compare with
106 	 * @return 0, if both colors are equal
107 	 * 		   1, if the two colors are not equal
108 	 */
109 	int compare (const Color& that) const;
110 
111 	/**
112 	 * Changes the red, green and blue values of the BColors::Color by a given
113 	 * brightness.
114 	 * @param brightness Brightness ranging from -1.0 (full darkness => black
115 	 * 					 over 0.0 (normal => unchanged) to 1.0 (full brightness
116 	 * 					 => white)
117 	 */
118 	void applyBrightness (const double brightness);
119 
120 private:
121 	double red_, green_, blue_, alpha_;
122 };
123 /*
124  * End of class BColors::Color
125  *****************************************************************************/
126 
127 const Color white = Color (1.0, 1.0, 1.0, 1.0);
128 const Color black = Color (0.0, 0.0, 0.0, 1.0);
129 const Color red = Color (1.0, 0.0, 0.0, 1.0);
130 const Color green = Color (0.0, 1.0, 0.0, 1.0);
131 const Color blue = Color (0.0, 0.0, 1.0, 1.0);
132 const Color yellow = Color (1.0, 1.0, 0.0, 1.0);
133 const Color grey = Color (0.5, 0.5, 0.5, 1.0);
134 const Color lightred = Color (1.0, 0.5, 0.5, 1.0);
135 const Color darkred = Color (0.5, 0.0, 0.0, 1.0);
136 const Color lightgreen = Color (0.5, 1.0, 0.5, 1.0);
137 const Color darkgreen = Color (0.0, 0.5, 0.0, 1.0);
138 const Color lightblue = Color (0.5, 0.5, 1.0, 1.0);
139 const Color darkblue = Color (0.0, 0.0, 0.5, 1.0);
140 const Color lightgrey = Color (0.75, 0.75, 0.75, 1.0);
141 const Color darkgrey = Color (0.25, 0.25, 0.25, 1.0);
142 const Color darkdarkgrey = Color (0.1, 0.1, 0.1, 1.0);
143 const Color grey80 = Color (0.8, 0.8, 0.8, 1.0);
144 const Color grey60 = Color (0.6, 0.6, 0.6, 1.0);
145 const Color grey40 = Color (0.4, 0.4, 0.4, 1.0);
146 const Color grey20 = Color (0.2, 0.2, 0.2, 1.0);
147 const Color invisible = Color (0.0, 0.0, 0.0, 0.0);
148 
149 /**
150  * Class BColors::ColorSet
151  *
152  * Defines a set of colors for different states. I can be used in combination
153  * with BColors::Style.
154  */
155 class ColorSet
156 {
157 public:
158 	ColorSet ();
159 	ColorSet (const std::vector<Color> vectorOfColors);
160 
161 	bool operator== (const ColorSet& that) const;
162 	bool operator!= (const ColorSet& that) const;
163 
164 	/**
165 	 * Compares the colorset of this object with the color of the given object by
166 	 * comparison of all colors of this set.
167 	 * @param that Colorset to compare with
168 	 * @return 0, if both sets (content and order) are equal
169 	 * 		   1, if the two sets (content and order) are not equal
170 	 */
171 	int compare (const ColorSet& that) const;
172 
173 	/**
174 	 * Adds (or overwrites) a BColors::Color to the ColorSet
175 	 * @param state BColors::State of the color to be added to the set. The
176 	 * 				set will be extended automatically if needed.
177 	 * @param color BColors::Color to be added
178 	 */
179 	void addColor (const State state, const Color& color);
180 
181 	/**
182 	 * Removes a BColors::Color from the ColorSet
183 	 * @param state BColors::State of the color to be removed from the set. The
184 	 * 				set will be shrinked automatically if needed.
185 	 */
186 	void removeColor (const State state);
187 
188 	/**
189 	 * Gets a (pointer to) BColors::Color from the ColorSet
190 	 * @param state BColors::State of the color to be returned.
191 	 * @return Pointer to BColors::Color of the respective state or pointer to
192 	 * 		   a copy of BColors::invisible if the states color is not within
193 	 * 		   the ColorSet.
194 	 */
195 	Color* getColor (const State state);
196 
197 private:
198 	std::vector<Color> colors;
199 	Color noColor = invisible;
200 };
201 /*
202  * End of class BColors::ColorSet
203  *****************************************************************************/
204 
205 const ColorSet reds = ColorSet ({red, lightred, darkred, black});
206 const ColorSet greens = ColorSet ({green, lightgreen, darkgreen, black});
207 const ColorSet blues = ColorSet ({blue, lightblue, darkblue, black});
208 const ColorSet greys = ColorSet ({grey, lightgrey, darkgrey, black});
209 const ColorSet whites = ColorSet ({lightgrey, white, grey, black});
210 const ColorSet darks = ColorSet ({darkgrey, grey, darkdarkgrey, black});
211 const ColorSet lights = ColorSet ({lightgrey, white, grey, darkgrey});
212 
213 }
214 
215 #endif /* BCOLORS_HPP_ */
216