src/colors.cpp

Mon, 20 Jun 2022 17:27:30 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Mon, 20 Jun 2022 17:27:30 +0300
changeset 235
7ef03c2b46ab
parent 205
1a4342d80de7
child 250
2837b549e616
permissions
-rw-r--r--

Add a basic message log

26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 #include "colors.h"
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
2
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
3 const ColorDefinition unknownColor{{}, {}, "Unknown", "???"};
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 template<typename... Args>
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
6 static QString replaced(QString text, Args&&... args)
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7 {
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
8 text.replace(args...);
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
9 return text;
139
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
10 }
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
11
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 * @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
14 * @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
15 */
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
16 static auto loadColorFromString(const QString& string)
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17 {
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
18 std::optional<std::pair<ColorIndex, ColorDefinition>> result;
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
19 static const QRegExp pattern{QStringLiteral(
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
20 R"(^\s*0 \!COLOUR\s+([^\s]+)\s+)"
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
21 R"(CODE\s+(\d+)\s+)"
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
22 R"(VALUE\s+(\#[0-9a-fA-F]{3,6})\s+)"
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
23 R"(EDGE\s+(\#[0-9a-fA-F]{3,6}))"
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
24 R"((?:\s+ALPHA\s+(\d+))?)"
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
25 )};
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26 if (pattern.indexIn(string) != -1)
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28 const int code = pattern.cap(2).toInt();
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
29 const QString name = pattern.cap(1);
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
30 ColorDefinition definition = {
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
31 .faceColor = pattern.cap(3),
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
32 .edgeColor = pattern.cap(4),
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
33 .name = name,
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
34 .displayName = replaced(name, "_", " "),
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
35 };
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36 if (not pattern.cap(5).isEmpty())
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38 const int alpha = pattern.cap(5).toInt();
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39 definition.faceColor.setAlpha(alpha);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 }
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
41 result = std::make_pair(ColorIndex{code}, definition);
26
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 return result;
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
44 }
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
45
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 * @brief Loads colors from LDConfig.ldr
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 std::optional<ColorTable> loadColorTable(QIODevice &device, QTextStream &errors)
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> result;
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
52 if (device.isReadable())
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
53 {
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
54 result.emplace();
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
55 QTextStream stream{&device};
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
56 QString line;
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
57 while (stream.readLineInto(&line))
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
58 {
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
59 const auto pair = loadColorFromString(line);
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
60 if (pair.has_value()) {
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
61 (*result)[pair->first] = pair->second;
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
62 }
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
63 }
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 else
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 errors << "could not read colors";
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 return result;
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70 }
43
08dc62e03a6d made edges white in dark backgrounds
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
71
139
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
72 /**
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
73 * @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
74 * @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
75 * @param color
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
76 * @returns luma value [0, 1]
43
08dc62e03a6d made edges white in dark backgrounds
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
77 */
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
78 qreal luma(const QColor& color)
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 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
81 }
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
82
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
83 //! @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
84 ColorIndex directColor(const QColor& color)
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
85 {
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
86 return directColor(color.red(), color.green(), color.blue());
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
87 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
88
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
89 //! @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
90 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
91 {
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
92 std::optional<QColor> result;
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
93 if (isDirectColor(color)) {
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
94 const std::array<int, 3> rgb = directColorRgb(color);
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
95 result = QColor{rgb[0], rgb[1], rgb[2]};
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
96 }
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
97 else {
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
98 const ColorDefinition* def = findInMap(colorTable, color);
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
99 if (def != nullptr) {
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
100 result = def->faceColor;
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
101 }
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
102 }
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
103 return result;
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
104 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
105
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
106 //! @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
107 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
108 {
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
109 if (isDirectColor(color)) {
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
110 const std::array<int, 3> rgb = directColorRgb(color);
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
111 return (luma(rgb[0], rgb[1], rgb[2]) < 0.4) ? Qt::white : Qt::black;
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
112 }
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
113 else {
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
114 return colorFace(color, colorTable);
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
115 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
116 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
117
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
118 QDataStream& operator<<(QDataStream& stream, ColorIndex color)
132
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
119 {
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
120 return stream << color.index;
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
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
123 QDataStream& operator>>(QDataStream& stream, ColorIndex& color)
132
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
124 {
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
125 return stream >> color.index;
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
126 }
178
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
127
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
128 std::optional<QString> colorDisplayName(ColorIndex color, const ColorTable &colorTable)
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> result;
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
131 if (isDirectColor(color)) {
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
132 result = colorFace(color, colorTable).value_or(QColor{}).name();
178
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
133 }
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
134 else {
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
135 const ColorDefinition* def = findInMap(colorTable, color);
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
136 if (def != nullptr) {
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
137 result = def->displayName;
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
138 }
178
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
139 }
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 178
diff changeset
140 return result;
178
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
141 }

mercurial