-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvghandler.cpp
88 lines (78 loc) · 2.43 KB
/
svghandler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "svghandler.h"
#include <QApplication>
SVGHandler::SVGHandler(QObject *parent) : QObject(parent)
{
}
const QPixmap SVGHandler::loadSvg(const QString &fileName)
{
int size = 24;
const auto ratio = qApp->devicePixelRatio();
if ( 2 == ratio) {
size = 48;
} else if (3 == ratio) {
size = 96;
}
QPixmap pixmap(size, size);
QSvgRenderer renderer(fileName);
pixmap.fill(Qt::transparent);
QPainter painter;
painter.begin(&pixmap);
renderer.render(&painter);
painter.end();
pixmap.setDevicePixelRatio(ratio);
return pixmap;
}
const QPixmap SVGHandler::loadSvgColor(const QString &path, const QString color, int size)
{
int origSize = size;
const auto ratio = qApp->devicePixelRatio();
if ( 2 == ratio) {
size += origSize;
} else if (3 == ratio) {
size += origSize;
}
QPixmap pixmap(size, size);
QSvgRenderer renderer(path);
pixmap.fill(Qt::transparent);
QPainter painter;
painter.begin(&pixmap);
renderer.render(&painter);
painter.end();
pixmap.setDevicePixelRatio(ratio);
return drawSymbolicColoredPixmap(pixmap, color);
}
QPixmap SVGHandler::drawSymbolicColoredPixmap(const QPixmap &source, QString cgColor)
{
QImage img = source.toImage();
for (int x = 0; x < img.width(); x++) {
for (int y = 0; y < img.height(); y++) {
auto color = img.pixelColor(x, y);
if (color.alpha() > 0) {
if ( "white" == cgColor) {
color.setRed(255);
color.setGreen(255);
color.setBlue(255);
img.setPixelColor(x, y, color);
} else if( "black" == cgColor) {
color.setRed(0);
color.setGreen(0);
color.setBlue(0);
img.setPixelColor(x, y, color);
} else if ("gray"== cgColor) {
color.setRed(152);
color.setGreen(163);
color.setBlue(164);
img.setPixelColor(x, y, color);
} else if ("blue" == cgColor){
color.setRed(61);
color.setGreen(107);
color.setBlue(229);
img.setPixelColor(x, y, color);
} else {
return source;
}
}
}
}
return QPixmap::fromImage(img);
}