-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorTable.cpp
More file actions
118 lines (95 loc) · 3.35 KB
/
ColorTable.cpp
File metadata and controls
118 lines (95 loc) · 3.35 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
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "ColorTable.h"
#include <QColor>
#include <QtMath>
float lerp(float v0, float v1, float t) {
float r = v0*(1-t)+v1*t;
return r;
}
ColorTable::ColorTable(float alphaVal[2], int step)
{
this->nStep = step;
this->alpha[0] = alphaVal[0];
this->alpha[1] = alphaVal[1];
QVector<QRgb> gColorTable (nStep);
for (int i = 0; i<nStep; ++i) {
gColorTable[i] = qRgb(i, i, i);
}
graysColorTable = gColorTable;
m_colorTable["Greys"] = gColorTable;
}
void ColorTable::registerMap(const QString &name, const std::map<FloatKey, rgb> &colorMap)
{
m_colorMapList[name] = colorMap;
//"jet":[{"index":0,"rgb":[0,0,131]},{"index":0.125,"rgb":[0,60,170]},{"index":0.375,"rgb":[5,255,255]},{"index":0.625,"rgb":[255,255,0]},{"index":0.875,"rgb":[250,0,0]},{"index":1,"rgb":[128,0,0]}],
QList<float> indicies;
std::map<FloatKey, rgb>::const_iterator i = colorMap.cbegin();
while (i != colorMap.cend()) {
indicies.append(i->first.id*this->nStep);
++i;
}
// making sure alpha is between 0 and 1
this->alpha[0] = qMin<float>( qMax<float>(this->alpha[0], 0), 1);
this->alpha[1] = qMin<float>( qMax<float>(this->alpha[1], 0), 1);
QList<rgba> steps;
std::map<FloatKey, rgb>::const_iterator c = colorMap.cbegin();
while (c != colorMap.cend()) {
int avgAlpha = (this->alpha[0] + (this->alpha[1] - this->alpha[0])*c->first.id)*255; // calse it between 0-255
steps.append(rgba(c->second.r, c->second.g, c->second.b, avgAlpha));
++c;
}
/*
* map increasing linear values between indicies to
* linear steps in colorvalues
*/
QVector<QRgb> colors;
for (int i = 0; i < indicies.size()-1; i++) {
int scaleRange = qRound(indicies[i+1] - indicies[i]);
rgba fromrgba = steps[i];
rgba torgba = steps[i+1];
for (int j = 0; j < scaleRange; j++) {
float amt = (float)j / (float)scaleRange;
int r = qRound(lerp(fromrgba.r, torgba.r, amt));
int g = qRound(lerp(fromrgba.g, torgba.g, amt));
int b = qRound(lerp(fromrgba.b, torgba.b, amt));
colors.append(qRgba(r,g,b,255));
}
}
rgb last = colorMap.rbegin()->second;
// QRgb lastQRgb = qRgba(last.r, last.g, last.b, this->alpha[1]);
QRgb lastQRgb = qRgb(last.r, last.g, last.b);
// colors[this->nStep-1]= lastQRgb;
colors.insert(this->nStep-1, lastQRgb);
m_colorTable[name] = colors;
}
QStringList ColorTable::availableColorMaps() const
{
QStringList keys;
std::map<QString, QVector<QRgb>>::const_iterator it = m_colorTable.cbegin();
while (it != m_colorTable.cend()) {
keys.append(it->first);
++it;
}
return keys;
}
QVector<QRgb> ColorTable::colorTable(const QString &name)
{
// if(m_colorTable.contains)
std::map<QString, QVector<QRgb>>::iterator check = m_colorTable.find(name);
if(check != m_colorTable.end()){
return check->second;
} else {
return graysColorTable;
}
// return m_colorTable.value(name, graysColorTable);
}
///////////////////////////////////////////////////////
bool rgb::operator<(const rgb & ot) const
{
return (this->r < ot.r) && (this->g < ot.g) && (this->b < ot.b);
}
rgb::rgb(int _r, int _g, int _b)
:r(_r), g(_g), b(_b){
}
rgba::rgba(int _r, int _g, int _b, int _a)
:r(_r), g(_g), b(_b), a(_a){
}