add more spacing
[personal-kdebase.git] / apps / konsole / src / CharacterColor.h
blobb6dcefcc7422ded2bf1e813be20ef839970b8a43
1 /*
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
20 02110-1301 USA.
23 #ifndef CHARACTERCOLOR_H
24 #define CHARACTERCOLOR_H
26 // Qt
27 #include <QtGui/QColor>
29 #include <kdemacros.h>
31 namespace Konsole
34 /**
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.
47 class ColorEntry
49 public:
50 /** Specifies the weight to use when drawing text with this color. */
51 enum FontWeight
53 /** Always draw text in this color with a bold weight. */
54 Bold,
55 /** Always draw text in this color with a normal weight. */
56 Normal,
57 /**
58 * Use the current font weight set by the terminal application.
59 * This is the default behavior.
61 UseCurrentFormat
64 /**
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) {}
74 /**
75 * Constructs a new color palette entry with an undefined color, and
76 * with the transparent and bold flags set to false.
77 */
78 ColorEntry() : transparent(false), fontWeight(UseCurrentFormat) {}
80 /**
81 * Sets the color, transparency and boldness of this color to those of @p rhs.
82 */
83 void operator=(const ColorEntry& rhs)
85 color = rhs.color;
86 transparent = rhs.transparent;
87 fontWeight = rhs.fontWeight;
90 /** The color value of this entry for display. */
91 QColor color;
93 /**
94 * If true character backgrounds using this color should be transparent.
95 * This is not applicable when the color is used to render text.
97 bool transparent;
98 /**
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 ///////////////////////////////
108 // Colors
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.
147 class CharacterColor
149 friend class Character;
151 public:
152 /** Constructs a new CharacterColor whoose color and color space are undefined. */
153 CharacterColor()
154 : _colorSpace(COLOR_SPACE_UNDEFINED),
155 _u(0),
156 _v(0),
157 _w(0)
160 /**
161 * Constructs a new CharacterColor using the specified @p colorSpace and with
162 * color value @p co
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),
172 _u(0),
173 _v(0),
174 _w(0)
176 switch (colorSpace)
178 case COLOR_SPACE_DEFAULT:
179 _u = co & 1;
180 break;
181 case COLOR_SPACE_SYSTEM:
182 _u = co & 7;
183 _v = (co >> 3) & 1;
184 break;
185 case COLOR_SPACE_256:
186 _u = co & 255;
187 break;
188 case COLOR_SPACE_RGB:
189 _u = co >> 16;
190 _v = co >> 8;
191 _w = co;
192 break;
193 default:
194 _colorSpace = COLOR_SPACE_UNDEFINED;
198 /**
199 * Returns true if this character color entry is valid.
201 bool isValid()
203 return _colorSpace != COLOR_SPACE_UNDEFINED;
206 /**
207 * Toggles the value of this color between a normal system color and the corresponding intensive
208 * system color.
210 * This is only applicable if the color is using the COLOR_SPACE_DEFAULT or COLOR_SPACE_SYSTEM
211 * color spaces.
213 void toggleIntensive();
215 /**
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
219 * it is ignored.
221 QColor color(const ColorEntry* palette) const;
223 /**
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);
234 private:
235 quint8 _colorSpace;
237 // bytes storing the character color
238 quint8 _u;
239 quint8 _v;
240 quint8 _w;
243 inline bool operator == (const CharacterColor& a, const CharacterColor& b)
245 return a._colorSpace == b._colorSpace &&
246 a._u == b._u &&
247 a._v == b._v &&
248 a._w == b._w;
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
272 switch (_colorSpace)
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
283 return QColor();
286 inline void CharacterColor::toggleIntensive()
288 if (_colorSpace == COLOR_SPACE_SYSTEM || _colorSpace == COLOR_SPACE_DEFAULT)
290 _v = !_v;
297 #endif // CHARACTERCOLOR_H