Skip to content

Commit e247e68

Browse files
committed
Add a few more features to ColorUtils
1 parent 8a5ddf6 commit e247e68

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

modules/Lith/Core/colortheme.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "colortheme.h"
2+
#include "colorutils.h"
23

34
QString ColorTheme::getIcon(const QString& name) {
45
switch (m_group) {

modules/Lith/Core/colorutils.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <QObject>
55
#include <QQmlEngine>
66
#include <QColor>
7+
#include <QRandomGenerator>
8+
79

810
class ColorUtils : public QObject {
911
Q_OBJECT
@@ -58,6 +60,33 @@ class ColorUtils : public QObject {
5860
}
5961
// If used with light mode, this will make things lighter and vice versa.
6062
Q_INVOKABLE static QColor daytimeModeAdjust(QColor c, float ratio);
63+
64+
65+
Q_INVOKABLE static QColor brighten(QColor input, float factor = 0.0F) {
66+
// TODO this is probably not too useful
67+
factor = std::clamp(factor, -1.0F, 1.0F);
68+
HSLA hsla(input);
69+
float lightnessRange = qMin(hsla.l, (1.0F - hsla.l));
70+
hsla.l += factor * lightnessRange;
71+
return hsla.toColor();
72+
}
73+
74+
Q_INVOKABLE static QColor setLightness(QColor input, float newL) {
75+
HSLA hsla(input);
76+
hsla.l = std::clamp(newL, 0.0F, 1.0F);
77+
return hsla.toColor();
78+
}
79+
80+
Q_INVOKABLE static QColor setSaturation(QColor input, float newS) {
81+
HSLA hsla(input);
82+
hsla.s = std::clamp(newS, 0.0F, 1.0F);
83+
return hsla.toColor();
84+
}
85+
86+
Q_INVOKABLE static QColor random() {
87+
auto* gen = QRandomGenerator::system();
88+
return QColor::fromRgb(gen->bounded(256), gen->bounded(256), gen->bounded(256));
89+
}
6190
};
6291

6392
#endif // COLORUTILS_H

0 commit comments

Comments
 (0)