SUMO - Simulation of Urban MObility
Main Page
Related Pages
Modules
Data Structures
Files
File List
Globals
All
Data Structures
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
Pages
RGBColor.cpp
Go to the documentation of this file.
1
/****************************************************************************/
10
// A RGB-color definition
11
/****************************************************************************/
12
// SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
13
// Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
14
/****************************************************************************/
15
//
16
// This file is part of SUMO.
17
// SUMO is free software: you can redistribute it and/or modify
18
// it under the terms of the GNU General Public License as published by
19
// the Free Software Foundation, either version 3 of the License, or
20
// (at your option) any later version.
21
//
22
/****************************************************************************/
23
24
25
// ===========================================================================
26
// included modules
27
// ===========================================================================
28
#ifdef _MSC_VER
29
#include <
windows_config.h
>
30
#else
31
#include <
config.h
>
32
#endif
33
34
#include <cmath>
35
#include <string>
36
#include <sstream>
37
#include <
utils/common/StringTokenizer.h
>
38
#include <
utils/common/TplConvert.h
>
39
#include <
utils/common/MsgHandler.h
>
40
#include <
utils/common/StdDefs.h
>
41
#include "
RGBColor.h
"
42
43
#ifdef CHECK_MEMORY_LEAKS
44
#include <
foreign/nvwa/debug_new.h
>
45
#endif // CHECK_MEMORY_LEAKS
46
47
48
// ===========================================================================
49
// static member definitions
50
// ===========================================================================
51
const
std::string
RGBColor::DEFAULT_COLOR_STRING
=
"1,1,0"
;
52
const
RGBColor
RGBColor::DEFAULT_COLOR
=
RGBColor::parseColor
(
RGBColor::DEFAULT_COLOR_STRING
);
53
54
55
// ===========================================================================
56
// method definitions
57
// ===========================================================================
58
RGBColor::RGBColor
()
59
: myRed(-1), myGreen(-1), myBlue(-1) {}
60
61
62
RGBColor::RGBColor
(
SUMOReal
red,
SUMOReal
green,
SUMOReal
blue)
63
: myRed(red), myGreen(green), myBlue(blue) {}
64
65
66
RGBColor::RGBColor
(
const
RGBColor
& col)
67
: myRed(col.myRed), myGreen(col.myGreen), myBlue(col.myBlue) {}
68
69
70
RGBColor::~RGBColor
() {}
71
72
73
void
74
RGBColor::set
(
SUMOReal
r,
SUMOReal
g,
SUMOReal
b) {
75
myRed
= r;
76
myGreen
= g;
77
myBlue
= b;
78
}
79
80
81
82
std::ostream&
83
operator<<
(std::ostream& os,
const
RGBColor
& col) {
84
os
85
<< col.
myRed
<<
","
86
<< col.
myGreen
<<
","
87
<< col.
myBlue
;
88
return
os;
89
}
90
91
92
bool
93
RGBColor::operator==
(
const
RGBColor
& c)
const
{
94
return
fabs(
myRed
- c.
myRed
) < 0.1 && fabs(
myGreen
- c.
myGreen
) < 0.1 && fabs(
myBlue
- c.
myBlue
) < 0.1;
95
//return myRed==c.myRed&&myGreen==c.myGreen&&myBlue==c.myBlue;
96
}
97
98
99
bool
100
RGBColor::operator!=
(
const
RGBColor
& c)
const
{
101
return
fabs(
myRed
- c.
myRed
) > 0.1 || fabs(
myGreen
- c.
myGreen
) > 0.1 || fabs(
myBlue
- c.
myBlue
) > 0.1;
102
//return myRed!=c.myRed||myGreen!=c.myGreen||myBlue!=c.myBlue;
103
}
104
105
106
RGBColor
107
RGBColor::changedBrightness
(
SUMOReal
change) {
108
SUMOReal
red
=
MIN2
(
MAX2
(
myRed
+ change, (
SUMOReal
)0), (
SUMOReal
)1);
109
SUMOReal
blue
=
MIN2
(
MAX2
(
myBlue
+ change, (
SUMOReal
)0), (
SUMOReal
)1);
110
SUMOReal
green
=
MIN2
(
MAX2
(
myGreen
+ change, (
SUMOReal
)0), (
SUMOReal
)1);
111
return
RGBColor
(red, green, blue);
112
113
}
114
115
RGBColor
116
RGBColor::parseColor
(
const
std::string& coldef)
throw
(
EmptyData
,
NumberFormatException
) {
117
StringTokenizer
st(coldef,
","
);
118
if
(st.
size
() < 3) {
119
throw
EmptyData
();
120
}
121
const
SUMOReal
r =
TplConvert::_2SUMOReal
(st.
next
().c_str());
122
const
SUMOReal
g =
TplConvert::_2SUMOReal
(st.
next
().c_str());
123
const
SUMOReal
b =
TplConvert::_2SUMOReal
(st.
next
().c_str());
124
return
RGBColor
(r, g, b);
125
}
126
127
128
RGBColor
129
RGBColor::parseColorReporting
(
130
const
std::string& coldef,
const
std::string& objecttype,
131
const
char
* objectid,
bool
report,
bool
& ok) {
132
UNUSED_PARAMETER
(report);
133
try
{
134
return
parseColor
(coldef);
135
}
catch
(
NumberFormatException
&) {
136
}
catch
(
EmptyData
&) {
137
}
138
ok =
false
;
139
std::ostringstream oss;
140
oss <<
"Attribute 'color' in definition of "
;
141
if
(objectid == 0) {
142
oss <<
"a "
;
143
}
144
oss << objecttype;
145
if
(objectid != 0) {
146
oss <<
" '"
<< objectid <<
"'"
;
147
}
148
oss <<
" is not a valid color."
;
149
WRITE_ERROR
(oss.str());
150
return
RGBColor
();
151
}
152
153
154
RGBColor
155
RGBColor::interpolate
(
const
RGBColor
& minColor,
const
RGBColor
& maxColor,
SUMOReal
weight) {
156
if
(weight < 0) {
157
weight = 0;
158
}
159
if
(weight > 1) {
160
weight = 1;
161
}
162
SUMOReal
r = minColor.
myRed
+ (maxColor.
myRed
- minColor.
myRed
) * weight;
163
SUMOReal
g = minColor.
myGreen
+ (maxColor.
myGreen
- minColor.
myGreen
) * weight;
164
SUMOReal
b = minColor.
myBlue
+ (maxColor.
myBlue
- minColor.
myBlue
) * weight;
165
return
RGBColor
(r, g, b);
166
}
167
168
169
RGBColor
170
RGBColor::fromHSV
(
SUMOReal
h,
SUMOReal
s,
SUMOReal
v) {
171
// H is given on [0, 6] or UNDEFINED. S and V are given on [0, 1].
172
// RGB are each returned on [0, 1].
173
//float h = HSV.H, s = HSV.S, v = HSV.V,
174
float
m, n, f;
175
h /= 60.;
176
int
i;
177
//if (h == UNDEFINED) RETURN_RGB(v, v, v);
178
i =
int
(floor(h));
179
f =
float
(h - i);
180
if
(!(i & 1)) {
181
f = 1 - f;
// if i is even
182
}
183
m =
float
(v * (1 - s));
184
n =
float
(v * (1 - s * f));
185
switch
(i) {
186
case
6:
187
case
0:
188
return
RGBColor
(v, n, m);
189
case
1:
190
return
RGBColor
(n, v, m);
191
case
2:
192
return
RGBColor
(m, v, n);
193
case
3:
194
return
RGBColor
(m, n, v);
195
case
4:
196
return
RGBColor
(n, m, v);
197
case
5:
198
return
RGBColor
(v, m, n);
199
}
200
return
RGBColor
(1, 1, 1);
201
}
202
203
204
/****************************************************************************/
205
build
buildd
sumo-0.16.0~dfsg
src
utils
common
RGBColor.cpp
Generated on Tue Apr 16 2013 01:32:20 for SUMO - Simulation of Urban MObility by
1.8.3.1