[vtkusers] Create Blue-White-Red color map lookup table

Hong Yi hongyi at renci.org
Wed Sep 28 12:51:21 EDT 2011


Hi Steve,

Many thanks for sharing your code and adapting it to red/white/blue color mapping. The code makes perfect sense to me and is handy for generating any customized color map look up table. Looks like it is unavoidable to define scalar to color mapping for a customized look up table, though.

Thanks again and best regards,

Hong


From: Steven Chall
Sent: Wednesday, September 28, 2011 12:14 PM
To: Hong Yi
Cc: vtkusers at vtk.org
Subject: [vtkusers] Create Blue-White-Red color map lookup table

On Sep 27, 2011, at 12:49 PM, Hong Yi wrote:

> Hello,
>
> I am wondering whether there is a simple way to create a blue-white-red color map vtkLookupTable other than defining scalar to color mapping through color transfer function. If I set the hue range from 0.667 to 0, a rainbow blue to red colormap will be generated. What I want is not a rainbow colormap, but a blue-white-red color map which varies colors from blue through white to red. Any advice is very much appreciated.
>
> Thanks,
>
> Hong

Hi Hong,
Here's some code I use to set up color mappings. I was using it for another mapping but I dropped in red/white/blue and it worked ok. It's from a class CaseSpaceDialog and there are a few objects that are class members that aren't defined within the code presented here but could be, notably min and max (the min and max values of the data you're color mapping, which need to be available prior to calling these methods), plus vtkScalarBarActor *legend, vtkLookupTable *lookupTable, vtkRenderer *ren, and numColors, which is just the total number of different color gradations you want: I set it to 100 for a reasonably continuous-looking color bar. The second method just calls the first one in a loop to generate the legend. I did some global replaces to generalize the way the code reads, so hopefully nothing went awry. Let me know if something's broken or doesn't make sense.

Steve

-Steve Chall
 Senior Research Software Developer
 Renaissance Computing Institute (RENCI)
 Phone: 919-681-9639
 Email: stevec at renci.org<mailto:stevec at renci.org>

///getColorCorrespondingToValue ////////////////////////////////////////////////
//
// Assumes you've already generated min and max -- the extrema for the data
// to which you're applying the color map. Then define the number of colorNodes
// and make sure there's a row of three double values (representing r, g, and b
// in a 0.0-1.0 range) for each node. Then call this method for with parameter
// val some double value between min and max inclusive. The corresponding rgb
// values will be returned in the reference-to-double parameters r, g, and b.
//
////////////////////////////////////////////////////////////////////////////////
void CaseSpaceDialog::getColorCorrespondingTovalue(double val,
      double &r, double &g, double &b)
{
      static const int numColorNodes = 3;
      double color[numColorNodes][3] =
      {
            0.0, 0.0, 1.0,    // blue
            1.0, 1.0, 1.0,    // white
            1.0, 0.0, 0.0     // red
      };

      for (int i = 0; i < (numColorNodes - 1); i++)
      {
            double currFloor = min + ((double)i / (numColorNodes - 1)) * range;
            double currCeil = min + ((double)(i + 1) / (numColorNodes - 1)) * range;

            if ((val >= currFloor) && (val <= currCeil))
            {
                  double currFraction = (val - currFloor) / (currCeil - currFloor);
                  r = color[i][0] * (1.0 - currFraction) + color[i + 1][0] * currFraction;
                  g = color[i][1] * (1.0 - currFraction) + color[i + 1][1] * currFraction;
                  b = color[i][2] * (1.0 - currFraction) + color[i + 1][2] * currFraction;
            }
      }
}

////initializeLegend////////////////////////////////////////////////////////////
//
// Make sure min and max have been generated before calling this method.
//
// Assumes that pointers legend and lookupTable are set to 0 if not already
// instantiated.
//
////////////////////////////////////////////////////////////////////////////////
void CaseSpaceDialog::initializeLegend()
{
      if (!legend)
      {
            legend = vtkScalarBarActor::New();
            lookupTable = vtkLookupTable::New();
      }

      lookupTable->SetScaleToLinear();
      lookupTable->SetRange(min, max);

      lookupTable->SetNumberOfTableValues(numColors);

      double r, g, b;

      for (int i = 0; i < numColors; i++)
      {
            double val = min + ((double)i / numColors) * range;
            getColorCorrespondingTovalue(val, r, g, b);
            lookupTable->SetTableValue(i, r, g, b);
      }

      lookupTable->Build();

      legend->SetLookupTable(lookupTable);
      legend->SetTitle("Values");
      legend->SetOrientationToHorizontal();
      legend->GetPositionCoordinate()->SetCoordinateSystemToNormalizedViewport();
      legend->GetPositionCoordinate()->SetValue(0.814, 0.94);
      legend->SetWidth(0.1515);
      legend->SetHeight(0.05);
      legend->GetTitleTextProperty()->ItalicOff();
      legend->GetLabelTextProperty()->ItalicOff();
      ren->AddActor2D(legend);
}


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20110928/5fbc3602/attachment.htm>


More information about the vtkusers mailing list