Sun, 03 Jul 2022 13:44:11 +0300
Extract the triangulation and triangle merging code into a new source file and clean it up somewhat
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 | 4 | |
205 | 5 | const ColorDefinition unknownColor{{}, {}, "Unknown", "???"}; |
26 | 6 | |
205 | 7 | template<typename... Args> |
8 | static QString replaced(QString text, Args&&... args) | |
26 | 9 | { |
205 | 10 | text.replace(args...); |
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 | 18 | static auto loadColorFromString(const QString& string) |
26 | 19 | { |
205 | 20 | std::optional<std::pair<ColorIndex, ColorDefinition>> result; |
21 | static const QRegExp pattern{QStringLiteral( | |
22 | R"(^\s*0 \!COLOUR\s+([^\s]+)\s+)" | |
23 | R"(CODE\s+(\d+)\s+)" | |
24 | R"(VALUE\s+(\#[0-9a-fA-F]{3,6})\s+)" | |
25 | R"(EDGE\s+(\#[0-9a-fA-F]{3,6}))" | |
26 | R"((?:\s+ALPHA\s+(\d+))?)" | |
27 | )}; | |
26 | 28 | if (pattern.indexIn(string) != -1) |
29 | { | |
30 | const int code = pattern.cap(2).toInt(); | |
205 | 31 | const QString name = pattern.cap(1); |
32 | ColorDefinition definition = { | |
33 | .faceColor = pattern.cap(3), | |
34 | .edgeColor = pattern.cap(4), | |
35 | .name = name, | |
36 | .displayName = replaced(name, "_", " "), | |
37 | }; | |
26 | 38 | if (not pattern.cap(5).isEmpty()) |
39 | { | |
40 | const int alpha = pattern.cap(5).toInt(); | |
41 | definition.faceColor.setAlpha(alpha); | |
42 | } | |
205 | 43 | result = std::make_pair(ColorIndex{code}, definition); |
26 | 44 | } |
205 | 45 | return result; |
46 | } | |
47 | ||
48 | /** | |
49 | * @brief Loads colors from LDConfig.ldr | |
50 | */ | |
51 | std::optional<ColorTable> loadColorTable(QIODevice &device, QTextStream &errors) | |
52 | { | |
53 | std::optional<ColorTable> result; | |
54 | if (device.isReadable()) | |
55 | { | |
56 | result.emplace(); | |
57 | QTextStream stream{&device}; | |
58 | QString line; | |
59 | while (stream.readLineInto(&line)) | |
60 | { | |
61 | const auto pair = loadColorFromString(line); | |
62 | if (pair.has_value()) { | |
63 | (*result)[pair->first] = pair->second; | |
64 | } | |
65 | } | |
66 | } | |
67 | else | |
68 | { | |
69 | errors << "could not read colors"; | |
70 | } | |
71 | return result; | |
26 | 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 | 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 | 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 | 85 | //! @brief Returns a direct color index that codes the specified color value |
86 | ColorIndex directColor(const QColor& color) | |
94
164f53fb5921
added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents:
43
diff
changeset
|
87 | { |
205 | 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 | 91 | //! @brief Returns a face color for @param color, taking direct colors into account |
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 | 94 | std::optional<QColor> result; |
95 | if (isDirectColor(color)) { | |
96 | const std::array<int, 3> rgb = directColorRgb(color); | |
97 | result = QColor{rgb[0], rgb[1], rgb[2]}; | |
98 | } | |
99 | else { | |
100 | const ColorDefinition* def = findInMap(colorTable, color); | |
101 | if (def != nullptr) { | |
102 | result = def->faceColor; | |
103 | } | |
104 | } | |
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 | 108 | //! @brief Returns an edge color for @param color, taking direct colors into account |
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 | 111 | if (isDirectColor(color)) { |
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 | 115 | else { |
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 | 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 | 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 | 129 | |
205 | 130 | std::optional<QString> colorDisplayName(ColorIndex color, const ColorTable &colorTable) |
178 | 131 | { |
205 | 132 | std::optional<QString> result; |
133 | if (isDirectColor(color)) { | |
134 | result = colorFace(color, colorTable).value_or(QColor{}).name(); | |
178 | 135 | } |
205 | 136 | else { |
137 | const ColorDefinition* def = findInMap(colorTable, color); | |
138 | if (def != nullptr) { | |
139 | result = def->displayName; | |
140 | } | |
178 | 141 | } |
205 | 142 | return result; |
178 | 143 | } |