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;
53 return QPalette::Link;
62 void ColorCache::init()
64 for (int col = 0; col <= Color_ignore; ++col) {
65 lcolors_[col] = QColor(lcolor.getX11Name(ColorCode(col)).c_str());
72 /// get the given color
73 QColor ColorCache::get(Color const & color) const
75 return get(color, lyxrc.use_system_colors);
79 /// get the given color
80 QColor ColorCache::get(Color const & color, bool syscolors) const
83 const_cast<ColorCache *>(this)->init();
84 if (color <= Color_ignore && color.mergeColor == Color_ignore) {
85 QPalette::ColorRole const cr = role(color.baseColor);
86 if (syscolors && cr != NoRole) {
87 static QColor const white = Qt::white;
88 QColor const c = pal_.brush(QPalette::Active, cr).color();
89 if (cr == QPalette::Base && c == white)
90 return lcolors_[color.baseColor];
94 return lcolors_[color.baseColor];
96 if (color.mergeColor != Color_ignore) {
97 // FIXME: This would ideally be done in the Color class, but
98 // that means that we'd have to use the Qt code in the core.
99 QColor base_color = get(color.baseColor, syscolors).toRgb();
100 QColor merge_color = get(color.mergeColor, syscolors).toRgb();
102 (base_color.red() + merge_color.red()) / 2,
103 (base_color.green() + merge_color.green()) / 2,
104 (base_color.blue() + merge_color.blue()) / 2);
107 return QColor(lcolor.getX11Name(color.baseColor).c_str());
111 bool ColorCache::isSystem(ColorCode const color) const
113 QPalette::ColorRole const cr = role(color);
114 if (cr == QPalette::Base) {
115 static QColor const white = Qt::white;
116 return pal_.brush(QPalette::Active, cr).color() != white;
122 QColor const rgb2qcolor(RGBColor const & rgb)
124 return QColor(rgb.r, rgb.g, rgb.b);