--- a/src/colors.h Wed Sep 22 13:28:53 2021 +0300 +++ b/src/colors.h Wed Sep 22 14:03:43 2021 +0300 @@ -23,6 +23,7 @@ namespace ldraw { struct Color; + struct ColorDefinition; class ColorTable; Color directColor(const QColor& color); bool isDirectColor(Color color); @@ -30,80 +31,118 @@ QColor colorFace(Color color, const ColorTable& colorTable); } +/** + * @brief Represents an LDraw color index (e.g 16, 24, ...) + * + * @details + * This is essentially just an integer. It is its own structure for the sake + * of type correctness. Commonly used color values are 16 for the main color + * and 24 for the edge color. + * + * Use @c ldraw::ColorTable to obtain rgb values for color indices. + * + * Direct colors are colors whose index is at least 0x2000000. These colors + * encode the rgb values directly in the index. The color table code also + * handles these cases. + * + * Note that it only makes sense to construct a color index from an integer + * value if the integer comes from an external source (e.g. user or + * LDConfig.ldr). Since this structure only contains an integer, it should be + * passed by value to functions instead of l-value reference. + */ struct ldraw::Color { qint32 index = 0; }; Q_DECLARE_METATYPE(ldraw::Color) +QDataStream& operator<<(QDataStream&, ldraw::Color); +QDataStream& operator>>(QDataStream&, ldraw::Color&); +namespace ldraw +{ + constexpr bool operator==(Color one, Color other) + { + return one.index == other.index; + } + + constexpr bool operator!=(Color one, Color other) + { + return one.index != other.index; + } + + constexpr bool operator<(Color one, Color other) + { + return one.index < other.index; + } + + constexpr bool operator<=(Color one, Color other) + { + return one.index <= other.index; + } + + constexpr bool operator>(Color one, Color other) + { + return one.index > other.index; + } + + constexpr bool operator>=(Color one, Color other) + { + return one.index >= other.index; + } + + static constexpr Color BLACK {0}; + static constexpr Color BLUE {1}; + static constexpr Color GREEN {2}; + static constexpr Color RED {4}; + static constexpr Color YELLOW {14}; + static constexpr Color WHITE {15}; + static constexpr Color MAIN_COLOR {16}; + static constexpr Color EDGE_COLOR {24}; +} + +/** + * @brief Contains the information about a specific color + */ +struct ldraw::ColorDefinition +{ + /** + * @brief The face color. This is the color used for most objects. + * This is also used for the polygons of a subfile reference. + */ + QColor faceColor; + /** + * @brief The edge color, used for the edge lines of a subfile reference. + * Note that edges using a color actually use the face color. LDraw standards + * require edges to use color 24, however. + */ + QColor edgeColor; + /** + * @brief Name of the color + */ + QString name; + /** + * @brief A version of the name that can be shown to the user. + */ + QString displayName; +}; + +/** + * @brief Reads LDConfig.ldr and contains color table information for lookup purposes. + */ class ldraw::ColorTable { public: - struct ColorDefinition - { - QColor faceColor; - QColor edgeColor; - QString name; - QString displayName; - }; void clear(); Result load(QIODevice& device, QTextStream& errors); const ColorDefinition& operator[](Color index) const; static const ColorDefinition unknownColor; auto begin() const { return this->definitions.begin(); } auto end() const { return this->definitions.end(); } - int size() const { return this->definitions.size(); } + int size() const; private: void loadColorFromString(const QString& string); std::map<qint32, ColorDefinition> definitions; }; -namespace ldraw -{ - inline bool operator==(const ldraw::Color& one, const ldraw::Color& other) - { - return one.index == other.index; - } - - inline bool operator!=(const ldraw::Color& one, const ldraw::Color& other) - { - return one.index != other.index; - } - - inline bool operator<(const ldraw::Color& one, const ldraw::Color& other) - { - return one.index < other.index; - } - - inline bool operator<=(const ldraw::Color& one, const ldraw::Color& other) - { - return one.index <= other.index; - } - - inline bool operator>(const ldraw::Color& one, const ldraw::Color& other) - { - return one.index > other.index; - } - - inline bool operator>=(const ldraw::Color& one, const ldraw::Color& other) - { - return one.index >= other.index; - } -} - -namespace ldraw -{ - static constexpr Color black {0}; - static constexpr Color blue {1}; - static constexpr Color green {2}; - static constexpr Color red {4}; - static constexpr Color yellow {14}; - static constexpr Color white {15}; - static constexpr Color mainColor {16}; - static constexpr Color edgeColor {24}; -} - double luma(const QColor& color); -QDataStream& operator<<(QDataStream&, ldraw::Color); -QDataStream& operator>>(QDataStream&, ldraw::Color&);