-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolorpalette.cpp
More file actions
84 lines (69 loc) · 2.4 KB
/
colorpalette.cpp
File metadata and controls
84 lines (69 loc) · 2.4 KB
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
#include "colorpalette.h"
#include <QRegularExpression>
#include <QDebug>
ColorPalette readGPL(QIODevice *file)
{
if (!file->isOpen())
file->open(QIODevice::ReadOnly);
ColorPalette result;
QString data = QString::fromUtf8(file->readAll());
auto lines = data.splitRef("\n");
auto lineIter = lines.begin();
if (!lineIter->startsWith("GIMP Palette"))
return {};
// qDebug() << *lineIter;
QRegularExpression paletteTagRegExp("^([^:]+):\\s*(.*)");
while (lineIter != lines.end() && !lineIter->startsWith("#"))
{
auto match = paletteTagRegExp.match(*lineIter);
if (match.hasMatch())
{
if (match.capturedRef(1) == QStringLiteral("Name"))
result.name = match.captured(2);
else if (match.capturedRef(1) == QStringLiteral("Columns"))
result.columns = match.capturedRef(2).toInt();
}
lineIter++;
}
// qDebug() << "Name" << result.name;
// qDebug() << "Columns" << result.columns;
while (lineIter != lines.end() && lineIter->startsWith("#"))
lineIter++;
if (lineIter == lines.end())
return {};
// qDebug() << "Skipped" << (lineIter - lines.begin());
QRegularExpression colorEntryRegExp("^\\s*(\\d+)\\s+(\\d+)\\s+(\\d+)\\s*(.*)");
colorEntryRegExp.optimize();
while (lineIter != lines.end())
{
if (!lineIter->startsWith("#"))
{
auto match = colorEntryRegExp.match(*lineIter);
if (match.hasMatch())
{
// qDebug() << match;
result.values.emplace_back(PaletteEntry{match.captured(4), QColor(match.capturedRef(1).toInt(), match.capturedRef(2).toInt(), match.capturedRef(3).toInt())});
}
}
lineIter++;
}
// qDebug() << lines.size();
// qDebug() << result.size();
return result;
}
QByteArray ColorPalette::toGPL()
{
QByteArray result;
result.append(QStringLiteral("GIMP Palette\n"));
if (!name.isEmpty())
result.append(QStringLiteral("Name: %1\n").arg(name));
if (columns != 0)
result.append(QStringLiteral("Columns: %1\n").arg(columns));
result.append("#\n");
for (const auto &c: values)
{
QString entry = QStringLiteral("%1 %2 %3 %4\n").arg(c.color.red(), 3).arg(c.color.green(), 3).arg(c.color.blue(), 3).arg(c.name);
result.append(entry);
}
return result;
}