Added support for a single color in a ColorRange

This commit is contained in:
Robert Osfield 2004-06-03 10:10:41 +00:00
parent 1526281793
commit 5d97f7ec92
2 changed files with 26 additions and 19 deletions

View File

@ -32,12 +32,18 @@ class OSGSIM_EXPORT ColorRange: public ScalarsToColors
{
public:
/** Constructor for a ColorRange with a default list of colors set to Red-Yellow-Green-Blue-Cyan
@param min minimum scalar value
@param max maximum scalar value
*/
ColorRange(float min, float max);
/** Constructor for a ColorRange
@param min minimum scalar value
@param max maximum scalar value
@param colors optional range of colors, defaulting to Red-Yellow-Green-Blue-Cyan
@param colors optional range of colors,
*/
ColorRange(float min, float max, const std::vector<osg::Vec4>& colors = std::vector<osg::Vec4>());
ColorRange(float min, float max, const std::vector<osg::Vec4>& colors);
/** Set the range of colors. */
void setColors(const std::vector<osg::Vec4>& colors);

View File

@ -2,6 +2,16 @@
using namespace osgSim;
ColorRange::ColorRange(float min, float max): ScalarsToColors(min,max)
{
// Default to something sensible
_colors.push_back(osg::Vec4(1.0,0.0,0.0,1.0)); // R
_colors.push_back(osg::Vec4(1.0,1.0,0.0,1.0)); // Y
_colors.push_back(osg::Vec4(0.0,1.0,0.0,1.0)); // G
_colors.push_back(osg::Vec4(0.0,1.0,1.0,1.0)); // C
_colors.push_back(osg::Vec4(0.0,0.0,1.0,1.0)); // B
}
ColorRange::ColorRange(float min, float max, const std::vector<osg::Vec4>& colors): ScalarsToColors(min,max)
{
setColors(colors);
@ -9,30 +19,21 @@ ColorRange::ColorRange(float min, float max, const std::vector<osg::Vec4>& color
void ColorRange::setColors(const std::vector<osg::Vec4>& colors)
{
if(colors.size()>1)
{
_colors=colors;
}
else
{
// Default to something sensible
_colors.push_back(osg::Vec4(1.0,0.0,0.0,1.0)); // R
_colors.push_back(osg::Vec4(1.0,1.0,0.0,1.0)); // Y
_colors.push_back(osg::Vec4(0.0,1.0,0.0,1.0)); // G
_colors.push_back(osg::Vec4(0.0,1.0,1.0,1.0)); // C
_colors.push_back(osg::Vec4(0.0,0.0,1.0,1.0)); // B
}
_colors=colors;
}
osg::Vec4 ColorRange::getColor(float scalar) const
{
if (_colors.empty()) return osg::Vec4(1.0f,1.0f,1.0f,1.0f);
if (_colors.size()==1) return _colors.front();
if(scalar<getMin()) return _colors.front();
if(scalar>getMax()) return _colors.back();
float r = ((scalar - getMin())/(getMax() - getMin())) * (_colors.size()-1);
float r = ((scalar - getMin())/(getMax() - getMin())) * (_colors.size()-1);
int lower = static_cast<int>(floor(r));
int upper = static_cast<int>(ceil(r));
int upper = static_cast<int>(ceil(r));
osg::Vec4 color = _colors[lower] + ((_colors[upper] - _colors[lower]) * (r-lower));
return color;
osg::Vec4 color = _colors[lower] + ((_colors[upper] - _colors[lower]) * (r-lower));
return color;
}