1 | /* |
---|
2 | This file is part of Konsole, KDE's terminal. |
---|
3 | |
---|
4 | Copyright 2007-2008 by Robert Knight <robertknight@gmail.com> |
---|
5 | Copyright 1997,1998 by Lars Doelle <lars.doelle@on-line.de> |
---|
6 | |
---|
7 | This program is free software; you can redistribute it and/or modify |
---|
8 | it under the terms of the GNU General Public License as published by |
---|
9 | the Free Software Foundation; either version 2 of the License, or |
---|
10 | (at your option) any later version. |
---|
11 | |
---|
12 | This program is distributed in the hope that it will be useful, |
---|
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | GNU General Public License for more details. |
---|
16 | |
---|
17 | You should have received a copy of the GNU General Public License |
---|
18 | along with this program; if not, write to the Free Software |
---|
19 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
---|
20 | 02110-1301 USA. |
---|
21 | */ |
---|
22 | |
---|
23 | #ifndef CHARACTERCOLOR_H |
---|
24 | #define CHARACTERCOLOR_H |
---|
25 | |
---|
26 | // Qt |
---|
27 | #include <QColor> |
---|
28 | |
---|
29 | //#include <kdemacros.h> |
---|
30 | #define KDE_NO_EXPORT |
---|
31 | |
---|
32 | namespace Konsole |
---|
33 | { |
---|
34 | |
---|
35 | /** |
---|
36 | * An entry in a terminal display's color palette. |
---|
37 | * |
---|
38 | * A color palette is an array of 16 ColorEntry instances which map |
---|
39 | * system color indexes (from 0 to 15) into actual colors. |
---|
40 | * |
---|
41 | * Each entry can be set as bold, in which case any text |
---|
42 | * drawn using the color should be drawn in bold. |
---|
43 | * |
---|
44 | * Each entry can also be transparent, in which case the terminal |
---|
45 | * display should avoid drawing the background for any characters |
---|
46 | * using the entry as a background. |
---|
47 | */ |
---|
48 | class ColorEntry |
---|
49 | { |
---|
50 | public: |
---|
51 | /** Specifies the weight to use when drawing text with this color. */ |
---|
52 | enum FontWeight |
---|
53 | { |
---|
54 | /** Always draw text in this color with a bold weight. */ |
---|
55 | Bold, |
---|
56 | /** Always draw text in this color with a normal weight. */ |
---|
57 | Normal, |
---|
58 | /** |
---|
59 | * Use the current font weight set by the terminal application. |
---|
60 | * This is the default behavior. |
---|
61 | */ |
---|
62 | UseCurrentFormat |
---|
63 | }; |
---|
64 | |
---|
65 | /** |
---|
66 | * Constructs a new color palette entry. |
---|
67 | * |
---|
68 | * @param c The color value for this entry. |
---|
69 | * @param tr Specifies that the color should be transparent when used as a background color. |
---|
70 | * @param weight Specifies the font weight to use when drawing text with this color. |
---|
71 | */ |
---|
72 | ColorEntry(QColor c, bool tr, FontWeight weight = UseCurrentFormat) |
---|
73 | : color(c), transparent(tr), fontWeight(weight) {} |
---|
74 | |
---|
75 | /** |
---|
76 | * Constructs a new color palette entry with an undefined color, and |
---|
77 | * with the transparent and bold flags set to false. |
---|
78 | */ |
---|
79 | ColorEntry() : transparent(false), fontWeight(UseCurrentFormat) {} |
---|
80 | |
---|
81 | /** The color value of this entry for display. */ |
---|
82 | QColor color; |
---|
83 | |
---|
84 | /** |
---|
85 | * If true character backgrounds using this color should be transparent. |
---|
86 | * This is not applicable when the color is used to render text. |
---|
87 | */ |
---|
88 | bool transparent; |
---|
89 | /** |
---|
90 | * Specifies the font weight to use when drawing text with this color. |
---|
91 | * This is not applicable when the color is used to draw a character's background. |
---|
92 | */ |
---|
93 | FontWeight fontWeight; |
---|
94 | }; |
---|
95 | |
---|
96 | |
---|
97 | // Attributed Character Representations /////////////////////////////// |
---|
98 | |
---|
99 | // Colors |
---|
100 | |
---|
101 | #define BASE_COLORS (2+8) |
---|
102 | #define INTENSITIES 2 |
---|
103 | #define TABLE_COLORS (INTENSITIES*BASE_COLORS) |
---|
104 | |
---|
105 | #define DEFAULT_FORE_COLOR 0 |
---|
106 | #define DEFAULT_BACK_COLOR 1 |
---|
107 | |
---|
108 | //a standard set of colors using black text on a white background. |
---|
109 | //defined in TerminalDisplay.cpp |
---|
110 | |
---|
111 | extern const ColorEntry base_color_table[TABLE_COLORS] KDE_NO_EXPORT; |
---|
112 | |
---|
113 | /* CharacterColor is a union of the various color spaces. |
---|
114 | |
---|
115 | Assignment is as follows: |
---|
116 | |
---|
117 | Type - Space - Values |
---|
118 | |
---|
119 | 0 - Undefined - u: 0, v:0 w:0 |
---|
120 | 1 - Default - u: 0..1 v:intense w:0 |
---|
121 | 2 - System - u: 0..7 v:intense w:0 |
---|
122 | 3 - Index(256) - u: 16..255 v:0 w:0 |
---|
123 | 4 - RGB - u: 0..255 v:0..256 w:0..256 |
---|
124 | |
---|
125 | Default colour space has two separate colours, namely |
---|
126 | default foreground and default background colour. |
---|
127 | */ |
---|
128 | |
---|
129 | #define COLOR_SPACE_UNDEFINED 0 |
---|
130 | #define COLOR_SPACE_DEFAULT 1 |
---|
131 | #define COLOR_SPACE_SYSTEM 2 |
---|
132 | #define COLOR_SPACE_256 3 |
---|
133 | #define COLOR_SPACE_RGB 4 |
---|
134 | |
---|
135 | /** |
---|
136 | * Describes the color of a single character in the terminal. |
---|
137 | */ |
---|
138 | class CharacterColor |
---|
139 | { |
---|
140 | friend class Character; |
---|
141 | |
---|
142 | public: |
---|
143 | /** Constructs a new CharacterColor whose color and color space are undefined. */ |
---|
144 | CharacterColor() |
---|
145 | : _colorSpace(COLOR_SPACE_UNDEFINED), |
---|
146 | _u(0), |
---|
147 | _v(0), |
---|
148 | _w(0) |
---|
149 | {} |
---|
150 | |
---|
151 | /** |
---|
152 | * Constructs a new CharacterColor using the specified @p colorSpace and with |
---|
153 | * color value @p co |
---|
154 | * |
---|
155 | * The meaning of @p co depends on the @p colorSpace used. |
---|
156 | * |
---|
157 | * TODO : Document how @p co relates to @p colorSpace |
---|
158 | * |
---|
159 | * TODO : Add documentation about available color spaces. |
---|
160 | */ |
---|
161 | CharacterColor(quint8 colorSpace, int co) |
---|
162 | : _colorSpace(colorSpace), |
---|
163 | _u(0), |
---|
164 | _v(0), |
---|
165 | _w(0) |
---|
166 | { |
---|
167 | switch (colorSpace) |
---|
168 | { |
---|
169 | case COLOR_SPACE_DEFAULT: |
---|
170 | _u = co & 1; |
---|
171 | break; |
---|
172 | case COLOR_SPACE_SYSTEM: |
---|
173 | _u = co & 7; |
---|
174 | _v = (co >> 3) & 1; |
---|
175 | break; |
---|
176 | case COLOR_SPACE_256: |
---|
177 | _u = co & 255; |
---|
178 | break; |
---|
179 | case COLOR_SPACE_RGB: |
---|
180 | _u = co >> 16; |
---|
181 | _v = co >> 8; |
---|
182 | _w = co; |
---|
183 | break; |
---|
184 | default: |
---|
185 | _colorSpace = COLOR_SPACE_UNDEFINED; |
---|
186 | } |
---|
187 | } |
---|
188 | |
---|
189 | /** |
---|
190 | * Returns true if this character color entry is valid. |
---|
191 | */ |
---|
192 | bool isValid() const |
---|
193 | { |
---|
194 | return _colorSpace != COLOR_SPACE_UNDEFINED; |
---|
195 | } |
---|
196 | |
---|
197 | /** |
---|
198 | * Set the value of this color from a normal system color to the corresponding intensive |
---|
199 | * system color if it's not already an intensive system color. |
---|
200 | * |
---|
201 | * This is only applicable if the color is using the COLOR_SPACE_DEFAULT or COLOR_SPACE_SYSTEM |
---|
202 | * color spaces. |
---|
203 | */ |
---|
204 | void setIntensive(); |
---|
205 | |
---|
206 | /** |
---|
207 | * Returns the color within the specified color @p palette |
---|
208 | * |
---|
209 | * The @p palette is only used if this color is one of the 16 system colors, otherwise |
---|
210 | * it is ignored. |
---|
211 | */ |
---|
212 | QColor color(const ColorEntry* palette) const; |
---|
213 | |
---|
214 | /** |
---|
215 | * Compares two colors and returns true if they represent the same color value and |
---|
216 | * use the same color space. |
---|
217 | */ |
---|
218 | friend bool operator == (const CharacterColor& a, const CharacterColor& b); |
---|
219 | /** |
---|
220 | * Compares two colors and returns true if they represent different color values |
---|
221 | * or use different color spaces. |
---|
222 | */ |
---|
223 | friend bool operator != (const CharacterColor& a, const CharacterColor& b); |
---|
224 | |
---|
225 | private: |
---|
226 | quint8 _colorSpace; |
---|
227 | |
---|
228 | // bytes storing the character color |
---|
229 | quint8 _u; |
---|
230 | quint8 _v; |
---|
231 | quint8 _w; |
---|
232 | }; |
---|
233 | |
---|
234 | inline bool operator == (const CharacterColor& a, const CharacterColor& b) |
---|
235 | { |
---|
236 | return a._colorSpace == b._colorSpace && |
---|
237 | a._u == b._u && |
---|
238 | a._v == b._v && |
---|
239 | a._w == b._w; |
---|
240 | } |
---|
241 | inline bool operator != (const CharacterColor& a, const CharacterColor& b) |
---|
242 | { |
---|
243 | return !operator==(a,b); |
---|
244 | } |
---|
245 | |
---|
246 | inline const QColor color256(quint8 u, const ColorEntry* base) |
---|
247 | { |
---|
248 | // 0.. 16: system colors |
---|
249 | if (u < 8) return base[u+2 ].color; |
---|
250 | u -= 8; |
---|
251 | if (u < 8) return base[u+2+BASE_COLORS].color; |
---|
252 | u -= 8; |
---|
253 | |
---|
254 | // 16..231: 6x6x6 rgb color cube |
---|
255 | if (u < 216) return QColor(((u/36)%6) ? (40*((u/36)%6)+55) : 0, |
---|
256 | ((u/ 6)%6) ? (40*((u/ 6)%6)+55) : 0, |
---|
257 | ((u/ 1)%6) ? (40*((u/ 1)%6)+55) : 0); |
---|
258 | u -= 216; |
---|
259 | |
---|
260 | // 232..255: gray, leaving out black and white |
---|
261 | int gray = u*10+8; return QColor(gray,gray,gray); |
---|
262 | } |
---|
263 | |
---|
264 | inline QColor CharacterColor::color(const ColorEntry* base) const |
---|
265 | { |
---|
266 | switch (_colorSpace) |
---|
267 | { |
---|
268 | case COLOR_SPACE_DEFAULT: return base[_u+0+(_v?BASE_COLORS:0)].color; |
---|
269 | case COLOR_SPACE_SYSTEM: return base[_u+2+(_v?BASE_COLORS:0)].color; |
---|
270 | case COLOR_SPACE_256: return color256(_u,base); |
---|
271 | case COLOR_SPACE_RGB: return {_u,_v,_w}; |
---|
272 | case COLOR_SPACE_UNDEFINED: return QColor(); |
---|
273 | } |
---|
274 | |
---|
275 | Q_ASSERT(false); // invalid color space |
---|
276 | |
---|
277 | return QColor(); |
---|
278 | } |
---|
279 | |
---|
280 | inline void CharacterColor::setIntensive() |
---|
281 | { |
---|
282 | if (_colorSpace == COLOR_SPACE_SYSTEM || _colorSpace == COLOR_SPACE_DEFAULT) |
---|
283 | { |
---|
284 | _v = 1; |
---|
285 | } |
---|
286 | } |
---|
287 | |
---|
288 | |
---|
289 | } |
---|
290 | |
---|
291 | #endif // CHARACTERCOLOR_H |
---|
292 | |
---|