~larryprice/acolyterm/release-0.1

« back to all changes in this revision

Viewing changes to src/plugin/konsole/CharacterColor.h

  • Committer: Larry Price
  • Date: 2016-06-15 14:47:59 UTC
  • Revision ID: larry.price@canonical.com-20160615144759-6wopn0gxwgta3x1n
Updating QMLTermWidget and removing unnecessary konsole codebase

Show diffs side-by-side

added added

removed removed

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