src/colors.cpp

Tue, 27 Jul 2021 16:29:00 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 27 Jul 2021 16:29:00 +0300
changeset 118
8e1c9f18ae15
parent 94
164f53fb5921
child 132
488d0ba6070b
permissions
-rw-r--r--

Add vertex rendering

26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 /*
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
3 * Copyright (C) 2013 - 2020 Teemu Piippo
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4 *
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8 * (at your option) any later version.
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 *
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 * GNU General Public License for more details.
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 *
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17 */
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 #include "colors.h"
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
21 const ldraw::ColorTable::ColorDefinition ldraw::ColorTable::unknownColor{{}, {}, "Unknown", "???"};
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
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
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 definitions = {};
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
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
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 this->clear();
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31 if (device.isReadable())
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33 QTextStream stream{&device};
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 QString line;
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 while (stream.readLineInto(&line))
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37 this->loadColorFromString(line);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39 return Success;
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41 else
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43 errors << "could not read colors";
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 return Failure;
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
45 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
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
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
50 auto it = this->definitions.find(color.index);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51 if (it != this->definitions.end())
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 {
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
53 return it->second;
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
55 else
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
56 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
57 return unknownColor;
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
59 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
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
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
62 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
63 const QRegExp pattern{
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
64 R"(^\s*0 \!COLOUR\s+([^\s]+)\s+)"_q +
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
65 R"(CODE\s+(\d+)\s+)"_q +
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
66 R"(VALUE\s+(\#[0-9a-fA-F]{3,6})\s+)"_q +
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
67 R"(EDGE\s+(\#[0-9a-fA-F]{3,6}))"_q +
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
68 R"((?:\s+ALPHA\s+(\d+))?)"_q
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
69 };
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70 if (pattern.indexIn(string) != -1)
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
71 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72 const int code = pattern.cap(2).toInt();
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73 ColorDefinition& definition = definitions[code];
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
74 definition = {}; // in case there's an existing definition
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
75 definition.name = pattern.cap(1);
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
76 definition.displayName = definition.name;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
77 definition.displayName.replace("_", " ");
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
78 definition.faceColor = pattern.cap(3);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
79 definition.edgeColor = pattern.cap(4);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
80 if (not pattern.cap(5).isEmpty())
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
81 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
82 const int alpha = pattern.cap(5).toInt();
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
83 definition.faceColor.setAlpha(alpha);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
84 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
85 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
86 }
43
08dc62e03a6d made edges white in dark backgrounds
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
87
08dc62e03a6d made edges white in dark backgrounds
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
88 /*
08dc62e03a6d made edges white in dark backgrounds
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
89 * Calculates the luma-value for the given color.
08dc62e03a6d made edges white in dark backgrounds
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
90 * 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
91 */
08dc62e03a6d made edges white in dark backgrounds
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
92 double luma(const QColor& color)
08dc62e03a6d made edges white in dark backgrounds
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
93 {
08dc62e03a6d made edges white in dark backgrounds
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
94 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
95 }
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
96
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
97 ldraw::Color ldraw::directColor(const QColor& color)
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
98 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
99 return ldraw::Color{0x2000000 | (color.red() << 16) | (color.green() << 8) | color.blue()};
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
100 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
101
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
102 bool ldraw::isDirectColor(ldraw::Color color)
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
103 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
104 return color.index >= 0x2000000;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
105 }
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 QColor ldraw::directColorFace(ldraw::Color color)
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
108 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
109 if (isDirectColor(color))
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
110 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
111 return {(color.index >> 16) & 0xff, (color.index >> 8) & 0xff, color.index & 0xff};
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
112 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
113 else
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
114 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
115 return {};
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 }
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 QColor ldraw::colorFace(ldraw::Color color, const ldraw::ColorTable& colorTable)
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
120 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
121 if (isDirectColor(color))
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
122 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
123 return directColorFace(color);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
124 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
125 else
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
126 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
127 return colorTable[color].faceColor;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
128 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
129 }

mercurial