2 This file is part of Konsole, KDE's terminal.
4 Copyright 2007-2008 by Robert Knight <robertknight@gmail.com>
5 Copyright 1997,1998 by Lars Doelle <lars.doelle@on-line.de>
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.
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.
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
23 #ifndef CHARACTERCOLOR_H
24 #define CHARACTERCOLOR_H
27 #include <QtGui/QColor>
29 #include <kdemacros.h>
35 * An entry in a terminal display's color palette.
37 * A color palette is an array of 16 ColorEntry instances which map
38 * system color indexes (from 0 to 15) into actual colors.
40 * Each entry can be set as bold, in which case any text
41 * drawn using the color should be drawn in bold.
43 * Each entry can also be transparent, in which case the terminal
44 * display should avoid drawing the background for any characters
45 * using the entry as a background.
50 /** Specifies the weight to use when drawing text with this color. */
53 /** Always draw text in this color with a bold weight. */
55 /** Always draw text in this color with a normal weight. */
58 * Use the current font weight set by the terminal application.
59 * This is the default behavior.
65 * Constructs a new color palette entry.
67 * @param c The color value for this entry.
68 * @param tr Specifies that the color should be transparent when used as a background color.
69 * @param b Specifies that text drawn with this color should be bold.
71 ColorEntry(QColor c
, bool tr
, FontWeight weight
= UseCurrentFormat
)
72 : color(c
), transparent(tr
), fontWeight(weight
) {}
75 * Constructs a new color palette entry with an undefined color, and
76 * with the transparent and bold flags set to false.
78 ColorEntry() : transparent(false), fontWeight(UseCurrentFormat
) {}
81 * Sets the color, transparency and boldness of this color to those of @p rhs.
83 void operator=(const ColorEntry
& rhs
)
86 transparent
= rhs
.transparent
;
87 fontWeight
= rhs
.fontWeight
;
90 /** The color value of this entry for display. */
94 * If true character backgrounds using this color should be transparent.
95 * This is not applicable when the color is used to render text.
99 * Specifies the font weight to use when drawing text with this color.
100 * This is not applicable when the color is used to draw a character's background.
102 FontWeight fontWeight
;
106 // Attributed Character Representations ///////////////////////////////
110 #define BASE_COLORS (2+8)
111 #define INTENSITIES 2
112 #define TABLE_COLORS (INTENSITIES*BASE_COLORS)
114 #define DEFAULT_FORE_COLOR 0
115 #define DEFAULT_BACK_COLOR 1
117 //a standard set of colors using black text on a white background.
118 //defined in TerminalDisplay.cpp
120 extern const ColorEntry base_color_table
[TABLE_COLORS
] KDE_NO_EXPORT
;
122 /* CharacterColor is a union of the various color spaces.
124 Assignment is as follows:
126 Type - Space - Values
128 0 - Undefined - u: 0, v:0 w:0
129 1 - Default - u: 0..1 v:intense w:0
130 2 - System - u: 0..7 v:intense w:0
131 3 - Index(256) - u: 16..255 v:0 w:0
132 4 - RGB - u: 0..255 v:0..256 w:0..256
134 Default colour space has two separate colours, namely
135 default foreground and default background colour.
138 #define COLOR_SPACE_UNDEFINED 0
139 #define COLOR_SPACE_DEFAULT 1
140 #define COLOR_SPACE_SYSTEM 2
141 #define COLOR_SPACE_256 3
142 #define COLOR_SPACE_RGB 4
145 * Describes the color of a single character in the terminal.
149 friend class Character
;
152 /** Constructs a new CharacterColor whoose color and color space are undefined. */
154 : _colorSpace(COLOR_SPACE_UNDEFINED
),
161 * Constructs a new CharacterColor using the specified @p colorSpace and with
164 * The meaning of @p co depends on the @p colorSpace used.
166 * TODO : Document how @p co relates to @p colorSpace
168 * TODO : Add documentation about available color spaces.
170 CharacterColor(quint8 colorSpace
, int co
)
171 : _colorSpace(colorSpace
),
178 case COLOR_SPACE_DEFAULT
:
181 case COLOR_SPACE_SYSTEM
:
185 case COLOR_SPACE_256
:
188 case COLOR_SPACE_RGB
:
194 _colorSpace
= COLOR_SPACE_UNDEFINED
;
199 * Returns true if this character color entry is valid.
203 return _colorSpace
!= COLOR_SPACE_UNDEFINED
;
207 * Toggles the value of this color between a normal system color and the corresponding intensive
210 * This is only applicable if the color is using the COLOR_SPACE_DEFAULT or COLOR_SPACE_SYSTEM
213 void toggleIntensive();
216 * Returns the color within the specified color @palette
218 * The @p palette is only used if this color is one of the 16 system colors, otherwise
221 QColor
color(const ColorEntry
* palette
) const;
224 * Compares two colors and returns true if they represent the same color value and
225 * use the same color space.
227 friend bool operator == (const CharacterColor
& a
, const CharacterColor
& b
);
229 * Compares two colors and returns true if they represent different color values
230 * or use different color spaces.
232 friend bool operator != (const CharacterColor
& a
, const CharacterColor
& b
);
237 // bytes storing the character color
243 inline bool operator == (const CharacterColor
& a
, const CharacterColor
& b
)
245 return a
._colorSpace
== b
._colorSpace
&&
250 inline bool operator != (const CharacterColor
& a
, const CharacterColor
& b
)
252 return !operator==(a
,b
);
255 inline const QColor
color256(quint8 u
, const ColorEntry
* base
)
257 // 0.. 16: system colors
258 if (u
< 8) return base
[u
+2 ].color
; u
-= 8;
259 if (u
< 8) return base
[u
+2+BASE_COLORS
].color
; u
-= 8;
261 // 16..231: 6x6x6 rgb color cube
262 if (u
< 216) return QColor(((u
/36)%6) ? (40*((u
/36)%6)+55) : 0,
263 ((u
/ 6)%6) ? (40*((u
/ 6)%6)+55) : 0,
264 ((u
/ 1)%6) ? (40*((u
/ 1)%6)+55) : 0); u
-= 216;
266 // 232..255: gray, leaving out black and white
267 int gray
= u
*10+8; return QColor(gray
,gray
,gray
);
270 inline QColor
CharacterColor::color(const ColorEntry
* base
) const
274 case COLOR_SPACE_DEFAULT
: return base
[_u
+0+(_v
?BASE_COLORS
:0)].color
;
275 case COLOR_SPACE_SYSTEM
: return base
[_u
+2+(_v
?BASE_COLORS
:0)].color
;
276 case COLOR_SPACE_256
: return color256(_u
,base
);
277 case COLOR_SPACE_RGB
: return QColor(_u
,_v
,_w
);
278 case COLOR_SPACE_UNDEFINED
: return QColor();
281 Q_ASSERT(false); // invalid color space
286 inline void CharacterColor::toggleIntensive()
288 if (_colorSpace
== COLOR_SPACE_SYSTEM
|| _colorSpace
== COLOR_SPACE_DEFAULT
)
297 #endif // CHARACTERCOLOR_H