[vtkusers] Create Blue-White-Red color map lookup table
Steven Chall
stevec at renci.org
Wed Sep 28 12:14:20 EDT 2011
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/4a8e6723/attachment.htm>
More information about the vtkusers
mailing list