3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
15 #include "ColorCache.h"
21 // FIXME (later): Qt >= 4.4 has a proper QPalette::NoRole value.
22 QPalette::ColorRole const NoRole = static_cast<QPalette::ColorRole>(-1);
24 QPalette::ColorRole role(ColorCode col)
26 switch (ColorCode(col)) {
27 case Color_background:
29 case Color_greyedoutbg:
31 case Color_graphicsbg:
32 case Color_mathmacrobg:
33 case Color_mathcorners:
34 return QPalette::Base;
37 case Color_foreground:
40 case Color_tabularline:
41 case Color_previewframe:
42 return QPalette::Text;
46 return QPalette::Highlight;
48 case Color_selectiontext:
49 return QPalette::HighlightedText;
59 void ColorCache::init()
61 for (int col = 0; col <= Color_ignore; ++col) {
62 lcolors_[col] = QColor(lcolor.getX11Name(ColorCode(col)).c_str());
69 /// get the given color
70 QColor ColorCache::get(Color const & color) const
72 return get(color, lyxrc.use_system_colors);
76 /// get the given color
77 QColor ColorCache::get(Color const & color, bool syscolors) const
80 const_cast<ColorCache *>(this)->init();
81 if (color <= Color_ignore && color.mergeColor == Color_ignore) {
82 QPalette::ColorRole const cr = role(color.baseColor);
83 if (syscolors && cr != NoRole) {
84 static QColor const white = Qt::white;
85 QColor const c = pal_.brush(QPalette::Active, cr).color();
86 if (cr == QPalette::Base && c == white)
87 return lcolors_[color.baseColor];
91 return lcolors_[color.baseColor];
93 if (color.mergeColor != Color_ignore) {
94 // FIXME: This would ideally be done in the Color class, but
95 // that means that we'd have to use the Qt code in the core.
96 QColor base_color = get(color.baseColor, syscolors).toRgb();
97 QColor merge_color = get(color.mergeColor, syscolors).toRgb();
99 (base_color.red() + merge_color.red()) / 2,
100 (base_color.green() + merge_color.green()) / 2,
101 (base_color.blue() + merge_color.blue()) / 2);
104 return QColor(lcolor.getX11Name(color.baseColor).c_str());
108 bool ColorCache::isSystem(ColorCode const color) const
110 QPalette::ColorRole const cr = role(color);
111 if (cr == QPalette::Base) {
112 static QColor const white = Qt::white;
113 return pal_.brush(QPalette::Active, cr).color() != white;
119 QColor const rgb2qcolor(RGBColor const & rgb)
121 return QColor(rgb.r, rgb.g, rgb.b);