template<typename PValue, int PDefaultPreset = CMAP_CUSTOM, int PDefaultFirstColor = -1, int PDefaultLastColor = -1>
class DGtal::GradientColorMap< PValue, PDefaultPreset, PDefaultFirstColor, PDefaultLastColor >
Aim: This class template may be used to (linearly) convert scalar values in a given range into a color in a gradient defined by two or more colors.
Description of template class 'GradientColorMap'
The GradientColorMap can be used either as a functor object (the value range is given at the object's construction, together with the reference color) which converts a value into a Color structure, or it can be used through a static method taking both the range and the value as parameters.
The code below shows a possible use of this class.
#include "Color.h"
#include "GradientColorMap.h"
{
Board b;
b.setPenColor( gradient( 230.0 ) );
GradientColorMap<int> grad3( 0, 500 );
b.setPenColor( grad3( 100 ) );
b.setPenColor( grad3( 300 ) );
}
- Template Parameters:
-
Value | The type of the range values. |
PDefaultPreset | The default gradient preset (e.g. CMAP_GRAYSCALE, CMAP_HOT, or CMAP_CUSTOM |
PDefaultFirstColor | If DefaultPreset is CMAP_CUSTOM, this is the starting color of the gradient. |
PDefaultLastColor | If DefaultPreset is CMAP_CUSTOM, this is the ending color of the gradient. |
- Examples:
- io/boards/dgtalBoard2D-4-colormaps.cpp, topology/3dKSSurfaceExtraction.cpp, topology/ctopo-2-3d.cpp, and topology/ctopo-2.cpp.
Definition at line 118 of file GradientColorMap.h.
template<typename PValue, int PDefaultPreset, int PDefaultFirstColor, int PDefaultLastColor>
DGtal::GradientColorMap< PValue, PDefaultPreset, PDefaultFirstColor, PDefaultLastColor >::GradientColorMap |
( |
const PValue & |
min, |
|
|
const PValue & |
max, |
|
|
const ColorGradientPreset |
preset = static_cast<ColorGradientPreset>( PDefaultPreset ) , |
|
|
const Color |
firstColor = ( PDefaultFirstColor == -1 ) ? Color::None : Color( DGTAL_RED_COMPONENT( PDefaultFirstColor ), DGTAL_GREEN_COMPONENT( PDefaultFirstColor ), DGTAL_BLUE_COMPONENT( PDefaultFirstColor ) ) , |
|
|
const Color |
lastColor = ( PDefaultFirstColor == -1 ) ? Color::None : Color( DGTAL_RED_COMPONENT( PDefaultLastColor ), DGTAL_GREEN_COMPONENT( PDefaultLastColor ), DGTAL_BLUE_COMPONENT( PDefaultLastColor ) ) |
|
) |
| |
|
inline |
Constructor.
- Parameters:
-
min | The lower bound of the value range. |
max | The upper bound of the value range. |
preset | A preset identifier. |
firstColor | The "left" color of the gradient if preset is CMAP_CUSTOM. |
lastColor | The "right" color of the gradient if preset is CMAP_CUSTOM. |
Definition at line 47 of file GradientColorMap.ih.
References DGtal::CMAP_AUTUMN, DGtal::CMAP_COOL, DGtal::CMAP_COPPER, DGtal::CMAP_CUSTOM, DGtal::CMAP_GRAYSCALE, DGtal::CMAP_HOT, DGtal::CMAP_JET, DGtal::CMAP_SPRING, DGtal::CMAP_SUMMER, and DGtal::CMAP_WINTER.
{
switch ( preset ) {
break;
myColors.push_back( Color( 255, 0, 255 ));
myColors.push_back( Color( 255, 255, 0 ));
break;
myColors.push_back( Color( 0, 132, 100 ));
myColors.push_back( Color( 255, 255, 100 ));
break;
myColors.push_back( Color( 255, 0, 0 ));
myColors.push_back( Color( 255, 255, 0));
break;
myColors.push_back( Color( 0, 0, 255 ));
myColors.push_back( Color( 0, 255, 132 ));
break;
myColors.push_back( Color( 0, 255, 255 ));
myColors.push_back( Color( 255, 0, 255 ));
break;
myColors.push_back( Color( 255, 198, 123 ));
break;
myColors.push_back( Color( 255, 140, 0 ) );
break;
myColors.push_back( Color( 255, 140, 0 ) );
myColors.push_back( Color( 132, 0, 0 ) );
break;
}
break;
}
}
template<typename PValue, int PDefaultPreset, int PDefaultFirstColor, int PDefaultLastColor>
Color DGtal::GradientColorMap< PValue, PDefaultPreset, PDefaultFirstColor, PDefaultLastColor >::getColor |
( |
const std::vector< Color > & |
colors, |
|
|
const PValue & |
min, |
|
|
const PValue & |
max, |
|
|
const PValue & |
value |
|
) |
| |
|
inlinestatic |
Computes the color associated with a value in a given range.
- Parameters:
-
colors | The gradients boundary colors. |
min | The lower bound of the value range. |
max | The upper bound of the value range. |
value | A value within the value range. |
- Returns:
- A color whose color linearly depends on the position of [value] within the range [min]..[max].
Definition at line 257 of file GradientColorMap.ih.
References DGtal::Color::blue(), DGtal::Color::green(), and DGtal::Color::red().
{
if ( colors.size() < 2 )
double scale =
static_cast<double>( value -
min ) / (
max -
min);
const int intervals = (const int)colors.size() - 1;
int upper_index = static_cast<int>( ceil( intervals * scale ) );
if ( !upper_index )
upper_index = 1;
const Color & firstColor = colors[ upper_index - 1 ];
const Color & lastColor = colors[ upper_index ];
scale = ( scale * intervals ) - (upper_index - 1);
const unsigned char red = static_cast<unsigned char>( firstColor.red() +
scale * ( lastColor.red() -
firstColor.red() ));
const unsigned char green = static_cast<unsigned char>( firstColor.green() +
scale * ( lastColor.green() -
firstColor.green() ));
const unsigned char blue = static_cast<unsigned char>( firstColor.blue() +
scale * ( lastColor.blue() -
firstColor.blue() ));
return Color( red, green, blue );
}
template<typename PValue, int PDefaultPreset = CMAP_CUSTOM, int PDefaultFirstColor = -1, int PDefaultLastColor = -1>
GradientColorMap< PValue, PDefaultPreset, PDefaultFirstColor, PDefaultLastColor > & GradientColorMap::operator= |
( |
const GradientColorMap< PValue, PDefaultPreset, PDefaultFirstColor, PDefaultLastColor > & |
other | ) |
|
Assignment.
- Parameters:
-
- Returns:
- a reference on 'this'.
Definition at line 134 of file GradientColorMap.ih.
References DGtal::GradientColorMap< PValue, PDefaultPreset, PDefaultFirstColor, PDefaultLastColor >::myColors, DGtal::GradientColorMap< PValue, PDefaultPreset, PDefaultFirstColor, PDefaultLastColor >::myMax, and DGtal::GradientColorMap< PValue, PDefaultPreset, PDefaultFirstColor, PDefaultLastColor >::myMin.
{
if ( &other != this ) {
}
return *this;
}