src/colors.cc

Sun, 27 Jul 2014 02:55:27 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sun, 27 Jul 2014 02:55:27 +0300
changeset 849
4af6f977d04b
parent 844
11587d419d2f
child 861
83426c5fa732
permissions
-rw-r--r--

- fixed: direct color support did not work properly with full-black #000000

655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 /*
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
844
11587d419d2f - changed copyright lines to use my legal name instead of my nickname
Teemu Piippo <crimsondusk64@gmail.com>
parents: 840
diff changeset
3 * Copyright (C) 2013, 2014 Teemu Piippo
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4 *
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8 * (at your option) any later version.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 *
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 * GNU General Public License for more details.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14 *
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 */
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 #include "main.h"
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 #include "colors.h"
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21 #include "ldDocument.h"
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 #include "miscallenous.h"
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23 #include "mainWindow.h"
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24 #include "ldConfig.h"
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25 #include <QColor>
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26
795
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
27 static LDColor g_LDConfigColors[512];
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29 void initColors()
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 {
806
4240f47aa2d4 - moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 797
diff changeset
31 LDColorData* col;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32 print ("Initializing color information.\n");
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34 // Always make sure there's 16 and 24 available. They're special like that.
795
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
35 col = new LDColorData;
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
36 col->_faceColor =
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
37 col->_hexcode = "#AAAAAA";
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
38 col->_edgeColor = Qt::black;
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
39 g_LDConfigColors[16] = col;
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
40
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
41 col = new LDColorData;
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
42 col->_faceColor =
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
43 col->_edgeColor =
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
44 col->_hexcode = "#000000";
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
45 g_LDConfigColors[24] = col;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46
795
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
47 LDConfigParser::parseLDConfig();
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
48 }
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49
795
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
50 LDColor maincolor()
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
51 {
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
52 return g_LDConfigColors[16];
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
53 }
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
54
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
55 LDColor edgecolor()
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
56 {
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
57 return g_LDConfigColors[24];
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59
795
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
60 void LDColor::addLDConfigColor (qint32 index, LDColor color)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 {
840
d077dd19bf9a - changed `&&` and `||` operators to their named variants `and` and `or`
Teemu Piippo <crimsondusk64@gmail.com>
parents: 806
diff changeset
62 assert (index >= 0 and index < countof (g_LDConfigColors));
795
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
63 g_LDConfigColors[index] = color;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65
795
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
66 LDColor LDColor::fromIndex (qint32 index)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 {
840
d077dd19bf9a - changed `&&` and `||` operators to their named variants `and` and `or`
Teemu Piippo <crimsondusk64@gmail.com>
parents: 806
diff changeset
68 if (index < countof (g_LDConfigColors) and g_LDConfigColors[index] != null)
795
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
69 return g_LDConfigColors[index];
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
70
849
4af6f977d04b - fixed: direct color support did not work properly with full-black #000000
Teemu Piippo <crimsondusk64@gmail.com>
parents: 844
diff changeset
71 if (index >= 0x2000000)
795
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
72 {
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
73 // Direct color
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
74 QColor col;
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
75 col.setRed ((index & 0x0FF0000) >> 16);
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
76 col.setGreen ((index & 0x000FF00) >> 8);
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
77 col.setBlue (index & 0x00000FF);
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
78
849
4af6f977d04b - fixed: direct color support did not work properly with full-black #000000
Teemu Piippo <crimsondusk64@gmail.com>
parents: 844
diff changeset
79 if (index >= 0x3000000)
795
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
80 col.setAlpha (128);
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
81
806
4240f47aa2d4 - moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 797
diff changeset
82 LDColorData* color = new LDColorData;
795
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
83 color->_name = "0x" + QString::number (index, 16).toUpper();
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
84 color->_faceColor = col;
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
85 color->_edgeColor = luma(col) < 48 ? Qt::white : Qt::black;
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
86 color->_hexcode = col.name();
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
87 color->_index = index;
806
4240f47aa2d4 - moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 797
diff changeset
88 return LDColor (color);
795
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
89 }
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
90
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
91 return null;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 }
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93
806
4240f47aa2d4 - moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 797
diff changeset
94 QString LDColor::indexString() const
4240f47aa2d4 - moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 797
diff changeset
95 {
4240f47aa2d4 - moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 797
diff changeset
96 if (isDirect())
4240f47aa2d4 - moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 797
diff changeset
97 return "0x" + QString::number (index(), 16).toUpper();
4240f47aa2d4 - moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 797
diff changeset
98
4240f47aa2d4 - moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 797
diff changeset
99 return QString::number (index());
4240f47aa2d4 - moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 797
diff changeset
100 }
4240f47aa2d4 - moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 797
diff changeset
101
4240f47aa2d4 - moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 797
diff changeset
102 bool LDColor::isDirect() const
4240f47aa2d4 - moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 797
diff changeset
103 {
4240f47aa2d4 - moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 797
diff changeset
104 return index() >= 0x02000000;
4240f47aa2d4 - moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 797
diff changeset
105 }
4240f47aa2d4 - moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 797
diff changeset
106
4240f47aa2d4 - moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 797
diff changeset
107 bool LDColor::operator== (LDColor const& other)
4240f47aa2d4 - moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 797
diff changeset
108 {
4240f47aa2d4 - moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 797
diff changeset
109 if ((data() == nullptr) ^ (other == nullptr))
4240f47aa2d4 - moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 797
diff changeset
110 return false;
4240f47aa2d4 - moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 797
diff changeset
111
4240f47aa2d4 - moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 797
diff changeset
112 if (data() != nullptr)
4240f47aa2d4 - moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 797
diff changeset
113 return index() == other.index();
4240f47aa2d4 - moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 797
diff changeset
114
4240f47aa2d4 - moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 797
diff changeset
115 // both are null
4240f47aa2d4 - moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 797
diff changeset
116 return true;
4240f47aa2d4 - moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 797
diff changeset
117 }
4240f47aa2d4 - moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents: 797
diff changeset
118
706
d79083b9f74d Merge ../ldforge into gl
Santeri Piippo <crimsondusk64@gmail.com>
parents: 655
diff changeset
119 int luma (const QColor& col)
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
120 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
121 return (0.2126f * col.red()) +
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
122 (0.7152f * col.green()) +
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
123 (0.0722f * col.blue());
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
124 }
795
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
125
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
126 int numLDConfigColors()
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
127 {
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
128 return countof (g_LDConfigColors);
195fa1fff9c3 - changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 706
diff changeset
129 }

mercurial