-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColor.cpp
81 lines (70 loc) · 1.94 KB
/
Color.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
/*
* Color.cpp
*
* Created on: Jun 16, 2010
* Author: KR initial implementation in header
* TF moved implementation to separate source file
* \copyright
* Copyright (c) 2015, OpenGeoSys Community (http://www.opengeosys.org)
* Distributed under a Modified BSD License.
* See accompanying file LICENSE.txt or
* http://www.opengeosys.org/project/license
*/
#include <iostream>
#include <sstream>
#include "Color.h"
#include "StringTools.h"
namespace GEOLIB
{
Color* getRandomColor()
{
return new Color((rand() % 5) * 50, (rand() % 5) * 50, (rand() % 5) * 50);
}
int readColorLookupTable(std::map<std::string, Color*>& colors, const std::string& filename)
{
std::string id = "", line = "";
std::ifstream in(filename.c_str());
if (!in.is_open())
{
std::cout << "Color::readLookupTable() - Could not open file..."
<< "\n";
return 0;
}
while (getline(in, line))
{
std::list<std::string> fields = splitString(line, '\t');
Color* c = new Color();
if (fields.size() >= 4)
{
id = fields.front();
fields.pop_front();
(*c)[0] = atoi(fields.front().c_str());
fields.pop_front();
(*c)[1] = atoi(fields.front().c_str());
fields.pop_front();
(*c)[2] = atoi(fields.front().c_str());
colors.insert(std::pair<std::string, Color*>(id, c));
}
else
delete c;
}
return 1;
}
const Color* getColor(const std::string& id, std::map<std::string, Color*>& colors)
{
for (std::map<std::string, Color*>::const_iterator it = colors.begin(); it != colors.end(); ++it)
if (id.compare(it->first) == 0)
return it->second;
std::cout << "Key \"" << id << "\" not found in color lookup table..."
<< "\n";
Color* c = getRandomColor();
colors.insert(std::pair<std::string, Color*>(id, c));
return c;
}
const Color* getColor(double id, std::map<std::string, GEOLIB::Color*>& colors)
{
std::ostringstream stream;
stream << id;
return getColor(stream.str(), colors);
}
}