--- a/src/colors.cpp Mon Aug 24 23:02:30 2020 +0300 +++ b/src/colors.cpp Mon Sep 21 19:48:18 2020 +0300 @@ -18,7 +18,7 @@ #include "colors.h" -const ldraw::ColorTable::ColorDefinition ldraw::ColorTable::unknownColor{{}, {}, "Unknown"}; +const ldraw::ColorTable::ColorDefinition ldraw::ColorTable::unknownColor{{}, {}, "Unknown", "???"}; void ldraw::ColorTable::clear() { @@ -50,7 +50,7 @@ auto it = this->definitions.find(color.index); if (it != this->definitions.end()) { - return *it; + return it->second; } else { @@ -73,6 +73,8 @@ ColorDefinition& definition = definitions[code]; definition = {}; // in case there's an existing definition definition.name = pattern.cap(1); + definition.displayName = definition.name; + definition.displayName.replace("_", " "); definition.faceColor = pattern.cap(3); definition.edgeColor = pattern.cap(4); if (not pattern.cap(5).isEmpty()) @@ -91,3 +93,37 @@ { return 0.2126 * color.redF() + 0.7152 * color.greenF() + 0.0722 * color.blueF(); } + +ldraw::Color ldraw::directColor(const QColor& color) +{ + return ldraw::Color{0x2000000 | (color.red() << 16) | (color.green() << 8) | color.blue()}; +} + +bool ldraw::isDirectColor(ldraw::Color color) +{ + return color.index >= 0x2000000; +} + +QColor ldraw::directColorFace(ldraw::Color color) +{ + if (isDirectColor(color)) + { + return {(color.index >> 16) & 0xff, (color.index >> 8) & 0xff, color.index & 0xff}; + } + else + { + return {}; + } +} + +QColor ldraw::colorFace(ldraw::Color color, const ldraw::ColorTable& colorTable) +{ + if (isDirectColor(color)) + { + return directColorFace(color); + } + else + { + return colorTable[color].faceColor; + } +}