Something went wrong. Try again.
Bring Windows XP themes to your KDE Plasma environment! xplasma.hotsocket.fyi
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482// SPDX-License-Identifier: GPL-3.0-or-later#include "tint.h"
#include <QStringList>
#include <algorithm>#include <array>#include <cmath>
namespace xpl {
namespace {
// sRGB <-> CIELAB (D65), by the book.struct Lab{ double l, a, b;};
const std::array<double, 256> &linearTable(){ static const std::array<double, 256> table = [] { std::array<double, 256> t{}; for (int i = 0; i < 256; ++i) { const double c = i / 255.0; t[i] = c <= 0.04045 ? c / 12.92 : std::pow((c + 0.055) / 1.055, 2.4); } return t; }(); return table;}
constexpr double kXn = 0.95047, kYn = 1.0, kZn = 1.08883;constexpr double kDelta = 6.0 / 29.0;
double labF(double t){ return t > kDelta * kDelta * kDelta ? std::cbrt(t) : t / (3 * kDelta * kDelta) + 4.0 / 29.0;}
double labFInverse(double t){ return t > kDelta ? t * t * t : 3 * kDelta * kDelta * (t - 4.0 / 29.0);}
Lab toLab(QRgb c){ const auto &lin = linearTable(); const double r = lin[qRed(c)], g = lin[qGreen(c)], b = lin[qBlue(c)]; const double x = 0.4124564 * r + 0.3575761 * g + 0.1804375 * b; const double y = 0.2126729 * r + 0.7151522 * g + 0.0721750 * b; const double z = 0.0193339 * r + 0.1191920 * g + 0.9503041 * b; const double fx = labF(x / kXn), fy = labF(y / kYn), fz = labF(z / kZn); return {116 * fy - 16, 500 * (fx - fy), 200 * (fy - fz)};}
// Linear RGB for a Lab colour; any channel may be out of [0, 1].std::array<double, 3> toLinear(const Lab &lab){ const double fy = (lab.l + 16) / 116; const double x = kXn * labFInverse(fy + lab.a / 500); const double y = kYn * labFInverse(fy); const double z = kZn * labFInverse(fy - lab.b / 200); return {3.2404542 * x - 1.5371385 * y - 0.4985314 * z, -0.9692660 * x + 1.8760108 * y + 0.0415560 * z, 0.0556434 * x - 0.2040259 * y + 1.0572252 * z};}
bool inGamut(const std::array<double, 3> &rgb){ constexpr double e = 1e-4; for (double c : rgb) if (c < -e || c > 1 + e) return false; return true;}
int toByte(double linear){ linear = std::clamp(linear, 0.0, 1.0); const double c = linear <= 0.0031308 ? 12.92 * linear : 1.055 * std::pow(linear, 1 / 2.4) - 0.055; return std::clamp(int(std::lround(c * 255)), 0, 255);}
constexpr double kPi = 3.14159265358979323846;
// How much of a laid colour (Tint::a, b) a lightness takes: all at mid// lightness, falling to none at white and black.double laidAt(double l){ return std::clamp(1 - std::pow((l - 50) / 50, 4), 0.0, 1.0);}
// Below this C*, a colour is grey enough that a tint toward another lays// that colour over it rather than turning a hue it hardly has.constexpr double kNearGrey = 15;
} // namespace
Lch toLch(QRgb colour){ const Lab lab = toLab(colour | 0xff000000u); double h = std::atan2(lab.b, lab.a) * 180 / kPi; if (h < 0) h += 360; return {lab.l, std::hypot(lab.a, lab.b), h};}
QRgb fromLch(const Lch &lch){ const double l = std::clamp(lch.l, 0.0, 100.0); const double h = lch.h * kPi / 180; auto at = [&](double c) { return toLinear({l, c * std::cos(h), c * std::sin(h)}); }; std::array<double, 3> out = at(std::max(0.0, lch.c)); if (!inGamut(out)) { double lo = 0, hi = std::max(0.0, lch.c); for (int i = 0; i < 14; ++i) { const double mid = (lo + hi) / 2; (inGamut(at(mid)) ? lo : hi) = mid; } out = at(lo); } return qRgb(toByte(out[0]), toByte(out[1]), toByte(out[2]));}
Tint Tint::toward(QRgb from, QRgb to){ const Lch a = toLch(from), b = toLch(to); Tint t; if (a.c < kNearGrey) { // Near grey: `to`'s colour laid over, as much as `from`'s lightness // takes it (none for white or black: only lightness follows there). const double laid = laidAt(a.l); const Lab src = toLab(from | 0xff000000u), dst = toLab(to | 0xff000000u); if (laid > 0.05) { t.a = std::clamp((dst.a - src.a) / laid, -128.0, 128.0); t.b = std::clamp((dst.b - src.b) / laid, -128.0, 128.0); } // Its lightness in proportion, so the art's shading keeps its shape // (faded as the colour is: see apply), solved so `from` lands on `to`. if (laid > 0.05 && b.l < a.l && a.l > 0) { t.lscale = std::clamp(1 - (1 - b.l / a.l) / laid, 0.05, 1.0); } else if (laid > 0.05 && b.l > a.l) { const double reach = std::min(99.9, a.l + (b.l - a.l) / laid); t.lscale = std::clamp((100 - a.l) / (100 - reach), 1.0, 20.0); } return t; } // (Lightness moves with colourfulness -- in full from C* 25: see apply.) const double weight = std::clamp(a.c / 25.0, 0.0, 1.0); t.lightness = weight > 0 ? std::clamp((b.l - a.l) / weight, -50.0, 50.0) : 0; if (a.c >= 1) { double turn = std::fmod(b.h - a.h + 540.0, 360.0) - 180.0; t.hue = std::clamp(turn, -180.0, 180.0); t.chroma = std::clamp(b.c / a.c, 0.0, 2.0); } return t;}
bool Tint::isIdentity() const{ return hue == 0 && chroma == 1 && lightness == 0 && a == 0 && b == 0 && lscale == 1;}
std::optional<Tint> Tint::parse(const QString &text){ const QStringList parts = text.split(QLatin1Char(',')); if (parts.size() != 3) return std::nullopt; bool ok[3] = {}; Tint t; t.hue = parts[0].trimmed().toDouble(&ok[0]); t.chroma = parts[1].trimmed().toDouble(&ok[1]); t.lightness = parts[2].trimmed().toDouble(&ok[2]); if (!ok[0] || !ok[1] || !ok[2] || !std::isfinite(t.hue) || !std::isfinite(t.chroma) || !std::isfinite(t.lightness)) return std::nullopt; t.hue = std::clamp(t.hue, -180.0, 180.0); t.chroma = std::clamp(t.chroma, 0.0, 2.0); t.lightness = std::clamp(t.lightness, -50.0, 50.0); return t;}
QString Tint::toString() const{ return QStringLiteral("%1,%2,%3").arg(hue).arg(chroma).arg(lightness);}
// --- specs ----------------------------------------------------------------------------
namespace {
QString formatNumber(double v){ return QString::number(v, 'g', 6);}
QString selectorText(const TintRule &r){ QString s; if (!r.sub.isEmpty()) s += r.sub + QLatin1String("::"); s += r.cls.isEmpty() ? QStringLiteral("*") : r.cls; if (!r.part.isEmpty()) s += QLatin1Char('.') + r.part; if (!r.state.isEmpty()) s += QLatin1Char('(') + r.state + QLatin1Char(')'); return s;}
bool isAny(const QString &cls){ return cls.isEmpty() || cls == QLatin1String("*");}
// XP's signal colours, kept under any tint that doesn't name them: green to// begin (the Start button, All Programs' arrow), red for high impact (Close,// Turn Off Computer), yellow for less (Log Off). As rules before a spec's// own, so one as specific, or more, still turns them.const QList<TintRule> &signalRules(){ static const QList<TintRule> rules = [] { QList<TintRule> out; auto keep = [&](const char *sub, const char *cls, const char *part) { TintRule r; r.sub = QLatin1String(sub); r.cls = QLatin1String(cls); r.part = QLatin1String(part); out.append(r); }; keep("start", "button", ""); for (const char *part : {"closebutton", "smallclosebutton", "mdiclosebutton"}) keep("", "window", part); keep("", "startpanel", "logoffbuttons"); keep("", "startpanel", "moreprogramsarrow"); return out; }(); return rules;}
} // namespace
TintSpec TintSpec::uniform(const Tint &tint){ TintSpec spec; TintRule all; all.cls = QStringLiteral("*"); all.tint = tint; spec.rules.append(all); TintRule start; start.sub = QStringLiteral("start"); start.cls = QStringLiteral("button"); spec.rules.append(start); for (const char *part : {"closebutton", "smallclosebutton", "mdiclosebutton"}) { TintRule close; close.cls = QStringLiteral("window"); close.part = QLatin1String(part); spec.rules.append(close); } return spec;}
std::optional<TintSpec> TintSpec::parse(const QString &text){ if (!text.contains(QLatin1Char(':'))) { if (text.trimmed().isEmpty()) return TintSpec(); if (const auto t = Tint::parse(text)) return uniform(*t); return std::nullopt; } TintSpec spec; for (const QString &chunk : text.split(QLatin1Char(';'))) { if (chunk.trimmed().isEmpty()) continue; // (The selector's own "::" isn't the separator: the last ':' is.) const int colon = int(chunk.lastIndexOf(QLatin1Char(':'))); if (colon < 0 || (colon > 0 && chunk.at(colon - 1) == QLatin1Char(':'))) return std::nullopt; TintRule rule; const QString selector = chunk.left(colon).trimmed().toLower(); QString rest = selector; if (const int sub = int(rest.indexOf(QLatin1String("::"))); sub >= 0) { rule.sub = rest.left(sub); rest = rest.mid(sub + 2); } if (const int open = int(rest.indexOf(QLatin1Char('('))); open >= 0) { if (!rest.endsWith(QLatin1Char(')'))) return std::nullopt; rule.state = rest.mid(open + 1, rest.size() - open - 2); rest = rest.left(open); } if (const int dot = int(rest.indexOf(QLatin1Char('.'))); dot >= 0) { rule.part = rest.mid(dot + 1); rest = rest.left(dot); } rule.cls = rest.isEmpty() ? QStringLiteral("*") : rest; for (const QString &word : chunk.mid(colon + 1).split(QLatin1Char(' '), Qt::SkipEmptyParts)) { const int eq = int(word.indexOf(QLatin1Char('='))); if (eq < 0) continue; // ("keep", and words a later build may add) bool ok = false; const double v = word.mid(eq + 1).toDouble(&ok); if (!ok || !std::isfinite(v)) continue; const QString key = word.left(eq).toLower(); if (key == QLatin1String("h")) rule.tint.hue = std::clamp(v, -180.0, 180.0); else if (key == QLatin1String("c")) rule.tint.chroma = std::clamp(v, 0.0, 2.0); else if (key == QLatin1String("l")) rule.tint.lightness = std::clamp(v, -50.0, 50.0); else if (key == QLatin1String("a")) rule.tint.a = std::clamp(v, -128.0, 128.0); else if (key == QLatin1String("b")) rule.tint.b = std::clamp(v, -128.0, 128.0); else if (key == QLatin1String("ls")) rule.tint.lscale = std::clamp(v, 0.05, 20.0); } spec.rules.append(rule); } return spec;}
QString TintSpec::toString() const{ QStringList out; for (const TintRule &r : rules) { QString tint = r.tint.isIdentity() ? QStringLiteral("keep") : QStringLiteral("h=%1 c=%2 l=%3") .arg(formatNumber(r.tint.hue), formatNumber(r.tint.chroma), formatNumber(r.tint.lightness)); if (r.tint.a != 0 || r.tint.b != 0) tint += QStringLiteral(" a=%1 b=%2").arg(formatNumber(r.tint.a), formatNumber(r.tint.b)); if (r.tint.lscale != 1) tint += QStringLiteral(" ls=%1").arg(formatNumber(r.tint.lscale)); out << selectorText(r) + QLatin1String(": ") + tint; } return out.join(QLatin1String("; "));}
bool TintSpec::isIdentity() const{ for (const TintRule &r : rules) if (!r.tint.isIdentity()) return false; return true;}
std::pair<Tint, int> TintSpec::forSection(QStringView sub, QStringView cls, QStringView part, QStringView state) const{ std::pair<Tint, int> best{Tint(), -1}; if (rules.isEmpty()) return best; auto consider = [&](const TintRule &r) { if ((!r.sub.isEmpty() && r.sub != sub) || (!isAny(r.cls) && r.cls != cls) || (!r.part.isEmpty() && r.part != part) || (!r.state.isEmpty() && r.state != state)) return; const int score = (r.sub.isEmpty() ? 0 : 8) + (isAny(r.cls) ? 0 : 4) + (r.part.isEmpty() ? 0 : 2) + (r.state.isEmpty() ? 0 : 1); if (score >= best.second) best = {r.tint, score}; }; for (const TintRule &r : signalRules()) consider(r); for (const TintRule &r : rules) consider(r); return best;}
Tint TintSpec::base() const{ for (const TintRule &r : rules) if (r.sub.isEmpty() && isAny(r.cls) && r.part.isEmpty() && r.state.isEmpty()) return r.tint; return Tint();}
TintSpec TintSpec::withBase(const Tint &tint) const{ if (rules.isEmpty()) return uniform(tint); TintSpec spec = *this; for (TintRule &r : spec.rules) if (r.sub.isEmpty() && isAny(r.cls) && r.part.isEmpty() && r.state.isEmpty()) { r.tint = tint; return spec; } TintRule all; all.cls = QStringLiteral("*"); all.tint = tint; spec.rules.prepend(all); return spec;}
bool TintSpec::setRule(const QString &selector, const Tint &tint){ const auto parsed = parse(selector + QLatin1String(": keep")); if (!parsed || parsed->rules.size() != 1) return false; TintRule rule = parsed->rules.first(); // (Not QList::removeIf: the core builds for Qt 5 too.) rules.erase(std::remove_if(rules.begin(), rules.end(), [&](const TintRule &r) { return r.sub == rule.sub && (r.cls == rule.cls || (isAny(r.cls) && isAny(rule.cls))) && r.part == rule.part && r.state == rule.state; }), rules.end()); if (!tint.isIdentity()) { rule.tint = tint; rules.append(rule); } return true;}
// --- the mapping ------------------------------------------------------------------------
Tinter::Tinter(const Tint &tint) : m_tint(tint){}
QRgb Tinter::apply(QRgb colour) const{ const QRgb rgb = colour | 0xff000000u; { QMutexLocker lock(&m_lock); if (auto it = m_memo.constFind(rgb); it != m_memo.cend()) return (it.value() & 0x00ffffffu) | (colour & 0xff000000u); } const Lab lab = toLab(rgb); const double c = std::hypot(lab.a, lab.b); const double h = std::atan2(lab.b, lab.a) + m_tint.hue * kPi / 180; // A laid colour, as much as this lightness takes. const double laid = m_tint.a != 0 || m_tint.b != 0 ? laidAt(lab.l) : 0; // Lightness moves with colourfulness -- none for greys, all from C* 25 // up -- or as far as a colour is laid. const double weight = std::max(std::clamp(c / 25.0, 0.0, 1.0), laid); // (In proportion, faded toward white and black as a laid colour is: // highlights and the art's white glyphs keep.) double scaled = lab.l; if (m_tint.lscale < 1) scaled *= m_tint.lscale; else if (m_tint.lscale > 1) scaled = 100 - (100 - scaled) / m_tint.lscale; scaled = lab.l + (scaled - lab.l) * (m_tint.lscale != 1 ? laidAt(lab.l) : 0.0); const double l = std::clamp(scaled + m_tint.lightness * weight, 0.0, 100.0); const double chroma = c * m_tint.chroma; const double ta = chroma * std::cos(h) + m_tint.a * laid, tb = chroma * std::sin(h) + m_tint.b * laid; // (Out of gamut, the colour (a*, b*) shortens, its hue and lightness kept.) auto at = [&](double scale) { return toLinear({l, ta * scale, tb * scale}); }; std::array<double, 3> out = at(1); if (!inGamut(out)) { double lo = 0, hi = 1; for (int i = 0; i < 14; ++i) { const double mid = (lo + hi) / 2; (inGamut(at(mid)) ? lo : hi) = mid; } out = at(lo); } const QRgb result = qRgb(toByte(out[0]), toByte(out[1]), toByte(out[2])); { QMutexLocker lock(&m_lock); m_memo.insert(rgb, result); } return (result & 0x00ffffffu) | (colour & 0xff000000u);}
void Tinter::apply(QImage &image, const QSet<QRgb> &keep) const{ if (image.isNull()) return; if (image.format() != QImage::Format_ARGB32) image = image.convertToFormat(QImage::Format_ARGB32); for (int y = 0; y < image.height(); ++y) { auto *row = reinterpret_cast<QRgb *>(image.scanLine(y)); for (int x = 0; x < image.width(); ++x) if (!keep.contains(row[x] & 0x00ffffffu)) row[x] = apply(row[x]); }}
} // namespace xpl