src/colors.cpp

changeset 139
72098474d362
parent 132
488d0ba6070b
child 178
a23024fc98e0
--- a/src/colors.cpp	Wed Sep 22 13:28:53 2021 +0300
+++ b/src/colors.cpp	Wed Sep 22 14:03:43 2021 +0300
@@ -18,13 +18,22 @@
 
 #include "colors.h"
 
-const ldraw::ColorTable::ColorDefinition ldraw::ColorTable::unknownColor{{}, {}, "Unknown", "???"};
+const ldraw::ColorDefinition ldraw::ColorTable::unknownColor{{}, {}, "Unknown", "???"};
 
+/**
+ * @brief Clears the color table
+ */
 void ldraw::ColorTable::clear()
 {
-	definitions = {};
+	this->definitions = {};
 }
 
+/**
+ * @brief Loads colors from LDConfig.ldr
+ * @param device Opened LDConfig.ldr I/O device
+ * @param errors Where to write any errors into
+ * @returns whether or not it succeeded.
+ */
 Result ldraw::ColorTable::load(QIODevice& device, QTextStream& errors)
 {
 	this->clear();
@@ -45,7 +54,12 @@
 	}
 }
 
-const ldraw::ColorTable::ColorDefinition& ldraw::ColorTable::operator[](Color color) const
+/**
+ * @brief Gets color information by color index.
+ * @param color
+ * @returns color table information
+ */
+const ldraw::ColorDefinition& ldraw::ColorTable::operator[](Color color) const
 {
 	auto it = this->definitions.find(color.index);
 	if (it != this->definitions.end())
@@ -58,6 +72,19 @@
 	}
 }
 
+/**
+ * @brief Gets the amount of elements in the color table
+ * @returns int
+ */
+int ldraw::ColorTable::size() const
+{
+	return this->definitions.size();
+}
+
+/**
+ * @brief Parses an LDConfig.ldr line from a string
+ * @param string LDConfig.ldr line to parse
+ */
 void ldraw::ColorTable::loadColorFromString(const QString& string)
 {
 	const QRegExp pattern{
@@ -70,7 +97,7 @@
 	if (pattern.indexIn(string) != -1)
 	{
 		const int code = pattern.cap(2).toInt();
-		ColorDefinition& definition = definitions[code];
+		ColorDefinition& definition = this->definitions[code];
 		definition = {}; // in case there's an existing definition
 		definition.name = pattern.cap(1);
 		definition.displayName = definition.name;
@@ -85,25 +112,42 @@
 	}
 }
 
-/*
- * Calculates the luma-value for the given color.
- * c.f. https://en.wikipedia.org/wiki/Luma_(video)
+/**
+ * @brief Calculates the luma-value for the given color.
+ * @details c.f. https://en.wikipedia.org/wiki/Luma_(video)
+ * @param color
+ * @returns luma value [0, 1]
  */
 double luma(const QColor& color)
 {
 	return 0.2126 * color.redF() + 0.7152 * color.greenF() + 0.0722 * color.blueF();
 }
 
+/**
+ * @brief Returns a direct color index that codes the specified color value
+ * @param color
+ * @returns direct color index
+ */
 ldraw::Color ldraw::directColor(const QColor& color)
 {
 	return ldraw::Color{0x2000000 | (color.red() << 16) | (color.green() << 8) | color.blue()};
 }
 
+/**
+ * @brief Checks whether or not the specified color index is a direct color
+ * @param color Color to check
+ * @returns bool
+ */
 bool ldraw::isDirectColor(ldraw::Color color)
 {
 	return color.index >= 0x2000000;
 }
 
+/**
+ * @brief Extracts the color value from a direct color index
+ * @param color Direct color index
+ * @returns color value. Returns a default-constructed QColor in case a non-direct color is given.
+ */
 QColor ldraw::directColorFace(ldraw::Color color)
 {
 	if (isDirectColor(color))
@@ -116,6 +160,12 @@
 	}
 }
 
+/**
+ * @brief Gets the face color for the specified color index
+ * @param color Color index to get face color for
+ * @param colorTable Color table to use for lookup
+ * @returns QColor
+ */
 QColor ldraw::colorFace(ldraw::Color color, const ldraw::ColorTable& colorTable)
 {
 	if (isDirectColor(color))
@@ -128,11 +178,23 @@
 	}
 }
 
+/**
+ * @brief Writes a color index into a @c QDataStream
+ * @param stream
+ * @param color
+ * @returns stream
+ */
 QDataStream& operator<<(QDataStream& stream, ldraw::Color color)
 {
 	return stream << color.index;
 }
 
+/**
+ * @brief Reads a color index from a @c QDataStream
+ * @param stream
+ * @param color
+ * @returns stream
+ */
 QDataStream& operator>>(QDataStream& stream, ldraw::Color& color)
 {
 	return stream >> color.index;

mercurial