src/colors.cpp

Wed, 25 May 2022 17:56:30 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 25 May 2022 17:56:30 +0300
changeset 196
6bcb284679d4
parent 178
a23024fc98e0
child 205
1a4342d80de7
permissions
-rw-r--r--

delete unneeded things

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
139
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
21 const ldraw::ColorDefinition ldraw::ColorTable::unknownColor{{}, {}, "Unknown", "???"};
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22
139
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
23 /**
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
24 * @brief Clears the color table
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
25 */
35
98906a94732f renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
26 void ldraw::ColorTable::clear()
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27 {
139
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
28 this->definitions = {};
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
139
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
31 /**
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
32 * @brief Loads colors from LDConfig.ldr
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
33 * @param device Opened LDConfig.ldr I/O device
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
34 * @param errors Where to write any errors into
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
35 * @returns whether or not it succeeded.
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
36 */
35
98906a94732f renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
37 Result ldraw::ColorTable::load(QIODevice& device, QTextStream& errors)
26
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 this->clear();
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 if (device.isReadable())
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42 QTextStream stream{&device};
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43 QString line;
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 while (stream.readLineInto(&line))
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 this->loadColorFromString(line);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
48 return Success;
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 else
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 errors << "could not read colors";
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
53 return Failure;
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 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
56
139
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
57 /**
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
58 * @brief Gets color information by color index.
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
59 * @param color
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
60 * @returns color table information
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
61 */
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
62 const ldraw::ColorDefinition& ldraw::ColorTable::operator[](Color color) const
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
63 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
64 auto it = this->definitions.find(color.index);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
65 if (it != this->definitions.end())
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
66 {
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
67 return it->second;
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
68 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
69 else
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
71 return unknownColor;
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
74
139
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
75 /**
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
76 * @brief Gets the amount of elements in the color table
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
77 * @returns int
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
78 */
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
79 int ldraw::ColorTable::size() const
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
80 {
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
81 return this->definitions.size();
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
82 }
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
83
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
84 /**
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
85 * @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
86 * @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
87 */
35
98906a94732f renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
88 void ldraw::ColorTable::loadColorFromString(const QString& string)
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
89 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
90 const QRegExp pattern{
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
91 R"(^\s*0 \!COLOUR\s+([^\s]+)\s+)"_q +
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
92 R"(CODE\s+(\d+)\s+)"_q +
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
93 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
94 R"(EDGE\s+(\#[0-9a-fA-F]{3,6}))"_q +
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
95 R"((?:\s+ALPHA\s+(\d+))?)"_q
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
96 };
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
97 if (pattern.indexIn(string) != -1)
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
98 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
99 const int code = pattern.cap(2).toInt();
139
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
100 ColorDefinition& definition = this->definitions[code];
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
101 definition = {}; // in case there's an existing definition
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
102 definition.name = pattern.cap(1);
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
103 definition.displayName = definition.name;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
104 definition.displayName.replace("_", " ");
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
105 definition.faceColor = pattern.cap(3);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
106 definition.edgeColor = pattern.cap(4);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
107 if (not pattern.cap(5).isEmpty())
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
108 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
109 const int alpha = pattern.cap(5).toInt();
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
110 definition.faceColor.setAlpha(alpha);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
111 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
112 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
113 }
43
08dc62e03a6d made edges white in dark backgrounds
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
114
139
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
115 /**
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
116 * @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
117 * @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
118 * @param color
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
119 * @returns luma value [0, 1]
43
08dc62e03a6d made edges white in dark backgrounds
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
120 */
08dc62e03a6d made edges white in dark backgrounds
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
121 double luma(const QColor& color)
08dc62e03a6d made edges white in dark backgrounds
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
122 {
08dc62e03a6d made edges white in dark backgrounds
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
123 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
124 }
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
125
139
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
126 /**
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
127 * @brief Returns a direct color index that codes the specified color value
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
128 * @param color
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
129 * @returns direct color index
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
130 */
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
131 ldraw::Color ldraw::directColor(const QColor& color)
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
132 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
133 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
134 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
135
139
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
136 /**
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
137 * @brief Checks whether or not the specified color index is a direct color
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
138 * @param color Color to check
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
139 * @returns bool
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
140 */
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
141 bool ldraw::isDirectColor(ldraw::Color color)
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
142 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
143 return color.index >= 0x2000000;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
144 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
145
139
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
146 /**
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
147 * @brief Extracts the color value from a direct color index
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
148 * @param color Direct color index
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
149 * @returns color value. Returns a default-constructed QColor in case a non-direct color is given.
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
150 */
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
151 QColor ldraw::directColorFace(ldraw::Color color)
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
152 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
153 if (isDirectColor(color))
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
154 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
155 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
156 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
157 else
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
158 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
159 return {};
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
160 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
161 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
162
139
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
163 /**
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
164 * @brief Gets the face color for the specified color index
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
165 * @param color Color index to get face color for
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
166 * @param colorTable Color table to use for lookup
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
167 * @returns QColor
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
168 */
94
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
169 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
170 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
171 if (isDirectColor(color))
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
172 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
173 return directColorFace(color);
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
174 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
175 else
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
176 {
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
177 return colorTable[color].faceColor;
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
178 }
164f53fb5921 added a color select dialog
Teemu Piippo <teemu@hecknology.net>
parents: 43
diff changeset
179 }
132
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
180
178
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
181 QColor ldraw::colorEdge(ldraw::Color color, const ldraw::ColorTable& colorTable)
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
182 {
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
183 if (isDirectColor(color))
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
184 {
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
185 QColor const faceColor = directColorFace(color);
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
186 return (luma(faceColor) < 0.4) ? Qt::white : Qt::black;
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
187 }
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
188 else
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
189 {
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
190 return colorTable[color].faceColor;
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
191 }
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
192 }
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
193
139
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
194 /**
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
195 * @brief Writes a color index into a @c QDataStream
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
196 * @param stream
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
197 * @param color
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
198 * @returns stream
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
199 */
132
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
200 QDataStream& operator<<(QDataStream& stream, ldraw::Color color)
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
201 {
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
202 return stream << color.index;
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
203 }
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
204
139
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
205 /**
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
206 * @brief Reads a color index from a @c QDataStream
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
207 * @param stream
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
208 * @param color
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
209 * @returns stream
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
210 */
132
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
211 QDataStream& operator>>(QDataStream& stream, ldraw::Color& color)
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
212 {
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
213 return stream >> color.index;
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
214 }
178
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
215
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
216 QString ldraw::colorDisplayName(ldraw::Color color, const ldraw::ColorTable &colorTable)
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
217 {
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
218 if (isDirectColor(color))
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
219 {
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
220 return directColorFace(color).name();
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
221 }
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
222 else
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
223 {
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
224 return colorTable[color].displayName;
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
225 }
a23024fc98e0 fix saving
Teemu Piippo <teemu@hecknology.net>
parents: 139
diff changeset
226 }

mercurial