mirror-chatterino2/src/colorscheme.cpp

183 lines
5.5 KiB
C++
Raw Normal View History

2017-01-18 01:04:54 +01:00
#define LOOKUP_COLOR_COUNT 360
2017-06-11 09:31:45 +02:00
#include "colorscheme.hpp"
#include "windowmanager.hpp"
2017-01-18 04:52:47 +01:00
#include <QColor>
2016-12-30 19:16:48 +01:00
2017-04-14 17:52:22 +02:00
namespace chatterino {
2017-01-18 21:30:23 +01:00
namespace detail {
double getMultiplierByTheme(const std::string &themeName)
2017-02-02 01:23:26 +01:00
{
if (themeName == "Light") {
return 0.8;
} else if (themeName == "White") {
return 1.0;
} else if (themeName == "Black") {
return -1.0;
} else if (themeName == "Dark") {
return -0.8;
}
2017-02-02 01:23:26 +01:00
return -0.8;
}
2017-02-02 01:23:26 +01:00
} // namespace detail
ColorScheme::ColorScheme(WindowManager &windowManager)
2017-07-02 12:36:50 +02:00
: themeName("/appearance/theme/name", "Dark")
, themeHue("/appearance/theme/hue", 0.0)
{
this->update();
2017-07-02 12:36:50 +02:00
this->themeName.getValueChangedSignal().connect([=](const auto &) {
this->update(); //
});
2017-07-02 12:36:50 +02:00
this->themeHue.getValueChangedSignal().connect([=](const auto &) {
this->update(); //
});
this->updated.connect([&windowManager] {
windowManager.repaintVisibleChatWidgets(); //
});
2017-02-02 01:23:26 +01:00
}
2017-04-12 17:46:44 +02:00
void ColorScheme::update()
2017-02-02 01:23:26 +01:00
{
2017-07-02 12:36:50 +02:00
this->setColors(this->themeHue, detail::getMultiplierByTheme(this->themeName));
2017-02-02 01:23:26 +01:00
}
2016-12-30 19:20:04 +01:00
// hue: theme color (0 - 1)
// multiplier: 1 = white, 0.8 = light, -0.8 dark, -1 black
void ColorScheme::setColors(double hue, double multiplier)
2016-12-30 19:16:48 +01:00
{
lightTheme = multiplier > 0;
2017-04-12 17:46:44 +02:00
bool hasDarkBorder = false;
2016-12-30 19:16:48 +01:00
2017-01-05 16:07:20 +01:00
SystemMessageColor = QColor(140, 127, 127);
auto getColor = [multiplier](double h, double s, double l, double a = 1.0) {
return QColor::fromHslF(h, s, (((l - 0.5) * multiplier) + 0.5), a);
2016-12-30 19:20:04 +01:00
};
2017-04-12 17:46:44 +02:00
DropPreviewBackground = getColor(hue, 0.5, 0.5, 0.6);
2017-01-01 02:30:42 +01:00
Text = TextCaret = lightTheme ? QColor(0, 0, 0) : QColor(255, 255, 255);
2016-12-30 19:20:04 +01:00
2017-01-01 02:30:42 +01:00
// tab
2017-04-12 17:46:44 +02:00
if (hasDarkBorder) {
// TabPanelBackground = getColor(hue, 0, 0.8);
// TabBackground = getColor(hue, 0, 0.8);
// TabHoverBackground = getColor(hue, 0, 0.8);
} else {
TabPanelBackground = QColor(255, 255, 255);
TabBackground = QColor(255, 255, 255);
TabHoverBackground = getColor(hue, 0, 0.05);
}
2017-01-01 02:30:42 +01:00
TabSelectedBackground = getColor(hue, 0.5, 0.5);
TabHighlightedBackground = getColor(hue, 0.5, 0.2);
2017-04-12 17:46:44 +02:00
TabNewMessageBackground = QBrush(getColor(hue, 0.5, 0.2), Qt::DiagCrossPattern);
2017-05-27 16:16:39 +02:00
if (hasDarkBorder) {
2017-04-12 17:46:44 +02:00
// TabText = QColor(210, 210, 210);
// TabHoverText = QColor(210, 210, 210);
TabText = QColor(0, 0, 0);
2017-05-27 16:16:39 +02:00
}
2017-01-01 02:30:42 +01:00
TabHoverText = QColor(0, 0, 0);
TabSelectedText = QColor(255, 255, 255);
TabHighlightedText = QColor(0, 0, 0);
// Chat
ChatBackground = getColor(0, 0.1, 1);
ChatHeaderBackground = getColor(0, 0.1, 0.9);
ChatHeaderBorder = getColor(0, 0.1, 0.85);
ChatInputBackground = getColor(0, 0.1, 0.95);
ChatInputBorder = getColor(0, 0.1, 0.9);
2017-01-18 01:04:54 +01:00
2017-01-26 04:26:40 +01:00
ScrollbarBG = ChatBackground;
2017-01-18 01:04:54 +01:00
// generate color lookuptable
2017-01-18 04:33:30 +01:00
fillLookupTableValues(this->middleLookupTable, 0.000, 0.166, 0.66, 0.5);
fillLookupTableValues(this->middleLookupTable, 0.166, 0.333, 0.5, 0.55);
fillLookupTableValues(this->middleLookupTable, 0.333, 0.500, 0.55, 0.45);
fillLookupTableValues(this->middleLookupTable, 0.500, 0.666, 0.45, 0.80);
fillLookupTableValues(this->middleLookupTable, 0.666, 0.833, 0.80, 0.61);
fillLookupTableValues(this->middleLookupTable, 0.833, 1.000, 0.61, 0.66);
fillLookupTableValues(this->minLookupTable, 0.000, 0.166, 0.33, 0.23);
fillLookupTableValues(this->minLookupTable, 0.166, 0.333, 0.23, 0.27);
fillLookupTableValues(this->minLookupTable, 0.333, 0.500, 0.27, 0.23);
fillLookupTableValues(this->minLookupTable, 0.500, 0.666, 0.23, 0.50);
fillLookupTableValues(this->minLookupTable, 0.666, 0.833, 0.50, 0.30);
fillLookupTableValues(this->minLookupTable, 0.833, 1.000, 0.30, 0.33);
2017-01-18 01:04:54 +01:00
2017-01-21 05:42:59 +01:00
// stylesheet
2017-04-12 17:46:44 +02:00
InputStyleSheet = "background:" + ChatInputBackground.name() + ";" +
"border:" + TabSelectedBackground.name() + ";" + "color:" + Text.name() +
";" + "selection-background-color:" + TabSelectedBackground.name();
2017-02-02 01:23:26 +01:00
this->updated();
2017-01-18 01:04:54 +01:00
}
void ColorScheme::fillLookupTableValues(double (&array)[360], double from, double to,
double fromValue, double toValue)
2017-01-18 01:04:54 +01:00
{
double diff = toValue - fromValue;
2017-01-18 01:04:54 +01:00
int start = from * LOOKUP_COLOR_COUNT;
int end = to * LOOKUP_COLOR_COUNT;
int length = end - start;
for (int i = 0; i < length; i++) {
array[start + i] = fromValue + (diff * ((double)i / length));
2017-01-18 01:04:54 +01:00
}
}
2017-04-12 17:46:44 +02:00
void ColorScheme::normalizeColor(QColor &color)
2017-01-18 01:04:54 +01:00
{
// double l = color.lightnessF();
// double s = color.saturationF();
// double x = this->colorLookupTable[std::max(0, color.hue())];
// double newL = (l - 1) * x + 1;
2017-01-18 01:04:54 +01:00
// newL = s * newL + (1 - s) * l;
// newL = newL > 0.5 ? newL : newL / 2 + 0.25;
// color.setHslF(color.hueF(), s, newL);
double l = color.lightnessF();
double s = color.saturationF();
2017-01-18 01:04:54 +01:00
int h = std::max(0, color.hue());
double x = this->middleLookupTable[h];
2017-01-18 01:04:54 +01:00
x = s * 0.5 + (1 - s) * x;
double min = this->minLookupTable[h];
2017-01-18 01:04:54 +01:00
min = (1 - s) * 0.5 + s * min;
double newL;
2017-01-18 01:04:54 +01:00
if (l < x) {
newL = l * ((x - min) / x) + min;
// newL = (l * ((x - min) / x) + min);
// newL = (1 - s) * newL + s * l;
} else {
newL = l;
}
color.setHslF(color.hueF(), s, newL);
// double newL = (l - 1) * x + 1;
2017-01-18 01:04:54 +01:00
// newL = s * newL + (1 - s) * l;
// newL = newL > 0.5 ? newL : newL / 2 + 0.25;
// color.setHslF(color.hueF(), s, newL);
2016-12-30 19:16:48 +01:00
}
2017-04-14 17:52:22 +02:00
} // namespace chatterino