-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpalette.cpp
81 lines (68 loc) · 1.64 KB
/
palette.cpp
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
#include "palette.hpp"
Idatag_palette::Idatag_palette(const std::vector<std::string>& feeders)
{
this->feeders = feeders;
srand(static_cast <unsigned> (time(0)));
this->generate_colours(32);
this->associate_colours();
}
void Idatag_palette::refresh_feeders(const std::vector<std::string>& feeders)
{
this->feeders = feeders;
}
void Idatag_palette::associate_colours()
{
for (const auto & feeder : this->feeders)
{
this->associate_colour(feeder);
}
}
void Idatag_palette::associate_colour(std::string feeder)
{
uint index;
QColor colour;
auto it = std::find(this->feeders.begin(), this->feeders.end(), feeder);
index = distance(this->feeders.begin(), it);
if (this->colours.size() >= index)
{
colour = this->colours[index];
}
else {
do {
this->generate_colour();
} while (this->colours.size() < index);
colour = this->colours[index];
}
this->association.insert(std::pair<std::string, QColor>(feeder, colour));
}
void Idatag_palette::generate_colour()
{
QColor colour;
qreal golden_ratio_conjugate = 0.618033988749895;
float r;
qreal hue;
r = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
hue = r;
hue += golden_ratio_conjugate;
hue = fmodf(hue, 1.0);
colour = colour.fromHsvF(hue, 0.7, 0.7);
this->colours.push_back(colour);
}
void Idatag_palette::generate_colours(int nb_colour)
{
for (int i = 0; i < nb_colour; i++)
{
this->generate_colour();
}
}
QColor Idatag_palette::get_feeder_colour(const std::string& feeder)
{
if (this->association.find(feeder) == this->association.end())
{
this->associate_colour(feeder);
return this->association[feeder];
}
else {
return this->association[feeder];
}
}