Sat, 08 Apr 2023 12:55:11 +0300
Fix BFC formatting not working due to being evaluated after comment format
24 | 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 | ||
3 | 19 | #pragma once |
26 | 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 | 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 | 42 | struct ColorIndex |
3 | 43 | { |
89 | 44 | qint32 index = 0; |
205 | 45 | constexpr auto operator<=>(const ColorIndex&) const = default; |
3 | 46 | }; |
47 | ||
205 | 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 | 58 | struct ColorDefinition |
139
72098474d362
Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents:
132
diff
changeset
|
59 | { |
205 | 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 | 62 | //! @brief The edge color, used for the edge lines of a subfile reference. |
63 | //! Note that edges using a color actually use the face color. LDraw standards | |
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 | 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 | 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 | 72 | //extern const ColorDefinition unknownColor; |
73 | using ColorTable = std::map<ColorIndex, ColorDefinition>; | |
74 | std::optional<ColorTable> loadColorTable(QIODevice& device, QTextStream& errors); | |
75 | qreal luma(const QColor& color); | |
76 | ColorIndex directColor(const QColor& color); | |
77 | std::optional<QColor> colorFace(ColorIndex color, const ColorTable& colorTable); | |
78 | std::optional<QColor> colorEdge(ColorIndex color, const ColorTable& colorTable); | |
79 | std::optional<QString> colorDisplayName(ColorIndex color, const ColorTable& colorTable); | |
80 | QDataStream& operator<<(QDataStream&, ColorIndex); | |
81 | QDataStream& operator>>(QDataStream&, ColorIndex&); | |
82 | ||
83 | //! @brief Calculates the luma-value for the given color. | |
84 | //! @details c.f. https://en.wikipedia.org/wiki/Luma_(video) | |
85 | template<typename T> | |
86 | constexpr T luma(T r, T g, T b) | |
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 | 89 | } |
90 | ||
91 | //! @brief Checks whether or not the specified color index is a direct color | |
92 | constexpr bool isDirectColor(ColorIndex color) | |
26 | 93 | { |
205 | 94 | return color.index >= 0x2000000; |
95 | } | |
96 | ||
97 | //! @brief Returns a direct color index that codes the specified color value | |
98 | constexpr ColorIndex directColor(int r, int g, int b) | |
99 | { | |
100 | return ColorIndex{0x2000000 | (r << 16) | (g << 8) | b}; | |
101 | } | |
26 | 102 | |
205 | 103 | //! @brief Extracts the r, g and b color channel values from a direct color |
104 | //! index. Returns a default-constructed QColor in case a non-direct color is | |
105 | //! given. | |
106 | constexpr std::array<int, 3> directColorRgb(ColorIndex color) | |
107 | { | |
108 | return { | |
109 | (color.index >> 16) & 0xff, | |
110 | (color.index >> 8) & 0xff, | |
111 | color.index & 0xff | |
112 | }; | |
113 | } | |
114 | ||
115 | namespace ldraw | |
116 | { | |
117 | using Color = ColorIndex; | |
118 | } |