Wed, 25 Mar 2020 16:07:20 +0200
added PolygonObject and refactored away a lot of boilerplate
26 | 1 | /* |
2 | * LDForge: LDraw parts authoring CAD | |
3 | * Copyright (C) 2013 - 2020 Teemu Piippo | |
4 | * | |
5 | * This program is free software: you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation, either version 3 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
19 | #include "colors.h" | |
20 | ||
35
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
21 | const ldraw::ColorTable::ColorDefinition ldraw::ColorTable::unknownColor{{}, {}, "Unknown"}; |
26 | 22 | |
35
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
23 | void ldraw::ColorTable::clear() |
26 | 24 | { |
25 | definitions = {}; | |
26 | } | |
27 | ||
35
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
28 | Result ldraw::ColorTable::load(QIODevice& device, QTextStream& errors) |
26 | 29 | { |
30 | this->clear(); | |
31 | if (device.isReadable()) | |
32 | { | |
33 | QTextStream stream{&device}; | |
34 | QString line; | |
35 | while (stream.readLineInto(&line)) | |
36 | { | |
37 | this->loadColorFromString(line); | |
38 | } | |
39 | return Success; | |
40 | } | |
41 | else | |
42 | { | |
43 | errors << "could not read colors"; | |
44 | return Failure; | |
45 | } | |
46 | } | |
47 | ||
35
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
48 | const ldraw::ColorTable::ColorDefinition& ldraw::ColorTable::operator[](Color color) const |
26 | 49 | { |
50 | auto it = this->definitions.find(color.index); | |
51 | if (it != this->definitions.end()) | |
52 | { | |
53 | return *it; | |
54 | } | |
55 | else | |
56 | { | |
57 | return unknownColor; | |
58 | } | |
59 | } | |
60 | ||
35
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
61 | void ldraw::ColorTable::loadColorFromString(const QString& string) |
26 | 62 | { |
63 | const QRegExp pattern{ | |
64 | R"(^\s*0 \!COLOUR\s+([^\s]+)\s+)"_q + | |
65 | R"(CODE\s+(\d+)\s+)"_q + | |
66 | R"(VALUE\s+(\#[0-9a-fA-F]{3,6})\s+)"_q + | |
67 | R"(EDGE\s+(\#[0-9a-fA-F]{3,6}))"_q + | |
68 | R"((?:\s+ALPHA\s+(\d+))?)"_q | |
69 | }; | |
70 | if (pattern.indexIn(string) != -1) | |
71 | { | |
72 | const int code = pattern.cap(2).toInt(); | |
73 | ColorDefinition& definition = definitions[code]; | |
74 | definition = {}; // in case there's an existing definition | |
75 | definition.name = pattern.cap(1); | |
76 | definition.faceColor = pattern.cap(3); | |
77 | definition.edgeColor = pattern.cap(4); | |
78 | if (not pattern.cap(5).isEmpty()) | |
79 | { | |
80 | const int alpha = pattern.cap(5).toInt(); | |
81 | definition.faceColor.setAlpha(alpha); | |
82 | } | |
83 | } | |
84 | } | |
43
08dc62e03a6d
made edges white in dark backgrounds
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
85 | |
08dc62e03a6d
made edges white in dark backgrounds
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
86 | /* |
08dc62e03a6d
made edges white in dark backgrounds
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
87 | * Calculates the luma-value for the given color. |
08dc62e03a6d
made edges white in dark backgrounds
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
88 | * c.f. https://en.wikipedia.org/wiki/Luma_(video) |
08dc62e03a6d
made edges white in dark backgrounds
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
89 | */ |
08dc62e03a6d
made edges white in dark backgrounds
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
90 | double luma(const QColor& color) |
08dc62e03a6d
made edges white in dark backgrounds
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
91 | { |
08dc62e03a6d
made edges white in dark backgrounds
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
92 | return 0.2126 * color.redF() + 0.7152 * color.greenF() + 0.0722 * color.blueF(); |
08dc62e03a6d
made edges white in dark backgrounds
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
93 | } |