PhoenixPNG  0.1.0
Set of tools to ease use of png file
Loading...
Searching...
No Matches
PColorMap.cpp
Go to the documentation of this file.
1/***************************************
2 Auteur : Pierre Aubert
3 Mail : pierre.aubert@lapp.in2p3.fr
4 Licence : CeCILL-C
5****************************************/
6
7#include <stdlib.h>
8#include "PColorMap.h"
9
11
15PColorValue createColorValue(float value, const PString & color){
16 size_t nbChannel(color.size());
17 color_t red(0), green(0), blue(0), alpha(0);
18 if(nbChannel >= 2lu){
19 PString redHex(color.substr(0lu, 2lu));
20 red = (color_t)strtol(redHex.c_str(), NULL, 16);
21 }
22 if(nbChannel >= 4lu){
23 PString greenHex(color.substr(2lu, 2lu));
24 green = (color_t)strtol(greenHex.c_str(), NULL, 16);
25 }
26 if(nbChannel >= 6lu){
27 PString blueHex(color.substr(4lu, 2lu));
28 blue = (color_t)strtol(blueHex.c_str(), NULL, 16);
29 }
30 if(nbChannel >= 8lu){
31 PString alphaHex(color.substr(6lu, 2lu));
32 alpha = (color_t)strtol(alphaHex.c_str(), NULL, 16);
33 }
34 return createColorValue(value, red, green, blue, alpha);
35}
36
38
45PColorValue createColorValue(float value, color_t red, color_t green, color_t blue, color_t alpha){
46 PColorValue colorValue;
47 colorValue.value = value;
48 colorValue.r = red;
49 colorValue.g = green;
50 colorValue.b = blue;
51 colorValue.a = alpha;
52 return colorValue;
53}
54
56
62void getColorValue(color_t & red, color_t & green, color_t & blue, color_t & alpha, const PColorValue & color){
63 red = color.r;
64 green = color.g;
65 blue = color.b;
66 alpha = color.a;
67}
68
70
75void phoenix_interpolateColor(PColorValue & output, const PColorValue & colorMin, const PColorValue & colorMax, float value){
76 float ratio((value - colorMin.value)/(colorMax.value - colorMin.value));
77
78 output.r = colorMax.r*ratio + colorMin.r*(1.0f - ratio);
79 output.g = colorMax.g*ratio + colorMin.g*(1.0f - ratio);
80 output.b = colorMax.b*ratio + colorMin.b*(1.0f - ratio);
81 output.a = colorMax.a*ratio + colorMin.a*(1.0f - ratio);
82}
83
85
90PColorValue phoenix_interpolateColor(const PColorValue & colorMin, const PColorValue & colorMax, float value){
91 PColorValue out;
92 phoenix_interpolateColor(out, colorMin, colorMax, value);
93 return out;
94}
95
100
102
105 copyPColorMap(other);
106}
107
112
114
118 copyPColorMap(other);
119 return *this;
120}
121
123
126void PColorMap::addColor(float value, const PString & color){
127 PColorValue colorValue = createColorValue(value, color);
128 p_mapColor[value] = colorValue;
129}
130
132
138void PColorMap::addColor(float value, color_t red, color_t green, color_t blue, color_t alpha){
139 PColorValue colorValue = createColorValue(value, red, green, blue, alpha);
140 p_mapColor[value] = colorValue;
141}
142
144
149void PColorMap::interpolate(color_t & red, color_t & green, color_t & blue, float value) const{
150 PColorValue out;
151 interpolate(out, value);
152 red = out.r;
153 green = out.g;
154 blue = out.b;
155}
156
158
164void PColorMap::interpolate(color_t & red, color_t & green, color_t & blue, color_t & alpha, float value) const{
165 PColorValue out;
166 interpolate(out, value);
167 red = out.r;
168 green = out.g;
169 blue = out.b;
170 alpha = out.a;
171}
172
174
177void PColorMap::interpolate(PColorValue & output, float value) const{
178 if(p_mapColor.size() == 0lu){return;} //No color
179 PMapColorValue::const_iterator itMin(p_mapColor.begin());
180 if(value <= itMin->second.value){ //If the value is smaller than the first colored one
181 output = itMin->second; //We copy this value
182 return;
183 }
184 PMapColorValue::const_reverse_iterator rit(p_mapColor.rbegin());
185 if(value >= rit->second.value){ //If the value is greater than the last colored one
186 output = rit->second; //We copy this value
187 return;
188 }
189 PMapColorValue::const_iterator itMax(itMin);
190 ++itMax;
191 while(itMax != p_mapColor.end()){
192 if(value > itMin->second.value && value < itMax->second.value){
193 phoenix_interpolateColor(output, itMin->second, itMax->second, value);
194 break;
195 }
196 ++itMax;
197 ++itMin;
198 }
199}
200
202
205 p_mapColor = other.p_mapColor;
206}
207
212
213
214
215
216
void getColorValue(color_t &red, color_t &green, color_t &blue, color_t &alpha, const PColorValue &color)
Get the rgba values from PColorValue.
Definition PColorMap.cpp:62
void phoenix_interpolateColor(PColorValue &output, const PColorValue &colorMin, const PColorValue &colorMax, float value)
Interpolate a color with a value by respect to min an max color.
Definition PColorMap.cpp:75
PColorValue createColorValue(float value, const PString &color)
Create a PColorValue.
Definition PColorMap.cpp:15
unsigned char color_t
Type des of a color.
Definition PColorMap.h:14
void phoenix_interpolateColor(PColorValue &output, const PColorValue &colorMin, const PColorValue &colorMax, float value)
Interpolate a color with a value by respect to min an max color.
Definition PColorMap.cpp:75
PColorValue createColorValue(float value, const PString &color)
Create a PColorValue.
Definition PColorMap.cpp:15
PMapColorValue p_mapColor
Vector of the value.
Definition PColorMap.h:58
void addColor(float value, const PString &color)
Add a color in the PColorMap.
PColorMap()
Default constructor of PColorMap.
Definition PColorMap.cpp:97
void copyPColorMap(const PColorMap &other)
Copy function of PColorMap.
PColorMap & operator=(const PColorMap &other)
Definition of equal operator of PColorMap.
void initialisationPColorMap()
Initialisation function of the class PColorMap.
void interpolate(color_t &red, color_t &green, color_t &blue, float value) const
Interpolate color by respect to the given value.
virtual ~PColorMap()
Destructor of PColorMap.
Color related to a value.
Definition PColorMap.h:17
color_t r
Red channel.
Definition PColorMap.h:21
color_t g
Green channel.
Definition PColorMap.h:23
float value
Value of the color.
Definition PColorMap.h:19
color_t a
Alpha channel.
Definition PColorMap.h:27
color_t b
Blue channel.
Definition PColorMap.h:25