mirror-chatterino2/src/singletons/Theme.cpp

236 lines
7.3 KiB
C++
Raw Normal View History

2017-01-18 01:04:54 +01:00
2018-07-15 14:03:41 +02:00
#include "singletons/Theme.hpp"
2018-11-25 15:02:48 +01:00
#include "Application.hpp"
#include "singletons/Resources.hpp"
2017-01-18 04:52:47 +01:00
#include <QColor>
2016-12-30 19:16:48 +01:00
#include <cmath>
namespace {
double getMultiplierByTheme(const QString &themeName)
{
if (themeName == "Light")
{
return 0.8;
}
2023-03-17 20:53:03 +01:00
if (themeName == "White")
{
return 1.0;
}
2023-03-17 20:53:03 +01:00
if (themeName == "Black")
{
return -1.0;
}
2023-03-17 20:53:03 +01:00
if (themeName == "Dark")
{
return -0.8;
}
2023-03-17 20:53:03 +01:00
return -0.8; // default: Dark
}
} // namespace
2017-04-14 17:52:22 +02:00
namespace chatterino {
2017-01-18 21:30:23 +01:00
bool Theme::isLightTheme() const
{
return this->isLight_;
}
2018-06-28 20:03:04 +02:00
Theme::Theme()
{
this->update();
this->themeName.connectSimple(
[this](auto) {
this->update();
},
false);
2017-02-02 01:23:26 +01:00
}
void Theme::update()
{
2023-03-17 20:53:03 +01:00
this->actuallyUpdate(getMultiplierByTheme(this->themeName.getValue()));
this->updated.invoke();
}
// multiplier: 1 = white, 0.8 = light, -0.8 dark, -1 black
2023-03-17 20:53:03 +01:00
void Theme::actuallyUpdate(double multiplier)
2016-12-30 19:16:48 +01:00
{
this->isLight_ = multiplier > 0;
2018-01-27 21:13:22 +01:00
const auto isLight = this->isLightTheme();
2018-05-26 17:11:09 +02:00
2023-03-17 20:53:03 +01:00
auto getGray = [multiplier](double l, double a = 1.0) {
return QColor::fromHslF(0, 0, ((l - 0.5) * multiplier) + 0.5, a);
};
/// WINDOW
#ifdef Q_OS_LINUX
2023-03-17 20:53:03 +01:00
this->window.background = isLight ? "#fff" : QColor(61, 60, 56);
#else
2023-03-17 20:53:03 +01:00
this->window.background = isLight ? "#fff" : "#111";
#endif
2023-03-17 20:53:03 +01:00
this->window.text = isLight ? "#000" : "#eee";
2023-03-17 20:53:03 +01:00
/// TABSs
if (isLight)
{
this->tabs.regular = {.text = "#444",
.backgrounds = {"#fff", "#eee", "#fff"},
.line = {"#fff", "#fff", "#fff"}};
this->tabs.newMessage = {.text = "#222",
.backgrounds = {"#fff", "#eee", "#fff"},
.line = {"#bbb", "#bbb", "#bbb"}};
this->tabs.highlighted = {.text = "#000",
.backgrounds = {"#fff", "#eee", "#fff"},
.line = {"#f00", "#f00", "#f00"}};
this->tabs.selected = {
.text = "#000",
.backgrounds = {"#b4d7ff", "#b4d7ff", "#b4d7ff"},
.line = {this->accent, this->accent, this->accent}};
}
2023-03-17 20:53:03 +01:00
else
{
this->tabs.regular = {.text = "#aaa",
.backgrounds{"#252525", "#252525", "#252525"},
.line = {"#444", "#444", "#444"}};
this->tabs.newMessage = {.text = "#eee",
.backgrounds{"#252525", "#252525", "#252525"},
.line = {"#888", "#888", "#888"}};
this->tabs.highlighted = {.text = "#eee",
.backgrounds{"#252525", "#252525", "#252525"},
.line = {"#ee6166", "#ee6166", "#ee6166"}};
this->tabs.selected = {
.text = "#fff",
.backgrounds{"#555", "#555", "#555"},
.line = {this->accent, this->accent, this->accent}};
}
this->tabs.dividerLine = this->tabs.selected.backgrounds.regular;
// Message
2023-03-17 20:53:03 +01:00
this->messages.textColors.caret = isLight ? "#000" : "#fff";
this->messages.textColors.regular = isLight ? "#000" : "#fff";
this->messages.textColors.link = QColor(66, 134, 244);
this->messages.textColors.system = QColor(140, 127, 127);
this->messages.textColors.chatPlaceholder =
2023-03-17 20:53:03 +01:00
isLight ? QColor(175, 159, 159) : QColor(93, 85, 85);
2023-03-17 20:53:03 +01:00
this->messages.backgrounds.regular = getGray(1);
this->messages.backgrounds.alternate = getGray(0.96);
2023-03-17 20:53:03 +01:00
this->messages.disabled = getGray(1, 0.6);
2023-03-17 20:53:03 +01:00
int complementaryGray = isLight ? 20 : 230;
this->messages.highlightAnimationStart =
QColor(complementaryGray, complementaryGray, complementaryGray, 110);
this->messages.highlightAnimationEnd =
QColor(complementaryGray, complementaryGray, complementaryGray, 0);
// Scrollbar
this->scrollbars.background = QColor(0, 0, 0, 0);
2023-03-17 20:53:03 +01:00
this->scrollbars.thumb = getGray(0.70);
this->scrollbars.thumbSelected = getGray(0.65);
// Selection
this->messages.selection =
2023-03-17 20:53:03 +01:00
isLight ? QColor(0, 0, 0, 64) : QColor(255, 255, 255, 64);
2023-03-17 20:53:03 +01:00
// Splits
if (isLight)
2018-10-21 13:43:02 +02:00
{
2023-03-17 20:53:03 +01:00
this->splits.dropTargetRect = QColor(255, 255, 255, 0);
2018-10-21 13:43:02 +02:00
}
else
{
2023-03-17 20:53:03 +01:00
this->splits.dropTargetRect = QColor(0, 148, 255, 0);
2018-06-07 17:43:21 +02:00
}
2023-03-17 20:53:03 +01:00
this->splits.dropTargetRectBorder = QColor(0, 148, 255, 0);
this->splits.dropPreview = QColor(0, 148, 255, 48);
this->splits.dropPreviewBorder = QColor(0, 148, 255);
this->splits.resizeHandle = QColor(0, 148, 255, isLight ? 255 : 112);
this->splits.resizeHandleBackground =
QColor(0, 148, 255, isLight ? 80 : 32);
this->splits.header.background = getGray(isLight ? 1 : 0.9);
this->splits.header.border = getGray(isLight ? 1 : 0.85);
2018-01-02 02:15:11 +01:00
this->splits.header.text = this->messages.textColors.regular;
2023-03-17 20:53:03 +01:00
this->splits.header.focusedBackground = getGray(isLight ? 0.95 : 0.79);
this->splits.header.focusedBorder = getGray(isLight ? 0.90 : 0.78);
this->splits.header.focusedText = QColor::fromHsvF(
0.58388, isLight ? 1.0 : 0.482, isLight ? 0.6375 : 1.0);
2018-01-02 02:15:11 +01:00
2023-03-17 20:53:03 +01:00
this->splits.input.background = getGray(0.95);
2018-01-02 02:15:11 +01:00
this->splits.input.text = this->messages.textColors.regular;
this->splits.input.styleSheet =
"background:" + this->splits.input.background.name() + ";" +
2023-03-17 20:53:03 +01:00
"border:" + this->tabs.selected.backgrounds.regular.name() + ";" +
"color:" + this->messages.textColors.regular.name() + ";" +
2018-06-07 17:43:21 +02:00
"selection-background-color:" +
2023-03-17 20:53:03 +01:00
(isLight ? "#68B1FF" : this->tabs.selected.backgrounds.regular.name());
2017-09-12 19:06:16 +02:00
2018-11-25 15:02:48 +01:00
this->splits.messageSeperator =
isLight ? QColor(127, 127, 127) : QColor(60, 60, 60);
2023-03-17 20:53:03 +01:00
this->splits.background = getGray(1);
// Copy button
2023-03-17 20:53:03 +01:00
if (isLight)
{
this->buttons.copy = getResources().buttons.copyDark;
this->buttons.pin = getResources().buttons.pinDisabledDark;
}
else
{
this->buttons.copy = getResources().buttons.copyLight;
this->buttons.pin = getResources().buttons.pinDisabledLight;
}
2017-09-12 19:06:16 +02:00
}
2023-03-17 20:53:03 +01:00
void Theme::normalizeColor(QColor &color) const
2017-01-18 01:04:54 +01:00
{
2018-11-25 15:02:48 +01:00
if (this->isLightTheme())
2018-10-21 13:43:02 +02:00
{
if (color.lightnessF() > 0.5)
{
2018-06-01 14:20:46 +02:00
color.setHslF(color.hueF(), color.saturationF(), 0.5);
}
2018-08-06 21:17:03 +02:00
if (color.lightnessF() > 0.4 && color.hueF() > 0.1 &&
2018-10-21 13:43:02 +02:00
color.hueF() < 0.33333)
{
2018-08-06 21:17:03 +02:00
color.setHslF(color.hueF(), color.saturationF(),
color.lightnessF() - sin((color.hueF() - 0.1) /
(0.3333 - 0.1) * 3.14159) *
color.saturationF() * 0.4);
}
2018-10-21 13:43:02 +02:00
}
else
{
if (color.lightnessF() < 0.5)
{
2018-06-01 14:20:46 +02:00
color.setHslF(color.hueF(), color.saturationF(), 0.5);
2017-08-12 12:09:26 +02:00
}
2018-08-06 21:17:03 +02:00
if (color.lightnessF() < 0.6 && color.hueF() > 0.54444 &&
2018-10-21 13:43:02 +02:00
color.hueF() < 0.83333)
{
2018-01-12 23:09:05 +01:00
color.setHslF(
color.hueF(), color.saturationF(),
2018-08-06 21:17:03 +02:00
color.lightnessF() + sin((color.hueF() - 0.54444) /
(0.8333 - 0.54444) * 3.14159) *
2018-01-12 23:09:05 +01:00
color.saturationF() * 0.4);
2017-08-12 12:09:26 +02:00
}
}
2016-12-30 19:16:48 +01:00
}
2018-11-25 15:02:48 +01:00
Theme *getTheme()
{
return getApp()->themes;
}
2017-04-14 17:52:22 +02:00
} // namespace chatterino