src/colors.cpp

Thu, 03 Mar 2022 11:42:52 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 03 Mar 2022 11:42:52 +0200
changeset 150
b6cbba6e29a1
parent 139
72098474d362
child 178
a23024fc98e0
permissions
-rw-r--r--

extract polygon cache out of Model

/*
 *  LDForge: LDraw parts authoring CAD
 *  Copyright (C) 2013 - 2020 Teemu Piippo
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "colors.h"

const ldraw::ColorDefinition ldraw::ColorTable::unknownColor{{}, {}, "Unknown", "???"};

/**
 * @brief Clears the color table
 */
void ldraw::ColorTable::clear()
{
	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();
	if (device.isReadable())
	{
		QTextStream stream{&device};
		QString line;
		while (stream.readLineInto(&line))
		{
			this->loadColorFromString(line);
		}
		return Success;
	}
	else
	{
		errors << "could not read colors";
		return Failure;
	}
}

/**
 * @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())
	{
		return it->second;
	}
	else
	{
		return unknownColor;
	}
}

/**
 * @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{
		R"(^\s*0 \!COLOUR\s+([^\s]+)\s+)"_q +
		R"(CODE\s+(\d+)\s+)"_q +
		R"(VALUE\s+(\#[0-9a-fA-F]{3,6})\s+)"_q +
		R"(EDGE\s+(\#[0-9a-fA-F]{3,6}))"_q +
		R"((?:\s+ALPHA\s+(\d+))?)"_q
	};
	if (pattern.indexIn(string) != -1)
	{
		const int code = pattern.cap(2).toInt();
		ColorDefinition& definition = this->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())
		{
			const int alpha = pattern.cap(5).toInt();
			definition.faceColor.setAlpha(alpha);
		}
	}
}

/**
 * @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))
	{
		return {(color.index >> 16) & 0xff, (color.index >> 8) & 0xff, color.index & 0xff};
	}
	else
	{
		return {};
	}
}

/**
 * @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))
	{
		return directColorFace(color);
	}
	else
	{
		return colorTable[color].faceColor;
	}
}

/**
 * @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