mirror-chatterino2/src/singletons/thememanager.cpp

200 lines
6.4 KiB
C++
Raw Normal View History

2017-01-18 01:04:54 +01:00
#define LOOKUP_COLOR_COUNT 360
2017-12-31 00:50:07 +01:00
#include "thememanager.hpp"
2017-01-18 04:52:47 +01:00
#include <QColor>
2016-12-30 19:16:48 +01:00
#include <cmath>
2017-04-14 17:52:22 +02:00
namespace chatterino {
2017-12-31 22:58:35 +01:00
namespace singletons {
2017-01-18 21:30:23 +01:00
namespace detail {
2018-01-12 23:09:05 +01:00
double getMultiplierByTheme(const QString &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
2017-12-31 00:50:07 +01:00
ThemeManager &ThemeManager::getInstance()
{
static ThemeManager instance;
return instance;
}
2017-12-17 16:45:15 +01:00
2017-12-31 00:50:07 +01:00
ThemeManager::ThemeManager()
2017-07-02 12:36:50 +02:00
: themeName("/appearance/theme/name", "Dark")
, themeHue("/appearance/theme/hue", 0.0)
{
this->update();
this->themeName.connectSimple([this](auto) { this->update(); });
this->themeHue.connectSimple([this](auto) { this->update(); });
2017-02-02 01:23:26 +01:00
}
2017-12-31 00:50:07 +01:00
void ThemeManager::update()
2017-02-02 01:23:26 +01:00
{
2018-01-12 23:09:05 +01:00
this->actuallyUpdate(this->themeHue, detail::getMultiplierByTheme(this->themeName.getValue()));
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
2017-12-31 00:50:07 +01:00
void ThemeManager::actuallyUpdate(double hue, double multiplier)
2016-12-30 19:16:48 +01:00
{
isLight = multiplier > 0;
bool isLightTabs;
QColor themeColor = QColor::fromHslF(hue, 0.5, 0.5);
QColor themeColorNoSat = QColor::fromHslF(hue, 0, 0.5);
qreal sat = 0;
2018-01-27 21:13:22 +01:00
// 0.05;
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);
};
2018-01-15 01:47:31 +01:00
//#ifdef USEWINSDK
// isLightTabs = isLight;
// QColor tabFg = isLight ? "#000" : "#fff";
2018-01-27 21:13:22 +01:00
// this->windowBg = isLight ? "#fff" : getColor(0, sat, 0.9);
2018-01-15 01:47:31 +01:00
//#else
isLightTabs = true;
QColor tabFg = isLightTabs ? "#000" : "#fff";
this->windowBg = "#fff";
2018-01-15 01:47:31 +01:00
//#endif
2016-12-30 19:16:48 +01:00
2018-01-02 02:15:11 +01:00
// Ubuntu style
// TODO: add setting for this
// TabText = QColor(210, 210, 210);
// TabBackground = QColor(61, 60, 56);
// TabHoverText = QColor(210, 210, 210);
// TabHoverBackground = QColor(73, 72, 68);
2017-08-17 14:52:41 +02:00
2018-01-02 02:15:11 +01:00
// message (referenced later)
this->messages.textColors.caret = //
this->messages.textColors.regular = isLight ? "#000" : "#fff";
2017-08-17 14:52:41 +02:00
2018-01-02 02:15:11 +01:00
// tabs
// text, {regular, hover, unfocused}
2018-04-01 16:41:32 +02:00
// this->windowBg = "#ccc";
2018-03-30 16:25:49 +02:00
this->tabs.border = "#999";
this->tabs.regular = {tabFg, {windowBg, blendColors(windowBg, "#999", 0.5), windowBg}};
2017-08-17 14:52:41 +02:00
this->tabs.selected = {"#fff", {themeColor, themeColor, QColor::fromHslF(hue, 0, 0.5)}};
this->tabs.newMessage = {
tabFg,
{QBrush(blendColors(themeColor, windowBg, 0.7), Qt::FDiagPattern),
QBrush(blendColors(themeColor, windowBg, 0.5), Qt::FDiagPattern),
QBrush(blendColors(themeColorNoSat, windowBg, 0.7), Qt::FDiagPattern)}};
2017-08-17 14:52:41 +02:00
this->tabs.highlighted = {
tabFg,
{blendColors(themeColor, windowBg, 0.7), blendColors(themeColor, windowBg, 0.5),
blendColors(themeColorNoSat, windowBg, 0.7)}};
2018-03-30 16:25:49 +02:00
2018-04-01 16:41:32 +02:00
// this->windowBg = "#fff";
2017-01-01 02:30:42 +01:00
2018-01-02 02:15:11 +01:00
// Split
bool flat = isLight;
2017-12-26 16:54:39 +01:00
this->splits.messageSeperator = isLight ? QColor(127, 127, 127) : QColor(80, 80, 80);
2018-01-02 02:15:11 +01:00
this->splits.background = getColor(0, sat, 1);
this->splits.dropPreview = getColor(hue, 0.5, 0.5, 0.6);
// this->splits.border
// this->splits.borderFocused
this->splits.header.background = getColor(0, sat, flat ? 1 : 0.9);
this->splits.header.border = getColor(0, sat, flat ? 1 : 0.85);
this->splits.header.text = this->messages.textColors.regular;
this->splits.input.background = getColor(0, sat, flat ? 0.95 : 0.95);
this->splits.input.border = getColor(0, sat, flat ? 1 : 1);
this->splits.input.text = this->messages.textColors.regular;
this->splits.input.styleSheet =
"background:" + this->splits.input.background.name() + ";" +
"border:" + this->tabs.selected.backgrounds.regular.color().name() + ";" +
"color:" + this->messages.textColors.regular.name() + ";" +
"selection-background-color:" + this->tabs.selected.backgrounds.regular.color().name();
// Message
this->messages.textColors.link = isLight ? QColor(66, 134, 244) : QColor(66, 134, 244);
2018-01-02 02:15:11 +01:00
this->messages.textColors.system = QColor(140, 127, 127);
this->messages.backgrounds.regular = splits.background;
this->messages.backgrounds.highlighted = blendColors(
this->tabs.selected.backgrounds.regular.color(), this->messages.backgrounds.regular, 0.8);
// this->messages.backgrounds.resub
// this->messages.backgrounds.whisper
this->messages.disabled = getColor(0, sat, 1, 0.6);
// this->messages.seperator =
// this->messages.seperatorInner =
2017-01-18 01:04:54 +01:00
2017-08-17 14:52:41 +02:00
// Scrollbar
2018-01-02 02:15:11 +01:00
this->scrollbars.background = getColor(0, sat, 0.94);
this->scrollbars.thumb = getColor(0, sat, 0.80);
this->scrollbars.thumbSelected = getColor(0, sat, 0.7);
2017-01-26 04:26:40 +01:00
2018-01-02 02:15:11 +01:00
// tooltip
this->tooltip.background = QColor(0, 0, 0);
this->tooltip.text = QColor(255, 255, 255);
2017-02-02 01:23:26 +01:00
2017-12-19 02:37:58 +01:00
// Selection
2018-01-02 02:15:11 +01:00
this->messages.selection = isLightTheme() ? QColor(0, 0, 0, 64) : QColor(255, 255, 255, 64);
2017-12-19 02:37:58 +01:00
this->updated.invoke();
2017-01-18 01:04:54 +01:00
}
2017-12-31 00:50:07 +01:00
QColor ThemeManager::blendColors(const QColor &color1, const QColor &color2, qreal ratio)
2017-09-12 19:06:16 +02:00
{
int r = color1.red() * (1 - ratio) + color2.red() * ratio;
int g = color1.green() * (1 - ratio) + color2.green() * ratio;
int b = color1.blue() * (1 - ratio) + color2.blue() * ratio;
return QColor(r, g, b, 255);
}
2017-12-31 00:50:07 +01:00
void ThemeManager::normalizeColor(QColor &color)
2017-01-18 01:04:54 +01:00
{
if (this->isLight) {
if (color.lightnessF() > 0.5f) {
color.setHslF(color.hueF(), color.saturationF(), 0.5f);
}
if (color.lightnessF() > 0.4f && color.hueF() > 0.1 && color.hueF() < 0.33333) {
2018-01-12 23:09:05 +01:00
color.setHslF(
color.hueF(), color.saturationF(),
color.lightnessF() - sin((color.hueF() - 0.1) / (0.3333 - 0.1) * 3.14159) *
color.saturationF() * 0.2);
}
2017-08-12 12:09:26 +02:00
} else {
if (color.lightnessF() < 0.5f) {
color.setHslF(color.hueF(), color.saturationF(), 0.5f);
}
if (color.lightnessF() < 0.6f && color.hueF() > 0.54444 && color.hueF() < 0.83333) {
2018-01-12 23:09:05 +01:00
color.setHslF(
color.hueF(), color.saturationF(),
color.lightnessF() + sin((color.hueF() - 0.54444) / (0.8333 - 0.54444) * 3.14159) *
color.saturationF() * 0.4);
2017-08-12 12:09:26 +02:00
}
}
2016-12-30 19:16:48 +01:00
}
2018-01-12 23:09:05 +01:00
} // namespace singletons
2017-04-14 17:52:22 +02:00
} // namespace chatterino