-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcolor.cpp
More file actions
executable file
·105 lines (98 loc) · 3.12 KB
/
color.cpp
File metadata and controls
executable file
·105 lines (98 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/****************************************************************************
**
** Copyright (C) 2007-2009 Kevin Clague. All rights reserved.
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file. Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://www.trolltech.com/products/qt/opensource.html
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/
#include "color.h"
#include <QMessageBox>
#include <QFile>
#include <QTextStream>
#include <QRegExp>
#include "lpub_preferences.h"
QHash<QString, QColor> LDrawColor::name2color;
QHash<QString, QString> LDrawColor::color2name;
/*
* This constructor reads in the LDraw ldconfig.ldr file and extracts
* the color codes, color names, and color values and puts them in
* the xlate (name to color translate) hash table.
*/
LDrawColor::LDrawColor ()
{
name2color.clear();
color2name.clear();
QString fileName(Preferences::ldrawPath + "/ldconfig.ldr");
QFile file(fileName);
if (! file.open(QFile::ReadOnly | QFile::Text)) {
QMessageBox::warning(NULL,QMessageBox::tr("LDrawColor"),
QMessageBox::tr("Cannot read file %1\n%2.")
.arg(fileName)
.arg(file.errorString()));
return;
}
QRegExp rx("^\\s*0\\s+!COLOUR\\s+(\\w+)\\s+"
"CODE\\s+(\\d+)\\s+VALUE\\s+#([\\da-fA-F]+)");
QTextStream in(&file);
while ( ! in.atEnd()) {
QString line = in.readLine(0);
if (line.contains(rx)) {
bool ok;
QRgb hex = rx.cap(3).toLong(&ok,16);
QColor color(hex);
color.setAlpha(0xff);
QString name = rx.cap(1).toLower();
name2color.insert(name,color);
name = rx.cap(2).toLower();
name2color.insert(name,color);
name = rx.cap(1);
QString code = rx.cap(2);
color2name.insert(code,name);
color2name.insert(color.name(),name);
}
}
}
/*
* This function provides the translate from LDraw color names and codes
* to QColor.
*/
QColor LDrawColor::color(QString nickname)
{
QString lower(nickname.toLower());
if (name2color.contains(lower)) {
return name2color[lower];
} else {
QRegExp rx("\\s*(0x|#)([\\da-fA-F]+)\\s*$");
if (nickname.contains(rx)) {
QString prefix("0xf"+rx.cap(2));
bool ok;
QRgb rgb = prefix.toLong(&ok,16);
QColor color(rgb);
color.setAlpha(0xff);
return color;
} else {
return Qt::black;
}
}
}
/*
* This function provides the translate from QColor back to LDraw color
* names. If there is no translation the hexadecimal value for the
* color is returned as a string.
*/
QString LDrawColor::name(QString code)
{
if (color2name.contains(code)) {
return color2name[code];
} else {
return "";
}
}