3131
3232using namespace SCIRun ::Core::Datatypes;
3333
34- ColorMap::ColorMap (const std::string& name, const size_t resolution, const double shift)
35- : name_(name), resolution_(resolution), shift_(shift) {}
34+ ColorMap::ColorMap (const std::string& name, const size_t resolution, const double shift, const bool invert)
35+ : name_(name), resolution_(resolution), shift_(shift), invert_(invert) {
36+ if (!(name_ == " Rainbow" ||
37+ name_ == " Blackbody" ||
38+ name_ == " Grayscale" ))
39+ throw InvalidArgumentException ();
40+ }
3641
3742ColorMap* ColorMap::clone () const
3843{
39- return new ColorMap (name_);
44+ return new ColorMap (name_,resolution_,shift_,invert_ );
4045}
4146
42- ColorMapHandle StandardColorMapFactory::create (const std::string& name)
47+ ColorMapHandle StandardColorMapFactory::create (const std::string& name, const size_t &resolution,
48+ const double &shift, const bool &invert)
4349{
44- if (name == " Rainbow" )
45- return ColorMapHandle (rainbow_.clone ());
46- if (name == " Grayscale" )
47- return ColorMapHandle (grayscale_.clone ());
48- if (name == " Blackbody" )
49- return ColorMapHandle (blackbody_.clone ());
50- THROW_INVALID_ARGUMENT (" Unknown standard colormap name: " + name);
50+ cm_ = ColorMap (name,resolution,shift,invert);
51+ return ColorMapHandle (cm_.clone ());
5152}
5253
5354float ColorMap::Hue_2_RGB (float v1, float v2, float vH) {
@@ -74,31 +75,52 @@ ColorRGB ColorMap::hslToRGB(float h, float s, float l) {
7475 g = Hue_2_RGB ( var_1, var_2, h );
7576 b = Hue_2_RGB ( var_1, var_2, h - ( .333333 ) );
7677 }
77- return ColorRGB (r,g,b);
78+ return ColorRGB (std::min (std::max (r,0 .f ),1 .f ),
79+ std::min (std::max (g,0 .f ),1 .f ),
80+ std::min (std::max (b,0 .f ),1 .f ));
7881}
7982
80- ColorRGB ColorMap::getColorMapVal (float v) {
83+
84+ float ColorMap::getTransformedColor (float f) {
8185 // @todo this will not be needed with rescale color map.
82- v = std::min (std::max (0 .f ,v),1 .f );
83- float shift = (static_cast <float >(shift_)+1 .f )/2 .f ;
84- v = (v + shift) / (shift + 0 .5f );
86+ float v = std::min (std::max (0 .f ,f),1 .f );
87+ double shift = shift_;
88+ if (invert_) {
89+ v = 1 .f - v;
90+ shift *= -1 .;
91+ }
8592 // apply the resolution
86- v = static_cast <double >((static_cast <int >(v * static_cast <float >(resolution_)))) / static_cast <double >(resolution_ - 1 );
93+ v = static_cast <double >((static_cast <int >(v *
94+ static_cast <float >(resolution_)))) /
95+ static_cast <double >(resolution_ - 1 );
96+ // the shift is a gamma.
97+ float denom = std::tan (M_PI_2 * ( 0 .5f - std::min (std::max (shift,-0.99 ),0.99 ) * 0 .5f ));
98+ // make sure we don't hit divide by zero
99+ if (std::isnan (denom)) denom = 0 .f ;
100+ denom = std::max (denom, 0 .001f );
101+ v = std::pow (v,(1 .f /denom));
102+ return v;
103+ }
104+
105+ ColorRGB ColorMap::getColorMapVal (float v) {
106+ float f = getTransformedColor (v);
107+ // now grab the RGB
87108 ColorRGB col;
88- if (name_ == " Rainbow" )
89- col = hslToRGB ((1.0 -v) * 0.8 , 0.95 , 0.5 );
90- else if (name_ == " Blackbody" ) {
91- if (v < 0.333333 )
92- col = ColorRGB (v * 3 ., 0 ., 0 .);
93- else if (v < 0.6666667 )
94- col = ColorRGB (1 .,(v - 0.333333 ) * 3 ., 0 .);
109+ if (name_ == " Rainbow" ) {
110+ // spread out the thin colors
111+ // if (v < 0.7 && v > 0.3)
112+ // v = v - 0.05f * std::sin((v - 0.3) * 6.f * M_PI / 0.8);
113+ col = hslToRGB ((1 . - f) * 0.7 , 0.95 , 0.5 );
114+ } else if (name_ == " Blackbody" ) {
115+ if (f < 0.333333 )
116+ col = ColorRGB (std::min (std::max (f * 3 .,0 .),1 .), 0 ., 0 .);
117+ else if (f < 0.6666666 )
118+ col = ColorRGB (1 .,std::min (std::max ((f - 0.333333 ) * 3 .,0 .),1 .), 0 .);
95119 else
96- col = ColorRGB (1 ., 1 ., (v - 0.6666667 ) * 3 .);
120+ col = ColorRGB (1 ., 1 ., std::min ( std::max ((f - 0.6666666 ) * 3 ., 0 .), 1 .) );
97121 } else if (name_ == " Grayscale" )
98- col = hslToRGB (0 ., 0 ., v );
122+ col = hslToRGB (0 ., 0 ., f );
99123 return col;
100124}
101125
102- ColorMap StandardColorMapFactory::rainbow_ (" Rainbow" );
103- ColorMap StandardColorMapFactory::grayscale_ (" Grayscale" );
104- ColorMap StandardColorMapFactory::blackbody_ (" Blackbody" );
126+ ColorMap StandardColorMapFactory::cm_ (" Rainbow" );
0 commit comments