~ubuntu-branches/ubuntu/saucy/qgis/saucy

« back to all changes in this revision

Viewing changes to src/plugins/grass/qtermwidget/CharacterColor.h

  • Committer: Bazaar Package Importer
  • Author(s): Johan Van de Wauw
  • Date: 2010-07-11 20:23:24 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100711202324-5ktghxa7hracohmr
Tags: 1.4.0+12730-3ubuntu1
* Merge from Debian unstable (LP: #540941).
* Fix compilation issues with QT 4.7
* Add build-depends on libqt4-webkit-dev 

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 (C) 2007 by Robert Knight <robertknight@gmail.com>
 
5
    Copyright (C) 1997,1998 by Lars Doelle <lars.doelle@on-line.de>
 
6
 
 
7
    Rewritten for QT4 by e_k <e_k at users.sourceforge.net>, Copyright (C)2008
 
8
 
 
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.
 
13
 
 
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.
 
18
 
 
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
 
22
    02110-1301  USA.
 
23
*/
 
24
 
 
25
#ifndef CHARACTERCOLOR_H
 
26
#define CHARACTERCOLOR_H
 
27
 
 
28
// Qt
 
29
#include <QtGui/QColor>
 
30
 
 
31
namespace Konsole
 
32
{
 
33
 
 
34
  /**
 
35
   * An entry in a terminal display's color palette.
 
36
   *
 
37
   * A color palette is an array of 16 ColorEntry instances which map
 
38
   * system color indexes (from 0 to 15) into actual colors.
 
39
   *
 
40
   * Each entry can be set as bold, in which case any text
 
41
   * drawn using the color should be drawn in bold.
 
42
   *
 
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.
 
46
   */
 
47
  class ColorEntry
 
48
  {
 
49
    public:
 
50
      /**
 
51
       * Constructs a new color palette entry.
 
52
       *
 
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.
 
56
       */
 
57
      ColorEntry( QColor c, bool tr, bool b ) : color( c ), transparent( tr ), bold( b ) {}
 
58
 
 
59
      /**
 
60
       * Constructs a new color palette entry with an undefined color, and
 
61
       * with the transparent and bold flags set to false.
 
62
       */
 
63
      ColorEntry() : transparent( false ), bold( false ) {}
 
64
 
 
65
      /**
 
66
       * Sets the color, transparency and boldness of this color to those of @p rhs.
 
67
       */
 
68
      void operator=( const ColorEntry& rhs )
 
69
      {
 
70
        color = rhs.color;
 
71
        transparent = rhs.transparent;
 
72
        bold = rhs.bold;
 
73
      }
 
74
 
 
75
      /** The color value of this entry for display. */
 
76
      QColor color;
 
77
 
 
78
      /**
 
79
       * If true character backgrounds using this color should be transparent.
 
80
       * This is not applicable when the color is used to render text.
 
81
       */
 
82
      bool   transparent;
 
83
      /**
 
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.
 
86
       */
 
87
      bool   bold;
 
88
  };
 
89
 
 
90
 
 
91
// Attributed Character Representations ///////////////////////////////
 
92
 
 
93
// Colors
 
94
 
 
95
#define BASE_COLORS   (2+8)
 
96
#define INTENSITIES   2
 
97
#define TABLE_COLORS  (INTENSITIES*BASE_COLORS)
 
98
 
 
99
#define DEFAULT_FORE_COLOR 0
 
100
#define DEFAULT_BACK_COLOR 1
 
101
 
 
102
//a standard set of colors using black text on a white background.
 
103
//defined in TerminalDisplay.cpp
 
104
 
 
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.
 
109
  {
 
110
    // Fixme: could add faint colors here, also.
 
111
    // normal
 
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
 
117
    // intensiv
 
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 )
 
123
  };
 
124
 
 
125
  /* CharacterColor is a union of the various color spaces.
 
126
 
 
127
     Assignment is as follows:
 
128
 
 
129
     Type  - Space        - Values
 
130
 
 
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
 
136
 
 
137
     Default color space has two separate colors, namely
 
138
     default foreground and default background color.
 
139
  */
 
140
 
 
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
 
146
 
 
147
  /**
 
148
   * Describes the color of a single character in the terminal.
 
149
   */
 
150
  class CharacterColor
 
151
  {
 
152
      friend class Character;
 
153
 
 
154
    public:
 
155
      /** Constructs a new CharacterColor whoose color and color space are undefined. */
 
156
      CharacterColor()
 
157
          : _colorSpace( COLOR_SPACE_UNDEFINED ),
 
158
          _u( 0 ),
 
159
          _v( 0 ),
 
160
          _w( 0 )
 
161
      {}
 
162
 
 
163
      /**
 
164
       * Constructs a new CharacterColor using the specified @p colorSpace and with
 
165
       * color value @p co
 
166
       *
 
167
       * The meaning of @p co depends on the @p colorSpace used.
 
168
       *
 
169
       * TODO : Document how @p co relates to @p colorSpace
 
170
       *
 
171
       * TODO : Add documentation about available color spaces.
 
172
       */
 
173
      CharacterColor( quint8 colorSpace, int co )
 
174
          : _colorSpace( colorSpace ),
 
175
          _u( 0 ),
 
176
          _v( 0 ),
 
177
          _w( 0 )
 
178
      {
 
179
        switch ( colorSpace )
 
180
        {
 
181
          case COLOR_SPACE_DEFAULT:
 
182
            _u = co & 1;
 
183
            break;
 
184
          case COLOR_SPACE_SYSTEM:
 
185
            _u = co & 7;
 
186
            _v = ( co >> 3 ) & 1;
 
187
            break;
 
188
          case COLOR_SPACE_256:
 
189
            _u = co & 255;
 
190
            break;
 
191
          case COLOR_SPACE_RGB:
 
192
            _u = co >> 16;
 
193
            _v = co >> 8;
 
194
            _w = co;
 
195
            break;
 
196
          default:
 
197
            _colorSpace = COLOR_SPACE_UNDEFINED;
 
198
        }
 
199
      }
 
200
 
 
201
      /**
 
202
       * Returns true if this character color entry is valid.
 
203
       */
 
204
      bool isValid()
 
205
      {
 
206
        return _colorSpace != COLOR_SPACE_UNDEFINED;
 
207
      }
 
208
 
 
209
      /**
 
210
       * Toggles the value of this color between a normal system color and the corresponding intensive
 
211
       * system color.
 
212
       *
 
213
       * This is only applicable if the color is using the COLOR_SPACE_DEFAULT or COLOR_SPACE_SYSTEM
 
214
       * color spaces.
 
215
       */
 
216
      void toggleIntensive();
 
217
 
 
218
      /**
 
219
       * Returns the color within the specified color @palette
 
220
       *
 
221
       * The @p palette is only used if this color is one of the 16 system colors, otherwise
 
222
       * it is ignored.
 
223
       */
 
224
      QColor color( const ColorEntry* palette ) const;
 
225
 
 
226
      /**
 
227
       * Compares two colors and returns true if they represent the same color value and
 
228
       * use the same color space.
 
229
       */
 
230
      friend bool operator == ( const CharacterColor& a, const CharacterColor& b );
 
231
      /**
 
232
       * Compares two colors and returns true if they represent different color values
 
233
       * or use different color spaces.
 
234
       */
 
235
      friend bool operator != ( const CharacterColor& a, const CharacterColor& b );
 
236
 
 
237
    private:
 
238
      quint8 _colorSpace;
 
239
 
 
240
      // bytes storing the character color
 
241
      quint8 _u;
 
242
      quint8 _v;
 
243
      quint8 _w;
 
244
  };
 
245
 
 
246
  inline bool operator == ( const CharacterColor& a, const CharacterColor& b )
 
247
  {
 
248
    return *reinterpret_cast<const quint32*>( &a._colorSpace ) ==
 
249
           *reinterpret_cast<const quint32*>( &b._colorSpace );
 
250
  }
 
251
 
 
252
  inline bool operator != ( const CharacterColor& a, const CharacterColor& b )
 
253
  {
 
254
    return *reinterpret_cast<const quint32*>( &a._colorSpace ) !=
 
255
           *reinterpret_cast<const quint32*>( &b._colorSpace );
 
256
  }
 
257
 
 
258
  inline const QColor color256( quint8 u, const ColorEntry* base )
 
259
  {
 
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;
 
263
 
 
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;
 
268
 
 
269
    // 232..255: gray, leaving out black and white
 
270
    int gray = u * 10 + 8; return QColor( gray, gray, gray );
 
271
  }
 
272
 
 
273
  inline QColor CharacterColor::color( const ColorEntry* base ) const
 
274
  {
 
275
    switch ( _colorSpace )
 
276
    {
 
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();
 
282
    }
 
283
 
 
284
    Q_ASSERT( false ); // invalid color space
 
285
 
 
286
    return QColor();
 
287
  }
 
288
 
 
289
  inline void CharacterColor::toggleIntensive()
 
290
  {
 
291
    if ( _colorSpace == COLOR_SPACE_SYSTEM || _colorSpace == COLOR_SPACE_DEFAULT )
 
292
    {
 
293
      _v = !_v;
 
294
    }
 
295
  }
 
296
 
 
297
 
 
298
}
 
299
 
 
300
#endif // CHARACTERCOLOR_H
 
301