src/colors.h

Mon, 06 Mar 2017 00:55:45 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Mon, 06 Mar 2017 00:55:45 +0200
changeset 1199
613a981223a6
parent 1174
91696a2e022c
child 1269
ec691d9472b3
permissions
-rw-r--r--

Grid is now a namespace.

/*
 *  LDForge: LDraw parts authoring CAD
 *  Copyright (C) 2013 - 2017 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 "basics.h"

/*
 * ColorData
 *
 * This class contains the model data for LDConfig-defined colors.
 */
class ColorData
{
public:
	struct Entry
	{
		QString name;
		QColor faceColor;
		QColor edgeColor;
	};

	ColorData();
	void loadFromLdconfig();
	bool contains(int code) const;
	const Entry& get(int code) const;

private:
	Entry m_data[512];
};

/*
 * LDColor
 *
 * This class represents an LDraw color. For a color index, this class provides information for that color.
 * It only contains the index of the color. Therefore it is a wrapper around an integer and should be passed by value.
 */
class LDColor
{
public:
	LDColor();
	LDColor(qint32 index);
	LDColor(QColor color, bool transparent = false);
	LDColor(const LDColor& other) = default;

	bool isLDConfigColor() const;
	bool isValid() const;
	QString name() const;
	QString hexcode() const;
	QColor faceColor() const;
	QColor edgeColor() const;
	qint32 index() const;
	bool isDirect() const;
	QString indexString() const;

	static const LDColor nullColor;
	static qint32 directColorIndex(QColor color, bool transparent = false);
	static void initColors();

	LDColor& operator=(qint32 index) { m_index = index; return *this; }
	LDColor& operator=(const LDColor &other) = default;
	LDColor operator++() { return ++m_index; }
	LDColor operator++(int) { return m_index++; }
	LDColor operator--() { return --m_index; }
	LDColor operator--(int) { return m_index--; }
	bool operator==(LDColor other) const { return index() == other.index(); }
	bool operator!=(LDColor other) const { return index() != other.index(); }
	bool operator<(LDColor other) const { return index() < other.index(); }
	bool operator<=(LDColor other) const { return index() <= other.index(); }
	bool operator>(LDColor other) const { return index() > other.index(); }
	bool operator>=(LDColor other) const { return index() >= other.index(); }

private:
	static ColorData* colorData;
	const ColorData::Entry& data() const;

	qint32 m_index;
};

uint qHash(LDColor color);

/*
 * LDConfigParser
 *
 * Parses LDConfig.ldr.
 */
class LDConfigParser
{
public:
	LDConfigParser(QString inputText);

	bool getToken(QString& tokenText, int position);
	bool findToken(int& tokenPosition, QString needle, int args);
	bool compareToken(int inPos, QString text);
	bool parseTag(QString key, QString& value);

private:
	QStringList m_tokens;
};

int luma(const QColor& col);

enum
{
	MainColor = 16,
	EdgeColor = 24,
};

mercurial