src/colors.cpp

branch
projects
changeset 935
8d98ee0dc917
parent 931
85080f7a1c20
child 941
f895379d7fab
equal deleted inserted replaced
930:ab77deb851fa 935:8d98ee0dc917
1 /*
2 * LDForge: LDraw parts authoring CAD
3 * Copyright (C) 2013 - 2015 Teemu Piippo
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "main.h"
20 #include "colors.h"
21 #include "ldDocument.h"
22 #include "miscallenous.h"
23 #include "mainWindow.h"
24 #include "ldConfig.h"
25 #include <QColor>
26
27 static LDColor g_LDConfigColors[512];
28
29 void InitColors()
30 {
31 LDColorData* col;
32 print ("Initializing color information.\n");
33
34 // Always make sure there's 16 and 24 available. They're special like that.
35 col = new LDColorData;
36 col->m_faceColor =
37 col->m_hexcode = "#AAAAAA";
38 col->m_edgeColor = Qt::black;
39 g_LDConfigColors[16] = col;
40
41 col = new LDColorData;
42 col->m_faceColor =
43 col->m_edgeColor =
44 col->m_hexcode = "#000000";
45 g_LDConfigColors[24] = col;
46
47 LDConfigParser::parseLDConfig();
48 }
49
50 LDColor MainColor()
51 {
52 return g_LDConfigColors[MainColorIndex];
53 }
54
55 LDColor EdgeColor()
56 {
57 return g_LDConfigColors[EdgeColorIndex];
58 }
59
60 void LDColor::addLDConfigColor (qint32 index, LDColor color)
61 {
62 assert (index >= 0 and index < countof (g_LDConfigColors));
63 g_LDConfigColors[index] = color;
64 }
65
66 LDColor LDColor::fromIndex (qint32 index)
67 {
68 if (index < countof (g_LDConfigColors) and g_LDConfigColors[index] != null)
69 return g_LDConfigColors[index];
70
71 if (index >= 0x2000000)
72 {
73 // Direct color
74 QColor col;
75 col.setRed ((index & 0x0FF0000) >> 16);
76 col.setGreen ((index & 0x000FF00) >> 8);
77 col.setBlue (index & 0x00000FF);
78
79 if (index >= 0x3000000)
80 col.setAlpha (128);
81
82 LDColorData* color = new LDColorData;
83 color->m_name = "0x" + QString::number (index, 16).toUpper();
84 color->m_faceColor = col;
85 color->m_edgeColor = Luma (col) < 48 ? Qt::white : Qt::black;
86 color->m_hexcode = col.name();
87 color->m_index = index;
88 return LDColor (color);
89 }
90
91 return null;
92 }
93
94 QString LDColor::indexString() const
95 {
96 if (isDirect())
97 return "0x" + QString::number (index(), 16).toUpper();
98
99 return QString::number (index());
100 }
101
102 bool LDColor::isDirect() const
103 {
104 return index() >= 0x02000000;
105 }
106
107 bool LDColor::operator== (LDColor const& other)
108 {
109 if ((data() == nullptr) ^ (other == nullptr))
110 return false;
111
112 if (data() != nullptr)
113 return index() == other.index();
114
115 // both are null
116 return true;
117 }
118
119 int Luma (const QColor& col)
120 {
121 return (0.2126f * col.red()) +
122 (0.7152f * col.green()) +
123 (0.0722f * col.blue());
124 }
125
126 int CountLDConfigColors()
127 {
128 return countof (g_LDConfigColors);
129 }

mercurial