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.
13 #include "ColorCache.h"
24 QPalette::ColorRole role(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_selectionmath:
49 case Color_selectiontext:
50 return QPalette::HighlightedText;
54 return QPalette::Link;
56 return QPalette::NoRole;
63 void ColorCache::init()
65 for (int col = 0; col <= Color_ignore; ++col) {
66 lcolors_[col] = QColor(lcolor.getX11HexName(ColorCode(col), isDarkMode()).c_str());
73 /// get the given color
74 QColor ColorCache::get(Color const & color) const
76 return get(color, lyxrc.use_system_colors);
80 /// get the given color
81 QColor ColorCache::get(Color const & color, bool syscolors) const
84 const_cast<ColorCache *>(this)->init();
85 if (color <= Color_ignore && color.mergeColor == Color_ignore) {
86 QPalette::ColorRole const cr = role(color.baseColor);
87 if (syscolors && cr != QPalette::NoRole) {
88 static QColor const white = Qt::white;
89 QColor const c = pal_.brush(QPalette::Active, cr).color();
90 if (cr == QPalette::Base && c == white)
91 return lcolors_[color.baseColor];
95 return lcolors_[color.baseColor];
97 if (color.mergeColor != Color_ignore) {
98 // FIXME: This would ideally be done in the Color class, but
99 // that means that we'd have to use the Qt code in the core.
100 QColor base_color = get(color.baseColor, syscolors).toRgb();
101 QColor merge_color = get(color.mergeColor, syscolors).toRgb();
103 (base_color.red() + merge_color.red()) / 2,
104 (base_color.green() + merge_color.green()) / 2,
105 (base_color.blue() + merge_color.blue()) / 2);
108 return QColor(lcolor.getX11HexName(color.baseColor, isDarkMode()).c_str());
112 bool ColorCache::isSystem(ColorCode const color) const
114 QPalette::ColorRole const cr = role(color);
115 if (cr == QPalette::Base) {
116 static QColor const white = Qt::white;
117 return pal_.brush(QPalette::Active, cr).color() != white;
119 return cr != QPalette::NoRole;
123 bool ColorCache::isDarkMode() const
125 QPalette palette = QPalette();
126 QColor text_color = palette.color(QPalette::Active, QPalette::WindowText);
127 QColor bg_color = palette.color(QPalette::Active, QPalette::Window);
129 return (text_color.black() < bg_color.black());
133 QColor const rgb2qcolor(RGBColor const & rgb)
135 return QColor(rgb.r, rgb.g, rgb.b);