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"
22 QPalette::ColorRole role(ColorCode col)
24 switch (ColorCode(col)) {
25 case Color_background:
27 case Color_greyedoutbg:
29 case Color_graphicsbg:
30 case Color_mathmacrobg:
31 case Color_mathcorners:
32 return QPalette::Base;
35 case Color_foreground:
38 case Color_tabularline:
39 case Color_previewframe:
40 return QPalette::Text;
44 return QPalette::Highlight;
46 case Color_selectiontext:
47 return QPalette::HighlightedText;
51 return QPalette::Link;
53 return QPalette::NoRole;
60 void ColorCache::init()
62 for (int col = 0; col <= Color_ignore; ++col) {
63 lcolors_[col] = QColor(lcolor.getX11Name(ColorCode(col)).c_str());
70 /// get the given color
71 QColor ColorCache::get(Color const & color) const
73 return get(color, lyxrc.use_system_colors);
77 /// get the given color
78 QColor ColorCache::get(Color const & color, bool syscolors) const
81 const_cast<ColorCache *>(this)->init();
82 if (color <= Color_ignore && color.mergeColor == Color_ignore) {
83 QPalette::ColorRole const cr = role(color.baseColor);
84 if (syscolors && cr != QPalette::NoRole) {
85 static QColor const white = Qt::white;
86 QColor const c = pal_.brush(QPalette::Active, cr).color();
87 if (cr == QPalette::Base && c == white)
88 return lcolors_[color.baseColor];
92 return lcolors_[color.baseColor];
94 if (color.mergeColor != Color_ignore) {
95 // FIXME: This would ideally be done in the Color class, but
96 // that means that we'd have to use the Qt code in the core.
97 QColor base_color = get(color.baseColor, syscolors).toRgb();
98 QColor merge_color = get(color.mergeColor, syscolors).toRgb();
100 (base_color.red() + merge_color.red()) / 2,
101 (base_color.green() + merge_color.green()) / 2,
102 (base_color.blue() + merge_color.blue()) / 2);
105 return QColor(lcolor.getX11Name(color.baseColor).c_str());
109 bool ColorCache::isSystem(ColorCode const color) const
111 QPalette::ColorRole const cr = role(color);
112 if (cr == QPalette::Base) {
113 static QColor const white = Qt::white;
114 return pal_.brush(QPalette::Active, cr).color() != white;
116 return cr != QPalette::NoRole;
120 QColor const rgb2qcolor(RGBColor const & rgb)
122 return QColor(rgb.r, rgb.g, rgb.b);