src/colors.h

Sat, 08 Apr 2023 12:55:11 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Sat, 08 Apr 2023 12:55:11 +0300
changeset 343
4a82990affd5
parent 308
daa8770b9d26
permissions
-rw-r--r--

Fix BFC formatting not working due to being evaluated after comment format

24
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
1 /*
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
2 * LDForge: LDraw parts authoring CAD
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
3 * Copyright (C) 2013 - 2020 Teemu Piippo
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
4 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
5 * This program is free software: you can redistribute it and/or modify
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
6 * it under the terms of the GNU General Public License as published by
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
8 * (at your option) any later version.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
9 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
10 * This program is distributed in the hope that it will be useful,
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
13 * GNU General Public License for more details.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
14 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
15 * You should have received a copy of the GNU General Public License
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
17 */
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
18
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 #pragma once
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
20 #include <QColor>
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: 250
diff changeset
21 #include "src/basics.h"
3
55a55a9ec2c2 Added lots of code
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 Represents an LDraw color index (e.g 16, 24, ...)
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
25 *
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
26 * @details
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
27 * This is essentially just an integer. It is its own structure for the sake
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
28 * of type correctness. Commonly used color values are 16 for the main color
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
29 * and 24 for the edge color.
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
30 *
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
31 * Use @c ldraw::ColorTable to obtain rgb values for color indices.
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
32 *
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
33 * Direct colors are colors whose index is at least 0x2000000. These colors
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
34 * encode the rgb values directly in the index. The color table code also
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
35 * handles these cases.
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
36 *
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
37 * Note that it only makes sense to construct a color index from an integer
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
38 * value if the integer comes from an external source (e.g. user or
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
39 * LDConfig.ldr). Since this structure only contains an integer, it should be
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
40 * passed by value to functions instead of l-value reference.
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
41 */
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
42 struct ColorIndex
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43 {
89
7abaf1d64719 object editing
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
44 qint32 index = 0;
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
45 constexpr auto operator<=>(const ColorIndex&) const = default;
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46 };
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
48 Q_DECLARE_METATYPE(ColorIndex)
308
daa8770b9d26 add comments in colors.h
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
49
daa8770b9d26 add comments in colors.h
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
50 //! \brief LDraw main color (16)
daa8770b9d26 add comments in colors.h
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
51 constexpr ColorIndex MAIN_COLOR{16};
daa8770b9d26 add comments in colors.h
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
52 //! \brief LDraw edge color (24)
daa8770b9d26 add comments in colors.h
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
53 constexpr ColorIndex EDGE_COLOR{24};
139
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
54
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
55 /**
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
56 * @brief Contains the information about a specific color
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
57 */
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
58 struct ColorDefinition
139
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
59 {
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
60 //! @brief The face color. This is the color used for most objects.
139
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
61 QColor faceColor;
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
62 //! @brief The edge color, used for the edge lines of a subfile reference.
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
63 //! Note that edges using a color actually use the face color. LDraw standards
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
64 //! require edges to use color 24, however.
139
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
65 QColor edgeColor;
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
66 //! @brief Name of the color
139
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
67 QString name;
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
68 //! @brief A version of the name that can be shown to the user.
139
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
69 QString displayName;
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
70 };
72098474d362 Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
71
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
72 //extern const ColorDefinition unknownColor;
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
73 using ColorTable = std::map<ColorIndex, ColorDefinition>;
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
74 std::optional<ColorTable> loadColorTable(QIODevice& device, QTextStream& errors);
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
75 qreal luma(const QColor& color);
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
76 ColorIndex directColor(const QColor& color);
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
77 std::optional<QColor> colorFace(ColorIndex color, const ColorTable& colorTable);
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
78 std::optional<QColor> colorEdge(ColorIndex color, const ColorTable& colorTable);
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
79 std::optional<QString> colorDisplayName(ColorIndex color, const ColorTable& colorTable);
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
80 QDataStream& operator<<(QDataStream&, ColorIndex);
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
81 QDataStream& operator>>(QDataStream&, ColorIndex&);
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
82
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
83 //! @brief Calculates the luma-value for the given color.
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
84 //! @details c.f. https://en.wikipedia.org/wiki/Luma_(video)
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
85 template<typename T>
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
86 constexpr T luma(T r, T g, T b)
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
87 {
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: 206
diff changeset
88 return static_cast<T>(0.2126 * r + 0.7152 * g + 0.0722 * b);
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
89 }
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
90
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
91 //! @brief Checks whether or not the specified color index is a direct color
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
92 constexpr bool isDirectColor(ColorIndex color)
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
93 {
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
94 return color.index >= 0x2000000;
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
95 }
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
96
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
97 //! @brief Returns a direct color index that codes the specified color value
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
98 constexpr ColorIndex directColor(int r, int g, int b)
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
99 {
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
100 return ColorIndex{0x2000000 | (r << 16) | (g << 8) | b};
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
101 }
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
102
205
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
103 //! @brief Extracts the r, g and b color channel values from a direct color
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
104 //! index. Returns a default-constructed QColor in case a non-direct color is
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
105 //! given.
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
106 constexpr std::array<int, 3> directColorRgb(ColorIndex color)
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
107 {
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
108 return {
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
109 (color.index >> 16) & 0xff,
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
110 (color.index >> 8) & 0xff,
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
111 color.index & 0xff
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
112 };
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
113 }
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
114
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
115 namespace ldraw
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
116 {
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
117 using Color = ColorIndex;
1a4342d80de7 Refactor colors.cpp/.h
Teemu Piippo <teemu@hecknology.net>
parents: 196
diff changeset
118 }

mercurial