From 390b24514cbc63f2f1840a43345ec4dda7f4aaf2 Mon Sep 17 00:00:00 2001 From: luisangelsm Date: Mon, 12 Jan 2026 18:48:06 +0100 Subject: [PATCH] Add helpers to tint svg files --- common/themes/icon_utils.cpp | 75 ++++++++++++++++++++++++++++++++++++ common/themes/icon_utils.h | 20 ++++++++++ 2 files changed, 95 insertions(+) create mode 100644 common/themes/icon_utils.cpp create mode 100644 common/themes/icon_utils.h diff --git a/common/themes/icon_utils.cpp b/common/themes/icon_utils.cpp new file mode 100644 index 00000000..826b31ef --- /dev/null +++ b/common/themes/icon_utils.cpp @@ -0,0 +1,75 @@ +#include "icon_utils.h" + +#include "yacreader_global.h" + +QString recolorSvgXML(QString &svg, + const QString &placeHolder, + const QColor &color) +{ + return svg.replace(placeHolder, color.name(QColor::HexRgb), Qt::CaseInsensitive); +} + +QString readSvg(const QString &resourcePath) +{ + QFile in(resourcePath); + if (!in.open(QIODevice::ReadOnly | QIODevice::Text)) { + qWarning() << "Failed to open SVG resource:" << resourcePath; + return {}; + } + + QString svg = QString::fromUtf8(in.readAll()); + in.close(); + + return svg; +} + +QString writeSvg(const QString &svg, const QString &resourcePath, const QString &themeName, const QString &suffix = QString()) +{ + const QString basePath = YACReader::getSettingsPath() + "/themes/" + themeName; + + QDir().mkpath(basePath); + + QString fileName = QFileInfo(resourcePath).completeBaseName(); + if (!suffix.isEmpty()) { + fileName += suffix; + } + fileName += "." + QFileInfo(resourcePath).suffix(); + const QString outPath = basePath + "/" + fileName; + + QFile out(outPath); + if (!out.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) { + qWarning() << "Failed to write SVG:" << outPath; + return {}; + } + + out.write(svg.toUtf8()); + out.close(); + + return outPath; +} + +QString recoloredSvgToThemeFile(const QString &resourcePath, + const QColor &color1, // #f0f (magenta) + const QColor &color2, // #0ff (cyan) + const QString &themeName, + const QString &suffix) +{ + auto svg = readSvg(resourcePath); + + recolorSvgXML(svg, "#f0f", color1); + recolorSvgXML(svg, "#0ff", color2); + + return writeSvg(svg, resourcePath, themeName, suffix); +} + +QString recoloredSvgToThemeFile(const QString &resourcePath, + const QColor &color, // #f0f (magenta) + const QString &themeName, + const QString &suffix) +{ + auto svg = readSvg(resourcePath); + + recolorSvgXML(svg, "#f0f", color); + + return writeSvg(svg, resourcePath, themeName, suffix); +} diff --git a/common/themes/icon_utils.h b/common/themes/icon_utils.h new file mode 100644 index 00000000..014a1b69 --- /dev/null +++ b/common/themes/icon_utils.h @@ -0,0 +1,20 @@ +#ifndef ICON_UTILS_H +#define ICON_UTILS_H + +#include + +QString recolorSvgXML(QString &svg, const QString &placeHolder, const QColor &color); +QString readSvg(const QString &resourcePath); +QString writeSvg(const QString &svg, const QString &resourcePath, const QString &themeName); +QString recoloredSvgToThemeFile(const QString &resourcePath, + const QColor &color1, // #f0f (magenta) + const QColor &color2, // #0ff (cyan) + const QString &themeName, + const QString &suffix = QString()); +QString recoloredSvgToThemeFile(const QString &resourcePath, + const QColor &color, // #f0f (magenta) + const QString &themeName, + const QString &suffix = QString()); +QString createMenuArrowSvg(const QColor &color, const QString &themeName); + +#endif // ICON_UTILS_H