src/colors.h

Mon, 27 Sep 2021 21:04:45 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Mon, 27 Sep 2021 21:04:45 +0300
changeset 140
2f383e88acf4
parent 139
72098474d362
child 178
a23024fc98e0
permissions
-rw-r--r--

work on saving

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

#pragma once
#include <QColor>
#include "main.h"

namespace ldraw
{
	struct Color;
	struct ColorDefinition;
	class ColorTable;
	Color directColor(const QColor& color);
	bool isDirectColor(Color color);
	QColor directColorFace(Color color);
	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:
	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;
private:
	void loadColorFromString(const QString& string);
	std::map<qint32, ColorDefinition> definitions;
};

double luma(const QColor& color);

mercurial