feat: change *.svg color

This commit is contained in:
XMuli 2022-07-30 20:20:17 +08:00
parent f11a649101
commit e206c3594c
No known key found for this signature in database
GPG Key ID: 9554B5DD5B8E986A
9 changed files with 363 additions and 0 deletions

73
ExChangeSVGColor/.gitignore vendored Normal file
View File

@ -0,0 +1,73 @@
# This file is used to ignore files which are generated
# ----------------------------------------------------------------------------
*~
*.autosave
*.a
*.core
*.moc
*.o
*.obj
*.orig
*.rej
*.so
*.so.*
*_pch.h.cpp
*_resource.rc
*.qm
.#*
*.*#
core
!core/
tags
.DS_Store
.directory
*.debug
Makefile*
*.prl
*.app
moc_*.cpp
ui_*.h
qrc_*.cpp
Thumbs.db
*.res
*.rc
/.qmake.cache
/.qmake.stash
# qtcreator generated files
*.pro.user*
# xemacs temporary files
*.flc
# Vim temporary files
.*.swp
# Visual Studio generated files
*.ib_pdb_index
*.idb
*.ilk
*.pdb
*.sln
*.suo
*.vcproj
*vcproj.*.*.user
*.ncb
*.sdf
*.opensdf
*.vcxproj
*vcxproj.*
# MinGW generated files
*.Debug
*.Release
# Python byte code
*.pyc
# Binaries
# --------
*.dll
*.exe

View File

@ -0,0 +1,68 @@
cmake_minimum_required(VERSION 3.5)
project(ChangeSVGColor VERSION 0.1 LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# QtCreator supports the following variables for Android, which are identical to qmake Android variables.
# Check https://doc.qt.io/qt/deployment-android.html for more information.
# They need to be set before the find_package( ...) calls below.
#if(ANDROID)
# set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
# if (ANDROID_ABI STREQUAL "armeabi-v7a")
# set(ANDROID_EXTRA_LIBS
# ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libcrypto.so
# ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libssl.so)
# endif()
#endif()
find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets Svg Xml REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets Svg Xml REQUIRED)
set(PROJECT_SOURCES
main.cpp
widget.cpp
widget.h
resource.qrc
)
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(ExQRect
MANUAL_FINALIZATION
${PROJECT_SOURCES}
)
else()
if(ANDROID)
add_library(ExQRect SHARED
${PROJECT_SOURCES}
)
else()
add_executable(ExQRect
${PROJECT_SOURCES}
)
endif()
endif()
target_link_libraries(ExQRect PRIVATE
Qt${QT_VERSION_MAJOR}::Widgets
Qt${QT_VERSION_MAJOR}::Svg
Qt${QT_VERSION_MAJOR}::Xml
)
set_target_properties(ExQRect PROPERTIES
MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
)
if(QT_VERSION_MAJOR EQUAL 6)
qt_finalize_executable(ExQRect)
endif()

View File

@ -0,0 +1,7 @@
### 效果:修改 `.svg` 的图片颜色,且不模糊(保持相同大小)
<img src="display.gif" width="100%"/>

Binary file not shown.

After

Width:  |  Height:  |  Size: 113 KiB

37
ExChangeSVGColor/main.cpp Normal file
View File

@ -0,0 +1,37 @@
#include "widget.h"
#include <QApplication>
#include <QMap>
#include <QString>
#include <QVector>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
// QVector<QMap<QString, QString>> vLab;
// vLab.append(QMap<QString, QString>({"labRed", "#DB000F"}));
// vLab.append(QMap<QString, QString>({"labYellow", "#FFCF53"}));
// vLab.append(QMap<QString, QString>({"labGreen", "#12F63B"}));
// vLab.append(QMap<QString, QString>({"labBlue", "#0E70FF"}));
// vLab.append(QMap<QString, QString>({"labPink", "#FB4288"}));
// vLab.append(QMap<QString, QString>({"labBlack", "#323232"}));
// vLab.append(QMap<QString, QString>({"labWhite", "#FBFBFB"}));
// vLab.append(QMap<QString, QString>({"labPick", "#FFDB34"})); //
// qDebug() << vLab[2].first() << " ";
// QPushButton* btn = new QPushButton("btn123");
// QIcon icon("D:/projects/PicShot/src/resourcesicons/normal/gif.svg");
// btn->setIcon(icon);
// btn->show();
return a.exec();
}

View File

@ -0,0 +1 @@
<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg class="icon" width="32px" height="32.00px" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg"><path fill="#515151" d="M796.444444 910.222222H484.977778V540.444444H853.333333V227.555556a56.888889 56.888889 0 0 0-56.888889-56.888889H484.977778v369.777777H113.777778V853.333333a56.888889 56.888889 0 0 0 56.888889 56.888889h625.777777z m56.888889-369.777778h56.888889V853.333333a113.777778 113.777778 0 0 1-113.777778 113.777778H170.666667a113.777778 113.777778 0 0 1-113.777778-113.777778V227.555556a113.777778 113.777778 0 0 1 113.777778-113.777778h625.777777a113.777778 113.777778 0 0 1 113.777778 113.777778v570.311111z" /></svg>

After

Width:  |  Height:  |  Size: 792 B

View File

@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/">
<file>mosaic.svg</file>
</qresource>
</RCC>

143
ExChangeSVGColor/widget.cpp Normal file
View File

@ -0,0 +1,143 @@
#include "widget.h"
#include <QPainter>
#include <QDebug>
// test
#include <QFile>
#include <QByteArray>
#include <QPainter>
#include <QPixmap>
#include <QIcon>
#include <QSvgRenderer>
#include <QDomElement>
#include <QDomElement>
#include <QIcon>
#include <QSize>
QIcon ChangeSVGColor(QString path, QString color, QSize size)
{
QFile file(path);
file.open(QIODevice::ReadOnly);
QByteArray baData = file.readAll();
QDomDocument doc;
doc.setContent(baData);
QDomElement elem = doc.documentElement(); // const 和 值传递
SetAttrRecur(elem, "path", "fill", color);
QSvgRenderer svgRenderer(doc.toByteArray());
// create pixmap target (could be a QImage)
// QPixmap pix(svgRenderer.defaultSize());
QPixmap pix(svgRenderer.defaultSize().scaled(size.width(), size.height(), Qt::KeepAspectRatio)); // fix: 修改 svg 颜色后略有模糊
pix.fill(Qt::transparent);
// create painter to act over pixmap
QPainter pixPainter(&pix);
// use renderer to render over painter which paints on pixmap
svgRenderer.render(&pixPainter);
QIcon myicon(pix);
return myicon;
}
void SetAttrRecur(QDomElement &elem, QString strtagname, QString strattr, QString strattrval)
{
// if it has the tagname then overwritte desired attribute
if (elem.tagName().compare(strtagname) == 0)
{
elem.setAttribute(strattr, strattrval);
}
// loop all children
for (int i = 0; i < elem.childNodes().count(); i++)
{
if (!elem.childNodes().at(i).isElement())
{
continue;
}
QDomElement t = elem.childNodes().at(i).toElement();
SetAttrRecur(t, strtagname, strattr, strattrval);
}
}
Widget::Widget(QWidget *parent)
: QWidget(parent)
, m_btn()
{
setWindowTitle("change .svg color");
resize(1600, 1000);
const int width = 80;
QIcon icon(ChangeSVGColor(":/mosaic.svg", QColor(Qt::green).name(), QSize(width, width)));
m_btn = new QToolButton(this);
m_btn->setCheckable(true);
m_btn->setToolButtonStyle(Qt::ToolButtonIconOnly);
m_btn->setAutoRaise(true); // 自动浮动模式
m_btn->setIcon(icon);
m_btn->setIconSize(QSize(width, width));
// m_btn->resize(200, 200);
m_btn->move(0, 0);
QIcon icon2(":/mosaic.svg");
m_btn2 = new QToolButton(this);
m_btn2->setCheckable(true);
m_btn2->setToolButtonStyle(Qt::ToolButtonIconOnly);
m_btn2->setAutoRaise(true); // 自动浮动模式
m_btn2->setIcon(icon);
m_btn2->setIconSize(QSize(width, width));
// m_btn->resize(200, 200);
m_btn2->move(100, 100);
m_btn->show();
m_btn2->show();
connect(m_btn, &QToolButton::clicked, this, [&](bool checked){
QString path(":/mosaic.svg");
QIcon ico;
if (checked)
ico.addFile(path);
else
ico = ChangeSVGColor(":/mosaic.svg", QColor(Qt::green).name(), QSize(width, width));
m_btn->setIcon(ico);
});
}
Widget::~Widget()
{
}
void Widget::paintEvent(QPaintEvent *event)
{
QPainter pa(this);
pa.setBrush(Qt::NoBrush);
pa.setPen(QPen(Qt::black, 1));
pa.setPen(Qt::blue);
QRect rt(200, 200, 200, 200);
pa.drawRect(rt);
QPoint pos(500, 200);
int off = 30;
pa.drawText(pos + QPoint(0, off * 0), QString("rt.x():%1 rt.y():%2 rt.width():%3 rt.height():%4")
.arg(rt.x()).arg(rt.y()).arg(rt.width()).arg(rt.height()));
pa.drawText(pos + QPoint(0, off * 1), QString("rt.left():%1 rt.top():%2 rt.right():%3 rt.bottom():%4")
.arg(rt.left()).arg(rt.top()).arg(rt.right()).arg(rt.bottom()));
pa.drawText(pos + QPoint(0, off * 2), QString("rt.topLeft().x():%1 rt.topLeft().y():%2 rt.bottomRight().x():%3 rt.bottomRight().y():%4")
.arg(rt.topLeft().x()).arg(rt.topLeft().y()).arg(rt.bottomRight().x()).arg(rt.bottomRight().y()));
update();
}

29
ExChangeSVGColor/widget.h Normal file
View File

@ -0,0 +1,29 @@
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPushButton>
#include <QToolButton>
class QDomElement;
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
// QWidget interface
protected:
virtual void paintEvent(QPaintEvent *event) override;
private:
QToolButton* m_btn;
QToolButton* m_btn2;
};
void SetAttrRecur(QDomElement &elem, QString strtagname, QString strattr, QString strattrval);
#endif // WIDGET_H