src/colors.cpp

Sat, 08 Apr 2023 12:53:15 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Sat, 08 Apr 2023 12:53:15 +0300
changeset 342
9fd7dcab6d76
parent 264
76a025db4948
permissions
-rw-r--r--

Fix line type 0 format regular expression

259
c27612f0eac0 - Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 250
diff changeset
1 #include <QRegExp>
c27612f0eac0 - Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 250
diff changeset
2 #include <QIODevice>
264
76a025db4948 Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 259
diff changeset
3 #include "src/colors.h"
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
5 const ColorDefinition unknownColor{{}, {}, "Unknown", "???"};
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
7 template<typename... Args>
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
8 static QString replaced(QString text, Args&&... args)
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 {
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
10 text.replace(args...);
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
11 return text;
139
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
12 }
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
13
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
14 /**
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
15 * @brief Parses an LDConfig.ldr line from a string
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
16 * @param string LDConfig.ldr line to parse
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
17 */
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
18 static auto loadColorFromString(const QString& string)
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 {
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
20 std::optional<std::pair<ColorIndex, ColorDefinition>> result;
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
21 static const QRegExp pattern{QStringLiteral(
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
22 R"(^\s*0 \!COLOUR\s+([^\s]+)\s+)"
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
23 R"(CODE\s+(\d+)\s+)"
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
24 R"(VALUE\s+(\#[0-9a-fA-F]{3,6})\s+)"
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
25 R"(EDGE\s+(\#[0-9a-fA-F]{3,6}))"
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
26 R"((?:\s+ALPHA\s+(\d+))?)"
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
27 )};
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28 if (pattern.indexIn(string) != -1)
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 const int code = pattern.cap(2).toInt();
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
31 const QString name = pattern.cap(1);
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
32 ColorDefinition definition = {
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
33 .faceColor = pattern.cap(3),
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
34 .edgeColor = pattern.cap(4),
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
35 .name = name,
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
36 .displayName = replaced(name, "_", " "),
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
37 };
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38 if (not pattern.cap(5).isEmpty())
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 const int alpha = pattern.cap(5).toInt();
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41 definition.faceColor.setAlpha(alpha);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42 }
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
43 result = std::make_pair(ColorIndex{code}, definition);
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 }
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
45 return result;
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
46 }
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
47
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
48 /**
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
49 * @brief Loads colors from LDConfig.ldr
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
50 */
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
51 std::optional<ColorTable> loadColorTable(QIODevice &device, QTextStream &errors)
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
52 {
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
53 std::optional<ColorTable> result;
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
54 if (device.isReadable())
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
55 {
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
56 result.emplace();
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
57 QTextStream stream{&device};
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
58 QString line;
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
59 while (stream.readLineInto(&line))
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
60 {
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
61 const auto pair = loadColorFromString(line);
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
62 if (pair.has_value()) {
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
63 (*result)[pair->first] = pair->second;
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
64 }
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
65 }
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
66 }
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
67 else
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
68 {
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
69 errors << "could not read colors";
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
70 }
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
71 return result;
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72 }
43
08dc62e03a6d made edges white in dark backgrounds
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
73
139
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
74 /**
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
75 * @brief Calculates the luma-value for the given color.
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
76 * @details c.f. https://en.wikipedia.org/wiki/Luma_(video)
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
77 * @param color
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
78 * @returns luma value [0, 1]
43
08dc62e03a6d made edges white in dark backgrounds
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
79 */
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
80 qreal luma(const QColor& color)
43
08dc62e03a6d made edges white in dark backgrounds
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
81 {
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
82 return luma(color.redF(), color.greenF(), color.blueF());
43
08dc62e03a6d made edges white in dark backgrounds
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
83 }
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
84
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
85 //! @brief Returns a direct color index that codes the specified color value
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
86 ColorIndex directColor(const QColor& color)
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
87 {
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
88 return directColor(color.red(), color.green(), color.blue());
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
89 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
90
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
91 //! @brief Returns a face color for @param color, taking direct colors into account
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
92 std::optional<QColor> colorFace(ColorIndex color, const ColorTable& colorTable)
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
93 {
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
94 std::optional<QColor> result;
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
95 if (isDirectColor(color)) {
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
96 const std::array<int, 3> rgb = directColorRgb(color);
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
97 result = QColor{rgb[0], rgb[1], rgb[2]};
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
98 }
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
99 else {
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
100 const ColorDefinition* def = findInMap(colorTable, color);
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
101 if (def != nullptr) {
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
102 result = def->faceColor;
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
103 }
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
104 }
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
105 return result;
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
106 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
107
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
108 //! @brief Returns an edge color for @param color, taking direct colors into account
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
109 std::optional<QColor> colorEdge(ColorIndex color, const ColorTable& colorTable)
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
110 {
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
111 if (isDirectColor(color)) {
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
112 const std::array<int, 3> rgb = directColorRgb(color);
250
2837b549e616 I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
113 return (luma(rgb[0], rgb[1], rgb[2]) < 102) ? Qt::white : Qt::black;
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
114 }
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
115 else {
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
116 return colorFace(color, colorTable);
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
117 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
118 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
119
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
120 QDataStream& operator<<(QDataStream& stream, ColorIndex color)
132
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
121 {
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
122 return stream << color.index;
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
123 }
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
124
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
125 QDataStream& operator>>(QDataStream& stream, ColorIndex& color)
132
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
126 {
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
127 return stream >> color.index;
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
128 }
178
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
129
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
130 std::optional<QString> colorDisplayName(ColorIndex color, const ColorTable &colorTable)
178
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
131 {
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
132 std::optional<QString> result;
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
133 if (isDirectColor(color)) {
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
134 result = colorFace(color, colorTable).value_or(QColor{}).name();
178
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
135 }
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
136 else {
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
137 const ColorDefinition* def = findInMap(colorTable, color);
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
138 if (def != nullptr) {
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
139 result = def->displayName;
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
140 }
178
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
141 }
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
142 return result;
178
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
143 }

mercurial