MauiKit Terminal

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

KDE's Doxygen guidelines are available online.