[vtkusers] Simple color mapping question

David Gobbi david.gobbi at gmail.com
Fri Mar 12 14:56:02 EST 2010


Hi Mike,

Here is how VTK does color mapping:

 double index = (v - TableRange[0])*NumberOfColors/(TableRange[1] -
TableRange[0]);
 if (index < 0) { index = 0; }
 if (index >= NumberOfColors) { index = NumberOfColors-1; }
 return &Table[4*static_cast<int>(index)];

In order to achieve a precise color mapping, you have to work
backwards through the mathematics to see exactly what range of values
a particular color will map to.

In your case, you will want to start with a lookup table with exactly
5 colors.  The vtkLookupTable class is old and quirky, and to build a
custom lookup table, you have to do this:

table->SetNumberOfColors(5);
table->Build();
table->SetTableValue(0, 1.0, 0.0, 0.0, 1.0);
table->SetTableValue(1, 1.0, 0.0, 1.0, 1.0);
table->SetTableValue(2, 1.0, 1.0, 0.0, 1.0);
table->SetTableValue(3, 0.0, 0.0, 1.0, 1.0);
table->SetTableValue(4, 0.0, 1.0, 0.0, 1.0);

Then you'll have to set the TableRange that will give the precise
ranges that you desire.  I'll leave that as an exercise for the
reader.  Hint: a bit of algebra will be required.

   David


On Fri, Mar 12, 2010 at 12:29 PM, Johnson, Mike (IS)
<michael.l.johnson at ngc.com> wrote:
> Hi all,
>
> I have requirements to show a simple 2D surface plot where the data point
> values range, let’s say, from 0 to 100 (or might be -100 to 200 or
> whatever…) Then let’s say I want to color like this:
>
> < 20: Red
>
> 20-40: Magenta
>
> 40-60: Yellow
>
> 60-80: Blue
>
>> 80: Green
>
> Right now I have set up a vtkLookupTable and assigned 20 to red, 40 to
> magenta, 60 to yellow, and 80 to blue.  A few things –
>
> 1.      By default it appears to map as a “ramp” between the colors; ie, if
> the value is 50, it’s “magenta-yellowish.”  I’d rather just have only the 5
> colors in my map. Is this possible?
>
> 2.      Since the colors from my #1 above really specify a “range” instead
> of a “single point value” (ie, I’d like anything from 20-40 to be the same
> color), how do I specify a “less than” (for the “red” values) and a “greater
> than” (for the green values)?
>
> Thanks in advance,
> Mike Johnson
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at:
> http://www.vtk.org/Wiki/VTK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers
>
>



More information about the vtkusers mailing list