2
This file is part of Konsole, KDE's terminal.
4
Copyright (C) 2007 by Robert Knight <robertknight@gmail.com>
5
Copyright (C) 1997,1998 by Lars Doelle <lars.doelle@on-line.de>
7
Rewritten for QT4 by e_k <e_k at users.sourceforge.net>, Copyright (C)2008
9
This program is free software; you can redistribute it and/or modify
10
it under the terms of the GNU General Public License as published by
11
the Free Software Foundation; either version 2 of the License, or
12
(at your option) any later version.
14
This program is distributed in the hope that it will be useful,
15
but WITHOUT ANY WARRANTY; without even the implied warranty of
16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
GNU General Public License for more details.
19
You should have received a copy of the GNU General Public License
20
along with this program; if not, write to the Free Software
21
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25
#ifndef CHARACTERCOLOR_H
26
#define CHARACTERCOLOR_H
29
#include <QtGui/QColor>
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.
51
* Constructs a new color palette entry.
53
* @param c The color value for this entry.
54
* @param tr Specifies that the color should be transparent when used as a background color.
55
* @param b Specifies that text drawn with this color should be bold.
57
ColorEntry( QColor c, bool tr, bool b ) : color( c ), transparent( tr ), bold( b ) {}
60
* Constructs a new color palette entry with an undefined color, and
61
* with the transparent and bold flags set to false.
63
ColorEntry() : transparent( false ), bold( false ) {}
66
* Sets the color, transparency and boldness of this color to those of @p rhs.
68
void operator=( const ColorEntry& rhs )
71
transparent = rhs.transparent;
75
/** The color value of this entry for display. */
79
* If true character backgrounds using this color should be transparent.
80
* This is not applicable when the color is used to render text.
84
* If true characters drawn using this color should be bold.
85
* This is not applicable when the color is used to draw a character's background.
91
// Attributed Character Representations ///////////////////////////////
95
#define BASE_COLORS (2+8)
97
#define TABLE_COLORS (INTENSITIES*BASE_COLORS)
99
#define DEFAULT_FORE_COLOR 0
100
#define DEFAULT_BACK_COLOR 1
102
//a standard set of colors using black text on a white background.
103
//defined in TerminalDisplay.cpp
105
static const ColorEntry base_color_table[TABLE_COLORS] =
106
// The following are almost IBM standard color codes, with some slight
107
// gamma correction for the dim colors to compensate for bright X screens.
108
// It contains the 8 ansiterm/xterm colors in 2 intensities.
110
// Fixme: could add faint colors here, also.
112
ColorEntry( QColor( 0x00, 0x00, 0x00 ), 0, 0 ), ColorEntry( QColor( 0xB2, 0xB2, 0xB2 ), 1, 0 ), // Dfore, Dback
113
ColorEntry( QColor( 0x00, 0x00, 0x00 ), 0, 0 ), ColorEntry( QColor( 0xB2, 0x18, 0x18 ), 0, 0 ), // Black, Red
114
ColorEntry( QColor( 0x18, 0xB2, 0x18 ), 0, 0 ), ColorEntry( QColor( 0xB2, 0x68, 0x18 ), 0, 0 ), // Green, Yellow
115
ColorEntry( QColor( 0x18, 0x18, 0xB2 ), 0, 0 ), ColorEntry( QColor( 0xB2, 0x18, 0xB2 ), 0, 0 ), // Blue, Magenta
116
ColorEntry( QColor( 0x18, 0xB2, 0xB2 ), 0, 0 ), ColorEntry( QColor( 0xB2, 0xB2, 0xB2 ), 0, 0 ), // Cyan, White
118
ColorEntry( QColor( 0x00, 0x00, 0x00 ), 0, 1 ), ColorEntry( QColor( 0xFF, 0xFF, 0xFF ), 1, 0 ),
119
ColorEntry( QColor( 0x68, 0x68, 0x68 ), 0, 0 ), ColorEntry( QColor( 0xFF, 0x54, 0x54 ), 0, 0 ),
120
ColorEntry( QColor( 0x54, 0xFF, 0x54 ), 0, 0 ), ColorEntry( QColor( 0xFF, 0xFF, 0x54 ), 0, 0 ),
121
ColorEntry( QColor( 0x54, 0x54, 0xFF ), 0, 0 ), ColorEntry( QColor( 0xFF, 0x54, 0xFF ), 0, 0 ),
122
ColorEntry( QColor( 0x54, 0xFF, 0xFF ), 0, 0 ), ColorEntry( QColor( 0xFF, 0xFF, 0xFF ), 0, 0 )
125
/* CharacterColor is a union of the various color spaces.
127
Assignment is as follows:
129
Type - Space - Values
131
0 - Undefined - u: 0, v:0 w:0
132
1 - Default - u: 0..1 v:intense w:0
133
2 - System - u: 0..7 v:intense w:0
134
3 - Index(256) - u: 16..255 v:0 w:0
135
4 - RGB - u: 0..255 v:0..256 w:0..256
137
Default color space has two separate colors, namely
138
default foreground and default background color.
141
#define COLOR_SPACE_UNDEFINED 0
142
#define COLOR_SPACE_DEFAULT 1
143
#define COLOR_SPACE_SYSTEM 2
144
#define COLOR_SPACE_256 3
145
#define COLOR_SPACE_RGB 4
148
* Describes the color of a single character in the terminal.
152
friend class Character;
155
/** Constructs a new CharacterColor whoose color and color space are undefined. */
157
: _colorSpace( COLOR_SPACE_UNDEFINED ),
164
* Constructs a new CharacterColor using the specified @p colorSpace and with
167
* The meaning of @p co depends on the @p colorSpace used.
169
* TODO : Document how @p co relates to @p colorSpace
171
* TODO : Add documentation about available color spaces.
173
CharacterColor( quint8 colorSpace, int co )
174
: _colorSpace( colorSpace ),
179
switch ( colorSpace )
181
case COLOR_SPACE_DEFAULT:
184
case COLOR_SPACE_SYSTEM:
186
_v = ( co >> 3 ) & 1;
188
case COLOR_SPACE_256:
191
case COLOR_SPACE_RGB:
197
_colorSpace = COLOR_SPACE_UNDEFINED;
202
* Returns true if this character color entry is valid.
206
return _colorSpace != COLOR_SPACE_UNDEFINED;
210
* Toggles the value of this color between a normal system color and the corresponding intensive
213
* This is only applicable if the color is using the COLOR_SPACE_DEFAULT or COLOR_SPACE_SYSTEM
216
void toggleIntensive();
219
* Returns the color within the specified color @palette
221
* The @p palette is only used if this color is one of the 16 system colors, otherwise
224
QColor color( const ColorEntry* palette ) const;
227
* Compares two colors and returns true if they represent the same color value and
228
* use the same color space.
230
friend bool operator == ( const CharacterColor& a, const CharacterColor& b );
232
* Compares two colors and returns true if they represent different color values
233
* or use different color spaces.
235
friend bool operator != ( const CharacterColor& a, const CharacterColor& b );
240
// bytes storing the character color
246
inline bool operator == ( const CharacterColor& a, const CharacterColor& b )
248
return *reinterpret_cast<const quint32*>( &a._colorSpace ) ==
249
*reinterpret_cast<const quint32*>( &b._colorSpace );
252
inline bool operator != ( const CharacterColor& a, const CharacterColor& b )
254
return *reinterpret_cast<const quint32*>( &a._colorSpace ) !=
255
*reinterpret_cast<const quint32*>( &b._colorSpace );
258
inline const QColor color256( quint8 u, const ColorEntry* base )
260
// 0.. 16: system colors
261
if ( u < 8 ) return base[u+2 ].color; u -= 8;
262
if ( u < 8 ) return base[u+2+BASE_COLORS].color; u -= 8;
264
// 16..231: 6x6x6 rgb color cube
265
if ( u < 216 ) return QColor( 255*(( u / 36 ) % 6 ) / 5,
266
255*(( u / 6 ) % 6 ) / 5,
267
255*(( u / 1 ) % 6 ) / 5 ); u -= 216;
269
// 232..255: gray, leaving out black and white
270
int gray = u * 10 + 8; return QColor( gray, gray, gray );
273
inline QColor CharacterColor::color( const ColorEntry* base ) const
275
switch ( _colorSpace )
277
case COLOR_SPACE_DEFAULT: return base[_u+0+( _v?BASE_COLORS:0 )].color;
278
case COLOR_SPACE_SYSTEM: return base[_u+2+( _v?BASE_COLORS:0 )].color;
279
case COLOR_SPACE_256: return color256( _u, base );
280
case COLOR_SPACE_RGB: return QColor( _u, _v, _w );
281
case COLOR_SPACE_UNDEFINED: return QColor();
284
Q_ASSERT( false ); // invalid color space
289
inline void CharacterColor::toggleIntensive()
291
if ( _colorSpace == COLOR_SPACE_SYSTEM || _colorSpace == COLOR_SPACE_DEFAULT )
300
#endif // CHARACTERCOLOR_H