• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • applications API Reference
  • KDE Home
  • Contact Us
 

Konsole

  • kde-4.14
  • applications
  • konsole
  • src
ColorScheme.cpp
Go to the documentation of this file.
1 /*
2  This source file is part of Konsole, a terminal emulator.
3 
4  Copyright 2007-2008 by Robert Knight <robertknight@gmail.com>
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  02110-1301 USA.
20 */
21 
22 // Own
23 #include "ColorScheme.h"
24 
25 // Qt
26 #include <QtGui/QPainter>
27 
28 // KDE
29 #include <KConfig>
30 #include <KLocalizedString>
31 #include <KConfigGroup>
32 
33 namespace
34 {
35 const int FGCOLOR_INDEX = 0;
36 const int BGCOLOR_INDEX = 1;
37 }
38 
39 using namespace Konsole;
40 
41 // The following are almost IBM standard color codes, with some slight
42 // gamma correction for the dim colors to compensate for bright X screens.
43 // It contains the 8 ansiterm/xterm colors in 2 intensities.
44 const ColorEntry ColorScheme::defaultTable[TABLE_COLORS] = {
45  ColorEntry(QColor(0x00, 0x00, 0x00)), // Dfore
46  ColorEntry(QColor(0xFF, 0xFF, 0xFF)), // Dback
47  ColorEntry(QColor(0x00, 0x00, 0x00)), // Black
48  ColorEntry(QColor(0xB2, 0x18, 0x18)), // Red
49  ColorEntry(QColor(0x18, 0xB2, 0x18)), // Green
50  ColorEntry(QColor(0xB2, 0x68, 0x18)), // Yellow
51  ColorEntry(QColor(0x18, 0x18, 0xB2)), // Blue
52  ColorEntry(QColor(0xB2, 0x18, 0xB2)), // Magenta
53  ColorEntry(QColor(0x18, 0xB2, 0xB2)), // Cyan
54  ColorEntry(QColor(0xB2, 0xB2, 0xB2)), // White
55  // intensive versions
56  ColorEntry(QColor(0x00, 0x00, 0x00)),
57  ColorEntry(QColor(0xFF, 0xFF, 0xFF)),
58  ColorEntry(QColor(0x68, 0x68, 0x68)),
59  ColorEntry(QColor(0xFF, 0x54, 0x54)),
60  ColorEntry(QColor(0x54, 0xFF, 0x54)),
61  ColorEntry(QColor(0xFF, 0xFF, 0x54)),
62  ColorEntry(QColor(0x54, 0x54, 0xFF)),
63  ColorEntry(QColor(0xFF, 0x54, 0xFF)),
64  ColorEntry(QColor(0x54, 0xFF, 0xFF)),
65  ColorEntry(QColor(0xFF, 0xFF, 0xFF))
66 };
67 
68 const char* const ColorScheme::colorNames[TABLE_COLORS] = {
69  "Foreground",
70  "Background",
71  "Color0",
72  "Color1",
73  "Color2",
74  "Color3",
75  "Color4",
76  "Color5",
77  "Color6",
78  "Color7",
79  "ForegroundIntense",
80  "BackgroundIntense",
81  "Color0Intense",
82  "Color1Intense",
83  "Color2Intense",
84  "Color3Intense",
85  "Color4Intense",
86  "Color5Intense",
87  "Color6Intense",
88  "Color7Intense"
89 };
90 const char* const ColorScheme::translatedColorNames[TABLE_COLORS] = {
91  I18N_NOOP2("@item:intable palette", "Foreground"),
92  I18N_NOOP2("@item:intable palette", "Background"),
93  I18N_NOOP2("@item:intable palette", "Color 1"),
94  I18N_NOOP2("@item:intable palette", "Color 2"),
95  I18N_NOOP2("@item:intable palette", "Color 3"),
96  I18N_NOOP2("@item:intable palette", "Color 4"),
97  I18N_NOOP2("@item:intable palette", "Color 5"),
98  I18N_NOOP2("@item:intable palette", "Color 6"),
99  I18N_NOOP2("@item:intable palette", "Color 7"),
100  I18N_NOOP2("@item:intable palette", "Color 8"),
101  I18N_NOOP2("@item:intable palette", "Foreground (Intense)"),
102  I18N_NOOP2("@item:intable palette", "Background (Intense)"),
103  I18N_NOOP2("@item:intable palette", "Color 1 (Intense)"),
104  I18N_NOOP2("@item:intable palette", "Color 2 (Intense)"),
105  I18N_NOOP2("@item:intable palette", "Color 3 (Intense)"),
106  I18N_NOOP2("@item:intable palette", "Color 4 (Intense)"),
107  I18N_NOOP2("@item:intable palette", "Color 5 (Intense)"),
108  I18N_NOOP2("@item:intable palette", "Color 6 (Intense)"),
109  I18N_NOOP2("@item:intable palette", "Color 7 (Intense)"),
110  I18N_NOOP2("@item:intable palette", "Color 8 (Intense)")
111 };
112 
113 QString ColorScheme::colorNameForIndex(int index)
114 {
115  Q_ASSERT(index >= 0 && index < TABLE_COLORS);
116 
117  return QString(colorNames[index]);
118 }
119 
120 QString ColorScheme::translatedColorNameForIndex(int index)
121 {
122  Q_ASSERT(index >= 0 && index < TABLE_COLORS);
123 
124  return i18nc("@item:intable palette", translatedColorNames[index]);
125 }
126 
127 ColorScheme::ColorScheme()
128 {
129  _table = 0;
130  _randomTable = 0;
131  _opacity = 1.0;
132  setWallpaper(QString());
133 }
134 
135 ColorScheme::ColorScheme(const ColorScheme& other)
136  : _table(0)
137  , _randomTable(0)
138  , _opacity(other._opacity)
139  , _wallpaper(other._wallpaper)
140 {
141  setName(other.name());
142  setDescription(other.description());
143 
144  if (other._table != 0) {
145  for (int i = 0 ; i < TABLE_COLORS ; i++)
146  setColorTableEntry(i, other._table[i]);
147  }
148 
149  if (other._randomTable != 0) {
150  for (int i = 0 ; i < TABLE_COLORS ; i++) {
151  const RandomizationRange& range = other._randomTable[i];
152  setRandomizationRange(i, range.hue, range.saturation, range.value);
153  }
154  }
155 }
156 ColorScheme::~ColorScheme()
157 {
158  delete[] _table;
159  delete[] _randomTable;
160 }
161 
162 void ColorScheme::setDescription(const QString& aDescription)
163 {
164  _description = aDescription;
165 }
166 QString ColorScheme::description() const
167 {
168  return _description;
169 }
170 
171 void ColorScheme::setName(const QString& aName)
172 {
173  _name = aName;
174 }
175 QString ColorScheme::name() const
176 {
177  return _name;
178 }
179 
180 void ColorScheme::setColorTableEntry(int index , const ColorEntry& entry)
181 {
182  Q_ASSERT(index >= 0 && index < TABLE_COLORS);
183 
184  if (!_table) {
185  _table = new ColorEntry[TABLE_COLORS];
186 
187  for (int i = 0; i < TABLE_COLORS; i++)
188  _table[i] = defaultTable[i];
189  }
190 
191  _table[index] = entry;
192 }
193 ColorEntry ColorScheme::colorEntry(int index , uint randomSeed) const
194 {
195  Q_ASSERT(index >= 0 && index < TABLE_COLORS);
196 
197  if (randomSeed != 0)
198  qsrand(randomSeed);
199 
200  ColorEntry entry = colorTable()[index];
201 
202  if (randomSeed != 0 &&
203  _randomTable != 0 &&
204  !_randomTable[index].isNull()) {
205  const RandomizationRange& range = _randomTable[index];
206 
207  int hueDifference = range.hue ? (qrand() % range.hue) - range.hue / 2 : 0;
208  int saturationDifference = range.saturation ? (qrand() % range.saturation) - range.saturation / 2 : 0;
209  int valueDifference = range.value ? (qrand() % range.value) - range.value / 2 : 0;
210 
211  QColor& color = entry.color;
212 
213  int newHue = qAbs((color.hue() + hueDifference) % MAX_HUE);
214  int newValue = qMin(qAbs(color.value() + valueDifference) , 255);
215  int newSaturation = qMin(qAbs(color.saturation() + saturationDifference) , 255);
216 
217  color.setHsv(newHue, newSaturation, newValue);
218  }
219 
220  return entry;
221 }
222 void ColorScheme::getColorTable(ColorEntry* table , uint randomSeed) const
223 {
224  for (int i = 0 ; i < TABLE_COLORS ; i++)
225  table[i] = colorEntry(i, randomSeed);
226 }
227 bool ColorScheme::randomizedBackgroundColor() const
228 {
229  return _randomTable == 0 ? false : !_randomTable[BGCOLOR_INDEX].isNull();
230 }
231 void ColorScheme::setRandomizedBackgroundColor(bool randomize)
232 {
233  // the hue of the background color is allowed to be randomly
234  // adjusted as much as possible.
235  //
236  // the value and saturation are left alone to maintain read-ability
237  if (randomize) {
238  setRandomizationRange(BGCOLOR_INDEX , MAX_HUE , 255 , 0);
239  } else {
240  if (_randomTable)
241  setRandomizationRange(BGCOLOR_INDEX , 0 , 0 , 0);
242  }
243 }
244 
245 void ColorScheme::setRandomizationRange(int index , quint16 hue , quint8 saturation ,
246  quint8 value)
247 {
248  Q_ASSERT(hue <= MAX_HUE);
249  Q_ASSERT(index >= 0 && index < TABLE_COLORS);
250 
251  if (_randomTable == 0)
252  _randomTable = new RandomizationRange[TABLE_COLORS];
253 
254  _randomTable[index].hue = hue;
255  _randomTable[index].value = value;
256  _randomTable[index].saturation = saturation;
257 }
258 
259 const ColorEntry* ColorScheme::colorTable() const
260 {
261  if (_table)
262  return _table;
263  else
264  return defaultTable;
265 }
266 QColor ColorScheme::foregroundColor() const
267 {
268  return colorTable()[FGCOLOR_INDEX].color;
269 }
270 QColor ColorScheme::backgroundColor() const
271 {
272  return colorTable()[BGCOLOR_INDEX].color;
273 }
274 bool ColorScheme::hasDarkBackground() const
275 {
276  // value can range from 0 - 255, with larger values indicating higher brightness.
277  // so 127 is in the middle, anything less is deemed 'dark'
278  return backgroundColor().value() < 127;
279 }
280 void ColorScheme::setOpacity(qreal aOpacity)
281 {
282  _opacity = aOpacity;
283 }
284 qreal ColorScheme::opacity() const
285 {
286  return _opacity;
287 }
288 
289 void ColorScheme::read(const KConfig& config)
290 {
291  KConfigGroup configGroup = config.group("General");
292 
293  const QString schemeDescription = configGroup.readEntry("Description", I18N_NOOP("Un-named Color Scheme"));
294 
295  _description = i18n(schemeDescription.toUtf8());
296  _opacity = configGroup.readEntry("Opacity", qreal(1.0));
297  setWallpaper(configGroup.readEntry("Wallpaper", QString()));
298 
299  for (int i = 0 ; i < TABLE_COLORS ; i++) {
300  readColorEntry(config, i);
301  }
302 }
303 
304 void ColorScheme::readColorEntry(const KConfig& config , int index)
305 {
306  KConfigGroup configGroup = config.group(colorNameForIndex(index));
307 
308  ColorEntry entry;
309 
310  entry.color = configGroup.readEntry("Color", QColor());
311 
312  setColorTableEntry(index , entry);
313 
314  const quint16 hue = configGroup.readEntry("MaxRandomHue", 0);
315  const quint8 value = configGroup.readEntry("MaxRandomValue", 0);
316  const quint8 saturation = configGroup.readEntry("MaxRandomSaturation", 0);
317 
318  if (hue != 0 || value != 0 || saturation != 0)
319  setRandomizationRange(index , hue , saturation , value);
320 }
321 
322 void ColorScheme::write(KConfig& config) const
323 {
324  KConfigGroup configGroup = config.group("General");
325 
326  configGroup.writeEntry("Description", _description);
327  configGroup.writeEntry("Opacity", _opacity);
328  configGroup.writeEntry("Wallpaper", _wallpaper->path());
329 
330  for (int i = 0 ; i < TABLE_COLORS ; i++) {
331  writeColorEntry(config, i);
332  }
333 }
334 
335 void ColorScheme::writeColorEntry(KConfig& config , int index) const
336 {
337  KConfigGroup configGroup = config.group(colorNameForIndex(index));
338 
339  const ColorEntry& entry = colorTable()[index];
340 
341  configGroup.writeEntry("Color", entry.color);
342 
343  // Remove unused keys
344  if (configGroup.hasKey("Transparent")) {
345  configGroup.deleteEntry("Transparent");
346  }
347  if (configGroup.hasKey("Transparency")) {
348  configGroup.deleteEntry("Transparency");
349  }
350  if (configGroup.hasKey("Bold")) {
351  configGroup.deleteEntry("Bold");
352  }
353 
354  RandomizationRange random = _randomTable != 0 ? _randomTable[index] : RandomizationRange();
355 
356  // record randomization if this color has randomization or
357  // if one of the keys already exists
358  if (!random.isNull() || configGroup.hasKey("MaxRandomHue")) {
359  configGroup.writeEntry("MaxRandomHue", static_cast<int>(random.hue));
360  configGroup.writeEntry("MaxRandomValue", static_cast<int>(random.value));
361  configGroup.writeEntry("MaxRandomSaturation", static_cast<int>(random.saturation));
362  }
363 }
364 
365 void ColorScheme::setWallpaper(const QString& path)
366 {
367  _wallpaper = new ColorSchemeWallpaper(path);
368 }
369 
370 ColorSchemeWallpaper::Ptr ColorScheme::wallpaper() const
371 {
372  return _wallpaper;
373 }
374 
375 ColorSchemeWallpaper::ColorSchemeWallpaper(const QString& aPath)
376  : _path(aPath),
377  _picture(0)
378 {
379 }
380 
381 ColorSchemeWallpaper::~ColorSchemeWallpaper()
382 {
383  delete _picture;
384 }
385 
386 void ColorSchemeWallpaper::load()
387 {
388  if (_path.isEmpty())
389  return;
390 
391  // Create and load original pixmap
392  if (!_picture)
393  _picture = new QPixmap();
394 
395  if (_picture->isNull())
396  _picture->load(_path);
397 }
398 
399 bool ColorSchemeWallpaper::isNull() const
400 {
401  return _path.isEmpty();
402 }
403 
404 bool ColorSchemeWallpaper::draw(QPainter& painter, const QRect& rect, qreal opacity)
405 {
406  if (!_picture || _picture->isNull())
407  return false;
408 
409  if (qFuzzyCompare(1.0, opacity)) {
410  painter.drawTiledPixmap(rect, *_picture, rect.topLeft());
411  return true;
412  }
413  painter.save();
414  painter.setCompositionMode(QPainter::CompositionMode_Source);
415  painter.fillRect(rect, QColor(0,0,0,0));
416  painter.setOpacity(opacity);
417  painter.drawTiledPixmap(rect, *_picture, rect.topLeft());
418  painter.restore();
419  return true;
420 }
421 
422 QString ColorSchemeWallpaper::path() const
423 {
424  return _path;
425 }
426 
Konsole::ColorScheme::translatedColorNameForIndex
static QString translatedColorNameForIndex(int index)
Definition: ColorScheme.cpp:120
TABLE_COLORS
#define TABLE_COLORS
Definition: CharacterColor.h:117
QPainter::setOpacity
void setOpacity(qreal opacity)
Konsole::ColorSchemeWallpaper::isNull
bool isNull() const
Definition: ColorScheme.cpp:399
Konsole::ColorScheme::ColorScheme
ColorScheme()
Constructs a new color scheme which is initialized to the default color set for Konsole.
Definition: ColorScheme.cpp:127
QPainter::fillRect
void fillRect(const QRectF &rectangle, const QBrush &brush)
QPainter::setCompositionMode
void setCompositionMode(CompositionMode mode)
Konsole::ColorScheme::hasDarkBackground
bool hasDarkBackground() const
Returns true if this color scheme has a dark background.
Definition: ColorScheme.cpp:274
Konsole::ColorSchemeWallpaper::~ColorSchemeWallpaper
~ColorSchemeWallpaper()
Definition: ColorScheme.cpp:381
Konsole::ColorSchemeWallpaper
This class holds the wallpaper pixmap associated with a color scheme.
Definition: ColorScheme.h:44
QColor::value
int value() const
QPainter::save
void save()
Konsole::ColorScheme::getColorTable
void getColorTable(ColorEntry *table, uint randomSeed=0) const
Copies the color entries which form the palette for this color scheme into table. ...
Definition: ColorScheme.cpp:222
QPainter::drawTiledPixmap
void drawTiledPixmap(const QRectF &rectangle, const QPixmap &pixmap, const QPointF &position)
Konsole::ColorScheme::setRandomizedBackgroundColor
void setRandomizedBackgroundColor(bool randomize)
Enables randomization of the background color.
Definition: ColorScheme.cpp:231
Konsole::ColorScheme::write
void write(KConfig &config) const
Writes the color scheme to the specified configuration source.
Definition: ColorScheme.cpp:322
Konsole::ColorScheme::setOpacity
void setOpacity(qreal opacity)
Sets the opacity level of the display background.
Definition: ColorScheme.cpp:280
Konsole::ColorScheme::colorEntry
ColorEntry colorEntry(int index, uint randomSeed=0) const
Retrieves a single color entry from the table.
Definition: ColorScheme.cpp:193
Konsole::ColorSchemeWallpaper::draw
bool draw(QPainter &painter, const QRect &rect, qreal opacity=1.0)
Returns true if wallpaper available and drawn.
Definition: ColorScheme.cpp:404
Konsole::ColorScheme::~ColorScheme
~ColorScheme()
Definition: ColorScheme.cpp:156
Konsole::ColorSchemeWallpaper::path
QString path() const
Definition: ColorScheme.cpp:422
QRect
Konsole::ColorScheme::opacity
qreal opacity() const
Returns the opacity level for this color scheme, see setOpacity() TODO: More documentation.
Definition: ColorScheme.cpp:284
Konsole::ColorScheme::randomizedBackgroundColor
bool randomizedBackgroundColor() const
Returns true if the background color is randomized.
Definition: ColorScheme.cpp:227
Konsole::ColorSchemeWallpaper::load
void load()
Definition: ColorScheme.cpp:386
Konsole::ColorEntry
An entry in a terminal display's color palette.
Definition: CharacterColor.h:40
Konsole::ColorSchemeWallpaper::Ptr
KSharedPtr< ColorSchemeWallpaper > Ptr
Definition: ColorScheme.h:47
Konsole::ColorSchemeWallpaper::ColorSchemeWallpaper
ColorSchemeWallpaper(const QString &path)
Definition: ColorScheme.cpp:375
Konsole::ColorScheme::setColorTableEntry
void setColorTableEntry(int index, const ColorEntry &entry)
Sets a single entry within the color palette.
Definition: ColorScheme.cpp:180
QPainter
QString::isEmpty
bool isEmpty() const
QString
QColor
QPixmap::load
bool load(const QString &fileName, const char *format, QFlags< Qt::ImageConversionFlag > flags)
Konsole::ColorScheme::description
QString description() const
Returns the descriptive name of the color scheme.
Definition: ColorScheme.cpp:166
QPixmap
Konsole::ColorScheme::setDescription
void setDescription(const QString &description)
Sets the descriptive name of the color scheme.
Definition: ColorScheme.cpp:162
Konsole::ColorScheme::setName
void setName(const QString &name)
Sets the name of the color scheme.
Definition: ColorScheme.cpp:171
QPixmap::isNull
bool isNull() const
ColorScheme.h
QPainter::restore
void restore()
Konsole::ColorScheme::setWallpaper
void setWallpaper(const QString &path)
Definition: ColorScheme.cpp:365
QRect::topLeft
QPoint topLeft() const
Konsole::ColorScheme::defaultTable
static const ColorEntry defaultTable[]
Definition: ColorScheme.h:171
Konsole::ColorScheme::colorNameForIndex
static QString colorNameForIndex(int index)
Definition: ColorScheme.cpp:113
Konsole::ColorScheme::name
QString name() const
Returns the name of the color scheme.
Definition: ColorScheme.cpp:175
Konsole::ColorEntry::color
QColor color
The color value of this entry for display.
Definition: CharacterColor.h:80
Konsole::ColorScheme::backgroundColor
QColor backgroundColor() const
Convenience method.
Definition: ColorScheme.cpp:270
Konsole::ColorScheme::foregroundColor
QColor foregroundColor() const
Convenience method.
Definition: ColorScheme.cpp:266
Konsole::ColorScheme
Represents a color scheme for a terminal display.
Definition: ColorScheme.h:72
Konsole::ColorScheme::wallpaper
ColorSchemeWallpaper::Ptr wallpaper() const
Definition: ColorScheme.cpp:370
Konsole::ColorScheme::read
void read(const KConfig &config)
Reads the color scheme from the specified configuration source.
Definition: ColorScheme.cpp:289
QString::toUtf8
QByteArray toUtf8() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:27 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Konsole

Skip menu "Konsole"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Konsole

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal