LookupTable

LookupTable

var LookupTable = require('paraviewweb/src/Common/Core/LookupTable'),
instance = new LookupTable('Temperature');

constructor(name)

Create an instance of a LookupTable while giving it a name.
Usually we use the field name that we want to use
that LookupTable for.

getName() : String

Return the name of the given LookupTable.

getPresets() : [ String… ]

Return the names of the available presets.

setPreset(name)

Use a preset to configure the current instance. This will reset all the
control points and use the one defined inside the preset.

This will trigger the following change event:

{
change: 'preset',
lut: lutInstance
}

getScalarRange() : [min, max]

Return the scalar range the LookupTable is using.

setScalarRange(min, max)

Update the scalar range that LookupTable is using.

build(trigger)

This generate a precomputed table for the lookuptable.

If trigger is true, this will trigger the following change event:

{
change: 'controlPoints',
lut: lutInstance
}

setNumberOfColors( numberOfColors )

Set the number of colors to be used for the concrete scalar to color mapping.

This will trigger the following change event:

{
change: 'numberOfColors',
lut: lutInstance
}

getNumberOfControlPoints() : Number

Return the number of control points used to define the color map.

removeControlPoint(idx) : Boolean

Removes the provided control point index and returns true if the index is
valid and the point was removed, the method will otherwise return false.

getControlPoint(idx) : {x, r, g, b}

Return the control point defined at the provided index.

updateControlPoint(idx, xrgb) : Number

Update the control point defined in the given index by replacing it with the
provided object. The control points will be reset and the table rebuilt.

The method will return the index of the updated control point which could be different if the x value is different.

addControlPoint(xrgb) : Number

Add a new control point and return its corresponding index while rebuilding
the full index map.

A control point should be composed of four fields x, r, g and b with a number between 0 and 1.

var xrgb = {
x: 0.5,
r: 1,
g: 0,
b: 0.1
};

drawToCanvas(canvas)

Draw the index map into the canvas using a height of 1 pixel and a width corresponding to the number of colors defined.

getColor(scalar) : [r, g, b]

Return the color that should be used for a given scalar value. If the value is outside the scalar range, then the last color defined for that edge will be used.

onChange(callback) : subscription

Helper method used to attach a listener to the topic that is used when the LookupTable change. We rely on Monologue.js for our observer pattern.

destroy()

Remove any listener attached to this LookupTable instance.

Source

Presets.js
/* eslint-disable no-multi-spaces */
export default {
lookuptables: {
spectralflip: {
controlpoints: [
{
x: 0,
r: 0.3686274509803922,
g: 0.3098039215686275,
b: 0.6352941176470588,
},
{
x: 0.1,
r: 0.196078431372549,
g: 0.5333333333333333,
b: 0.7411764705882353,
},
{ x: 0.2, r: 0.4, g: 0.7607843137254902, b: 0.6470588235294118 },
{
x: 0.3,
r: 0.6705882352941176,
g: 0.8666666666666667,
b: 0.6431372549019608,
},
{
x: 0.4,
r: 0.9019607843137255,
g: 0.9607843137254902,
b: 0.596078431372549,
},
{ x: 0.5, r: 1, g: 1, b: 0.7490196078431373 },
{
x: 0.6,
r: 0.996078431372549,
g: 0.8784313725490196,
b: 0.5450980392156862,
},
{
x: 0.7,
r: 0.9921568627450981,
g: 0.6823529411764706,
b: 0.3803921568627451,
},
{
x: 0.8,
r: 0.9568627450980393,
g: 0.4274509803921568,
b: 0.2627450980392157,
},
{
x: 0.9,
r: 0.8352941176470589,
g: 0.2431372549019608,
b: 0.3098039215686275,
},
{
x: 1,
r: 0.6196078431372549,
g: 0.00392156862745098,
b: 0.2588235294117647,
},
],
range: [0.0, 1.0],
},
cold2warm: {
controlpoints: [
{ x: 0.0, r: 0.23137254902, g: 0.298039215686, b: 0.752941176471 },
{ x: 0.5, r: 0.865, g: 0.865, b: 0.865 },
{ x: 1.0, r: 0.705882352941, g: 0.0156862745098, b: 0.149019607843 },
],
range: [0.0, 1.0],
},
rainbow: {
controlpoints: [
{ x: 0.0, r: 0.0, g: 0.0, b: 1.0 },
{ x: 0.25, r: 0.0, g: 1.0, b: 1.0 },
{ x: 0.5, r: 0.0, g: 1.0, b: 0.0 },
{ x: 0.75, r: 1.0, g: 1.0, b: 0.0 },
{ x: 1.0, r: 1.0, g: 0.0, b: 0.0 },
],
range: [0.0, 1.0],
},
'gray scale': {
controlpoints: [
{ x: 0.0, r: 0.0, g: 0.0, b: 0.0 },
{ x: 1.0, r: 1.0, g: 1.0, b: 1.0 },
],
range: [0.0, 1.0],
},
'gray scale flip': {
controlpoints: [
{ x: 0.0, r: 1.0, g: 1.0, b: 1.0 },
{ x: 1.0, r: 0.0, g: 0.0, b: 0.0 },
],
range: [0.0, 1.0],
},
spectral: {
controlpoints: [
{
x: 0,
r: 0.6196078431372549,
g: 0.00392156862745098,
b: 0.2588235294117647,
},
{
x: 0.1,
r: 0.8352941176470589,
g: 0.2431372549019608,
b: 0.3098039215686275,
},
{
x: 0.2,
r: 0.9568627450980393,
g: 0.4274509803921568,
b: 0.2627450980392157,
},
{
x: 0.3,
r: 0.9921568627450981,
g: 0.6823529411764706,
b: 0.3803921568627451,
},
{
x: 0.4,
r: 0.996078431372549,
g: 0.8784313725490196,
b: 0.5450980392156862,
},
{ x: 0.5, r: 1, g: 1, b: 0.7490196078431373 },
{
x: 0.6,
r: 0.9019607843137255,
g: 0.9607843137254902,
b: 0.596078431372549,
},
{
x: 0.7,
r: 0.6705882352941176,
g: 0.8666666666666667,
b: 0.6431372549019608,
},
{ x: 0.8, r: 0.4, g: 0.7607843137254902, b: 0.6470588235294118 },
{
x: 0.9,
r: 0.196078431372549,
g: 0.5333333333333333,
b: 0.7411764705882353,
},
{
x: 1,
r: 0.3686274509803922,
g: 0.3098039215686275,
b: 0.6352941176470588,
},
],
range: [0.0, 1.0],
},
warm: {
controlpoints: [
{
x: 0.0,
r: 0.4745098039215686,
g: 0.09019607843137255,
b: 0.09019607843137255,
},
{
x: 0.2,
r: 0.7098039215686275,
g: 0.00392156862745098,
b: 0.00392156862745098,
},
{
x: 0.4,
r: 0.9372549019607843,
g: 0.2784313725490196,
b: 0.09803921568627451,
},
{
x: 0.6,
r: 0.9764705882352941,
g: 0.5137254901960784,
b: 0.1411764705882353,
},
{ x: 0.8, r: 1.0, g: 0.7058823529411765, b: 0.0 },
{ x: 1.0, r: 1.0, g: 0.8980392156862745, b: 0.02352941176470588 },
],
range: [0.0, 1.0],
},
cool: {
controlpoints: [
{
x: 0,
r: 0.4588235294117647,
g: 0.6941176470588235,
b: 0.00392156862745098,
},
{
x: 0.1666666666666667,
r: 0.3450980392156863,
g: 0.5019607843137255,
b: 0.1607843137254902,
},
{
x: 0.3333333333333333,
r: 0.3137254901960784,
g: 0.8431372549019608,
b: 0.7490196078431373,
},
{
x: 0.5,
r: 0.1098039215686274,
g: 0.5843137254901961,
b: 0.803921568627451,
},
{
x: 0.6666666666666666,
r: 0.2313725490196079,
g: 0.407843137254902,
b: 0.6705882352941176,
},
{
x: 0.8333333333333334,
r: 0.6039215686274509,
g: 0.407843137254902,
b: 1,
},
{ x: 1, r: 0.3725490196078431, g: 0.2, b: 0.5019607843137255 },
],
range: [0.0, 1.0],
},
blues: {
controlpoints: [
{
x: 0,
r: 0.2313725490196079,
g: 0.407843137254902,
b: 0.6705882352941176,
},
{
x: 0.1666666666666667,
r: 0.1098039215686274,
g: 0.5843137254901961,
b: 0.803921568627451,
},
{
x: 0.3333333333333333,
r: 0.3058823529411765,
g: 0.8509803921568627,
b: 0.9176470588235294,
},
{
x: 0.5,
r: 0.4509803921568628,
g: 0.6039215686274509,
b: 0.8352941176470589,
},
{
x: 0.6666666666666666,
r: 0.2588235294117647,
g: 0.2392156862745098,
b: 0.6627450980392157,
},
{
x: 0.8333333333333334,
r: 0.3137254901960784,
g: 0.3294117647058823,
b: 0.5294117647058824,
},
{
x: 1,
r: 0.06274509803921569,
g: 0.1647058823529412,
b: 0.3215686274509804,
},
],
range: [0.0, 1.0],
},
wildflower: {
controlpoints: [
{
x: 0,
r: 0.1098039215686274,
g: 0.5843137254901961,
b: 0.803921568627451,
},
{
x: 0.1666666666666667,
r: 0.2313725490196079,
g: 0.407843137254902,
b: 0.6705882352941176,
},
{
x: 0.3333333333333333,
r: 0.4,
g: 0.2431372549019608,
b: 0.7176470588235294,
},
{
x: 0.5,
r: 0.6352941176470588,
g: 0.3294117647058823,
b: 0.8117647058823529,
},
{
x: 0.6666666666666666,
r: 0.8705882352941177,
g: 0.3803921568627451,
b: 0.807843137254902,
},
{
x: 0.8333333333333334,
r: 0.8627450980392157,
g: 0.3803921568627451,
b: 0.5843137254901961,
},
{
x: 1,
r: 0.2392156862745098,
g: 0.06274509803921569,
b: 0.3215686274509804,
},
],
range: [0.0, 1.0],
},
citrus: {
controlpoints: [
{
x: 0,
r: 0.396078431372549,
g: 0.4862745098039216,
b: 0.2156862745098039,
},
{
x: 0.2,
r: 0.4588235294117647,
g: 0.6941176470588235,
b: 0.00392156862745098,
},
{
x: 0.4,
r: 0.6980392156862745,
g: 0.7294117647058823,
b: 0.1882352941176471,
},
{ x: 0.6, r: 1, g: 0.8980392156862745, b: 0.02352941176470588 },
{ x: 0.8, r: 1, g: 0.7058823529411765, b: 0 },
{
x: 1,
r: 0.9764705882352941,
g: 0.5137254901960784,
b: 0.1411764705882353,
},
],
range: [0.0, 1.0],
},
organge2purple: {
controlpoints: [
{
x: 0,
r: 0.4980392156862745,
g: 0.2313725490196079,
b: 0.03137254901960784,
},
{
x: 0.1,
r: 0.7019607843137254,
g: 0.3450980392156863,
b: 0.02352941176470588,
},
{
x: 0.2,
r: 0.8784313725490196,
g: 0.5098039215686274,
b: 0.07843137254901961,
},
{
x: 0.3,
r: 0.9921568627450981,
g: 0.7215686274509804,
b: 0.3882352941176471,
},
{
x: 0.4,
r: 0.996078431372549,
g: 0.8784313725490196,
b: 0.7137254901960784,
},
{
x: 0.5,
r: 0.9686274509803922,
g: 0.9686274509803922,
b: 0.9686274509803922,
},
{
x: 0.6,
r: 0.8470588235294118,
g: 0.8549019607843137,
b: 0.9215686274509803,
},
{
x: 0.7,
r: 0.6980392156862745,
g: 0.6705882352941176,
b: 0.8235294117647058,
},
{
x: 0.8,
r: 0.5019607843137255,
g: 0.4509803921568628,
b: 0.6745098039215687,
},
{
x: 0.9,
r: 0.3294117647058823,
g: 0.1529411764705882,
b: 0.5333333333333333,
},
{ x: 1, r: 0.1764705882352941, g: 0, b: 0.2941176470588235 },
],
range: [0.0, 1.0],
},
brown2green: {
controlpoints: [
{
x: 0,
r: 0.3294117647058823,
g: 0.1882352941176471,
b: 0.0196078431372549,
},
{
x: 0.1,
r: 0.5490196078431373,
g: 0.3176470588235294,
b: 0.0392156862745098,
},
{
x: 0.2,
r: 0.7490196078431373,
g: 0.5058823529411764,
b: 0.1764705882352941,
},
{
x: 0.3,
r: 0.8745098039215686,
g: 0.7607843137254902,
b: 0.4901960784313725,
},
{
x: 0.4,
r: 0.9647058823529412,
g: 0.9098039215686274,
b: 0.7647058823529411,
},
{
x: 0.5,
r: 0.9607843137254902,
g: 0.9607843137254902,
b: 0.9607843137254902,
},
{
x: 0.6,
r: 0.7803921568627451,
g: 0.9176470588235294,
b: 0.8980392156862745,
},
{
x: 0.7,
r: 0.5019607843137255,
g: 0.803921568627451,
b: 0.7568627450980392,
},
{
x: 0.8,
r: 0.207843137254902,
g: 0.592156862745098,
b: 0.5607843137254902,
},
{ x: 0.9, r: 0.00392156862745098, g: 0.4, b: 0.3686274509803922 },
{ x: 1, r: 0, g: 0.2352941176470588, b: 0.1882352941176471 },
],
range: [0.0, 1.0],
},
blue2green: {
controlpoints: [
{
x: 0,
r: 0.9686274509803922,
g: 0.9882352941176471,
b: 0.9921568627450981,
},
{
x: 0.125,
r: 0.8980392156862745,
g: 0.9607843137254902,
b: 0.9764705882352941,
},
{ x: 0.25, r: 0.8, g: 0.9254901960784314, b: 0.9019607843137255 },
{ x: 0.375, r: 0.6, g: 0.8470588235294118, b: 0.788235294117647 },
{ x: 0.5, r: 0.4, g: 0.7607843137254902, b: 0.6431372549019608 },
{
x: 0.625,
r: 0.2549019607843137,
g: 0.6823529411764706,
b: 0.4627450980392157,
},
{
x: 0.75,
r: 0.1372549019607843,
g: 0.5450980392156862,
b: 0.2705882352941176,
},
{ x: 0.875, r: 0, g: 0.4274509803921568, b: 0.1725490196078431 },
{ x: 1, r: 0, g: 0.2666666666666667, b: 0.1058823529411765 },
],
range: [0.0, 1.0],
},
yellow2brown: {
controlpoints: [
{ x: 0, r: 1, g: 1, b: 0.8980392156862745 },
{ x: 0.125, r: 1, g: 0.9686274509803922, b: 0.7372549019607844 },
{
x: 0.25,
r: 0.996078431372549,
g: 0.8901960784313725,
b: 0.5686274509803921,
},
{
x: 0.375,
r: 0.996078431372549,
g: 0.7686274509803922,
b: 0.3098039215686275,
},
{ x: 0.5, r: 0.996078431372549, g: 0.6, b: 0.1607843137254902 },
{
x: 0.625,
r: 0.9254901960784314,
g: 0.4392156862745098,
b: 0.07843137254901961,
},
{ x: 0.75, r: 0.8, g: 0.2980392156862745, b: 0.007843137254901961 },
{ x: 0.875, r: 0.6, g: 0.203921568627451, b: 0.01568627450980392 },
{ x: 1, r: 0.4, g: 0.1450980392156863, b: 0.02352941176470588 },
],
range: [0.0, 1.0],
},
blue2purple: {
controlpoints: [
{
x: 0,
r: 0.9686274509803922,
g: 0.9882352941176471,
b: 0.9921568627450981,
},
{
x: 0.125,
r: 0.8784313725490196,
g: 0.9254901960784314,
b: 0.9568627450980393,
},
{
x: 0.25,
r: 0.7490196078431373,
g: 0.8274509803921568,
b: 0.9019607843137255,
},
{
x: 0.375,
r: 0.6196078431372549,
g: 0.7372549019607844,
b: 0.8549019607843137,
},
{
x: 0.5,
r: 0.5490196078431373,
g: 0.5882352941176471,
b: 0.7764705882352941,
},
{
x: 0.625,
r: 0.5490196078431373,
g: 0.4196078431372549,
b: 0.6941176470588235,
},
{
x: 0.75,
r: 0.5333333333333333,
g: 0.2549019607843137,
b: 0.615686274509804,
},
{
x: 0.875,
r: 0.5058823529411764,
g: 0.05882352941176471,
b: 0.4862745098039216,
},
{ x: 1, r: 0.3019607843137255, g: 0, b: 0.2941176470588235 },
],
range: [0.0, 1.0],
},
ocean: {
controlpoints: [
{ x: 0.0, r: 0.039215, g: 0.090195, b: 0.25098 },
{ x: 0.125, r: 0.133333, g: 0.364706, b: 0.521569 },
{ x: 0.25, r: 0.321569, g: 0.760784, b: 0.8 },
{ x: 0.375, r: 0.690196, g: 0.960784, b: 0.894118 },
{ x: 0.5, r: 0.552941, g: 0.921569, b: 0.552941 },
{ x: 0.625, r: 0.329412, g: 0.6, b: 0.239216 },
{ x: 0.75, r: 0.211765, g: 0.34902, b: 0.078435 },
{ x: 0.875, r: 0.011765, g: 0.207843, b: 0.023525 },
{ x: 1.0, r: 0.286275, g: 0.294118, b: 0.301961 },
],
range: [0.0, 1.0],
},
earth: {
controlpoints: [
{ x: 0.0, r: 0.392157, g: 0.392157, b: 0.392157 },
{ x: 0.586175, r: 0.392157, g: 0.392157, b: 0.392157 },
{ x: 0.589041, r: 0.141176, g: 0.345098, b: 0.478431 },
{ x: 0.589042, r: 0.501961, g: 0.694118, b: 0.172549 },
{ x: 0.617699, r: 0.74902, g: 0.560784, b: 0.188235 },
{ x: 0.789648, r: 0.752941, g: 0.741176, b: 0.729412 },
{ x: 0.993079, r: 0.796078, g: 0.780392, b: 0.772549 },
{ x: 1.0, r: 0.796078, g: 0.780392, b: 0.772549 },
],
range: [0.0, 1.0],
},
},
swatches: {
colors: [
{ r: 255, g: 255, b: 255 },
{ r: 204, g: 255, b: 255 },
{ r: 204, g: 204, b: 255 },
{ r: 204, g: 204, b: 255 },
{ r: 204, g: 204, b: 255 },
{ r: 204, g: 204, b: 255 },
{ r: 204, g: 204, b: 255 },
{ r: 204, g: 204, b: 255 },
{ r: 204, g: 204, b: 255 },
{ r: 204, g: 204, b: 255 },
{ r: 204, g: 204, b: 255 },
{ r: 255, g: 204, b: 255 },
{ r: 255, g: 204, b: 204 },
{ r: 255, g: 204, b: 204 },
{ r: 255, g: 204, b: 204 },
{ r: 255, g: 204, b: 204 },
{ r: 255, g: 204, b: 204 },
{ r: 255, g: 204, b: 204 },
{ r: 255, g: 204, b: 204 },
{ r: 255, g: 204, b: 204 },
{ r: 255, g: 204, b: 204 },
{ r: 255, g: 255, b: 204 },
{ r: 204, g: 255, b: 204 },
{ r: 204, g: 255, b: 204 },
{ r: 204, g: 255, b: 204 },
{ r: 204, g: 255, b: 204 },
{ r: 204, g: 255, b: 204 },
{ r: 204, g: 255, b: 204 },
{ r: 204, g: 255, b: 204 },
{ r: 204, g: 255, b: 204 },
{ r: 204, g: 255, b: 204 },
{ r: 204, g: 204, b: 204 },
{ r: 153, g: 255, b: 255 },
{ r: 153, g: 204, b: 255 },
{ r: 153, g: 153, b: 255 },
{ r: 153, g: 153, b: 255 },
{ r: 153, g: 153, b: 255 },
{ r: 153, g: 153, b: 255 },
{ r: 153, g: 153, b: 255 },
{ r: 153, g: 153, b: 255 },
{ r: 153, g: 153, b: 255 },
{ r: 204, g: 153, b: 255 },
{ r: 255, g: 153, b: 255 },
{ r: 255, g: 153, b: 204 },
{ r: 255, g: 153, b: 153 },
{ r: 255, g: 153, b: 153 },
{ r: 255, g: 153, b: 153 },
{ r: 255, g: 153, b: 153 },
{ r: 255, g: 153, b: 153 },
{ r: 255, g: 153, b: 153 },
{ r: 255, g: 153, b: 153 },
{ r: 255, g: 204, b: 153 },
{ r: 255, g: 255, b: 153 },
{ r: 204, g: 255, b: 153 },
{ r: 153, g: 255, b: 153 },
{ r: 153, g: 255, b: 153 },
{ r: 153, g: 255, b: 153 },
{ r: 153, g: 255, b: 153 },
{ r: 153, g: 255, b: 153 },
{ r: 153, g: 255, b: 153 },
{ r: 153, g: 255, b: 153 },
{ r: 153, g: 255, b: 204 },
{ r: 204, g: 204, b: 204 },
{ r: 102, g: 255, b: 255 },
{ r: 102, g: 204, b: 255 },
{ r: 102, g: 153, b: 255 },
{ r: 102, g: 102, b: 255 },
{ r: 102, g: 102, b: 255 },
{ r: 102, g: 102, b: 255 },
{ r: 102, g: 102, b: 255 },
{ r: 102, g: 102, b: 255 },
{ r: 153, g: 102, b: 255 },
{ r: 204, g: 102, b: 255 },
{ r: 255, g: 102, b: 255 },
{ r: 255, g: 102, b: 204 },
{ r: 255, g: 102, b: 153 },
{ r: 255, g: 102, b: 102 },
{ r: 255, g: 102, b: 102 },
{ r: 255, g: 102, b: 102 },
{ r: 255, g: 102, b: 102 },
{ r: 255, g: 102, b: 102 },
{ r: 255, g: 153, b: 102 },
{ r: 255, g: 204, b: 102 },
{ r: 255, g: 255, b: 102 },
{ r: 204, g: 255, b: 102 },
{ r: 153, g: 255, b: 102 },
{ r: 102, g: 255, b: 102 },
{ r: 102, g: 255, b: 102 },
{ r: 102, g: 255, b: 102 },
{ r: 102, g: 255, b: 102 },
{ r: 102, g: 255, b: 102 },
{ r: 102, g: 255, b: 153 },
{ r: 102, g: 255, b: 204 },
{ r: 153, g: 153, b: 153 },
{ r: 51, g: 255, b: 255 },
{ r: 51, g: 204, b: 255 },
{ r: 51, g: 153, b: 255 },
{ r: 51, g: 102, b: 255 },
{ r: 51, g: 51, b: 255 },
{ r: 51, g: 51, b: 255 },
{ r: 51, g: 51, b: 255 },
{ r: 102, g: 51, b: 255 },
{ r: 153, g: 51, b: 255 },
{ r: 204, g: 51, b: 255 },
{ r: 255, g: 51, b: 255 },
{ r: 255, g: 51, b: 204 },
{ r: 255, g: 51, b: 153 },
{ r: 255, g: 51, b: 102 },
{ r: 255, g: 51, b: 51 },
{ r: 255, g: 51, b: 51 },
{ r: 255, g: 51, b: 51 },
{ r: 255, g: 102, b: 51 },
{ r: 255, g: 153, b: 51 },
{ r: 255, g: 204, b: 51 },
{ r: 255, g: 255, b: 51 },
{ r: 204, g: 255, b: 51 },
{ r: 153, g: 255, b: 51 },
{ r: 102, g: 255, b: 51 },
{ r: 51, g: 255, b: 51 },
{ r: 51, g: 255, b: 51 },
{ r: 51, g: 255, b: 51 },
{ r: 51, g: 255, b: 102 },
{ r: 51, g: 255, b: 153 },
{ r: 51, g: 255, b: 204 },
{ r: 153, g: 153, b: 153 },
{ r: 0, g: 255, b: 255 },
{ r: 0, g: 204, b: 255 },
{ r: 0, g: 153, b: 255 },
{ r: 0, g: 102, b: 255 },
{ r: 0, g: 51, b: 255 },
{ r: 0, g: 0, b: 255 },
{ r: 51, g: 0, b: 255 },
{ r: 102, g: 0, b: 255 },
{ r: 153, g: 0, b: 255 },
{ r: 204, g: 0, b: 255 },
{ r: 255, g: 0, b: 255 },
{ r: 255, g: 0, b: 204 },
{ r: 255, g: 0, b: 153 },
{ r: 255, g: 0, b: 102 },
{ r: 255, g: 0, b: 51 },
{ r: 255, g: 0, b: 0 },
{ r: 255, g: 51, b: 0 },
{ r: 255, g: 102, b: 0 },
{ r: 255, g: 153, b: 0 },
{ r: 255, g: 204, b: 0 },
{ r: 255, g: 255, b: 0 },
{ r: 204, g: 255, b: 0 },
{ r: 153, g: 255, b: 0 },
{ r: 102, g: 255, b: 0 },
{ r: 51, g: 255, b: 0 },
{ r: 0, g: 255, b: 0 },
{ r: 0, g: 255, b: 51 },
{ r: 0, g: 255, b: 102 },
{ r: 0, g: 255, b: 153 },
{ r: 0, g: 255, b: 204 },
{ r: 102, g: 102, b: 102 },
{ r: 0, g: 204, b: 204 },
{ r: 0, g: 204, b: 204 },
{ r: 0, g: 153, b: 204 },
{ r: 0, g: 102, b: 204 },
{ r: 0, g: 51, b: 204 },
{ r: 0, g: 0, b: 204 },
{ r: 51, g: 0, b: 204 },
{ r: 102, g: 0, b: 204 },
{ r: 153, g: 0, b: 204 },
{ r: 204, g: 0, b: 204 },
{ r: 204, g: 0, b: 204 },
{ r: 204, g: 0, b: 204 },
{ r: 204, g: 0, b: 153 },
{ r: 204, g: 0, b: 102 },
{ r: 204, g: 0, b: 51 },
{ r: 204, g: 0, b: 0 },
{ r: 204, g: 51, b: 0 },
{ r: 204, g: 102, b: 0 },
{ r: 204, g: 153, b: 0 },
{ r: 204, g: 204, b: 0 },
{ r: 204, g: 204, b: 0 },
{ r: 204, g: 204, b: 0 },
{ r: 153, g: 204, b: 0 },
{ r: 102, g: 204, b: 0 },
{ r: 51, g: 204, b: 0 },
{ r: 0, g: 204, b: 0 },
{ r: 0, g: 204, b: 51 },
{ r: 0, g: 204, b: 102 },
{ r: 0, g: 204, b: 153 },
{ r: 0, g: 204, b: 204 },
{ r: 102, g: 102, b: 102 },
{ r: 0, g: 153, b: 153 },
{ r: 0, g: 153, b: 153 },
{ r: 0, g: 153, b: 153 },
{ r: 0, g: 102, b: 153 },
{ r: 0, g: 51, b: 153 },
{ r: 0, g: 0, b: 153 },
{ r: 51, g: 0, b: 153 },
{ r: 102, g: 0, b: 153 },
{ r: 153, g: 0, b: 153 },
{ r: 153, g: 0, b: 153 },
{ r: 153, g: 0, b: 153 },
{ r: 153, g: 0, b: 153 },
{ r: 153, g: 0, b: 153 },
{ r: 153, g: 0, b: 102 },
{ r: 153, g: 0, b: 51 },
{ r: 153, g: 0, b: 0 },
{ r: 153, g: 51, b: 0 },
{ r: 153, g: 102, b: 0 },
{ r: 153, g: 153, b: 0 },
{ r: 153, g: 153, b: 0 },
{ r: 153, g: 153, b: 0 },
{ r: 153, g: 153, b: 0 },
{ r: 153, g: 153, b: 0 },
{ r: 102, g: 153, b: 0 },
{ r: 51, g: 153, b: 0 },
{ r: 0, g: 153, b: 0 },
{ r: 0, g: 153, b: 51 },
{ r: 0, g: 153, b: 102 },
{ r: 0, g: 153, b: 153 },
{ r: 0, g: 153, b: 153 },
{ r: 51, g: 51, b: 51 },
{ r: 0, g: 102, b: 102 },
{ r: 0, g: 102, b: 102 },
{ r: 0, g: 102, b: 102 },
{ r: 0, g: 102, b: 102 },
{ r: 0, g: 51, b: 102 },
{ r: 0, g: 0, b: 102 },
{ r: 51, g: 0, b: 102 },
{ r: 102, g: 0, b: 102 },
{ r: 102, g: 0, b: 102 },
{ r: 102, g: 0, b: 102 },
{ r: 102, g: 0, b: 102 },
{ r: 102, g: 0, b: 102 },
{ r: 102, g: 0, b: 102 },
{ r: 102, g: 0, b: 102 },
{ r: 102, g: 0, b: 51 },
{ r: 102, g: 0, b: 0 },
{ r: 102, g: 51, b: 0 },
{ r: 102, g: 102, b: 0 },
{ r: 102, g: 102, b: 0 },
{ r: 102, g: 102, b: 0 },
{ r: 102, g: 102, b: 0 },
{ r: 102, g: 102, b: 0 },
{ r: 102, g: 102, b: 0 },
{ r: 102, g: 102, b: 0 },
{ r: 51, g: 102, b: 0 },
{ r: 0, g: 102, b: 0 },
{ r: 0, g: 102, b: 51 },
{ r: 0, g: 102, b: 102 },
{ r: 0, g: 102, b: 102 },
{ r: 0, g: 102, b: 102 },
{ r: 0, g: 0, b: 0 },
{ r: 0, g: 51, b: 51 },
{ r: 0, g: 51, b: 51 },
{ r: 0, g: 51, b: 51 },
{ r: 0, g: 51, b: 51 },
{ r: 0, g: 51, b: 51 },
{ r: 0, g: 0, b: 51 },
{ r: 51, g: 0, b: 51 },
{ r: 51, g: 0, b: 51 },
{ r: 51, g: 0, b: 51 },
{ r: 51, g: 0, b: 51 },
{ r: 51, g: 0, b: 51 },
{ r: 51, g: 0, b: 51 },
{ r: 51, g: 0, b: 51 },
{ r: 51, g: 0, b: 51 },
{ r: 51, g: 0, b: 51 },
{ r: 51, g: 0, b: 0 },
{ r: 51, g: 51, b: 0 },
{ r: 51, g: 51, b: 0 },
{ r: 51, g: 51, b: 0 },
{ r: 51, g: 51, b: 0 },
{ r: 51, g: 51, b: 0 },
{ r: 51, g: 51, b: 0 },
{ r: 51, g: 51, b: 0 },
{ r: 51, g: 51, b: 0 },
{ r: 0, g: 51, b: 0 },
{ r: 0, g: 51, b: 51 },
{ r: 0, g: 51, b: 51 },
{ r: 0, g: 51, b: 51 },
{ r: 0, g: 51, b: 51 },
{ r: 51, g: 51, b: 51 },
],
columns: 31,
rows: 9,
},
};
index.js
import Monologue from 'monologue.js';
import Presets from 'paraviewweb/src/Common/Core/LookupTable/Presets';

const CHANGE_TOPIC = 'LookupTable.change';

// Global helper methods ------------------------------------------------------

function applyRatio(a, b, ratio) {
return (b - a) * ratio + a;
}

function interpolateColor(pointA, pointB, scalar) {
const ratio = (scalar - pointA[0]) / (pointB[0] - pointA[0]);
return [
applyRatio(pointA[1], pointB[1], ratio),
applyRatio(pointA[2], pointB[2], ratio),
applyRatio(pointA[3], pointB[3], ratio),
255,
];
}

function extractPoint(controlPoints, idx) {
return [
controlPoints[idx].x,
controlPoints[idx].r,
controlPoints[idx].g,
controlPoints[idx].b,
];
}

function xrgbCompare(a, b) {
return a.x - b.x;
}

// ----------------------------------------------------------------------------

export default class LookupTable {
constructor(name, discrete = false) {
this.name = name;
this.scalarRange = [0, 1];
this.delta = 1;
this.controlPoints = null;
this.colorTableSize = 256;
this.colorTable = null;
this.colorNaN = [0, 0, 0, 0];
this.setPreset('spectralflip');
this.discrete = discrete;
this.scale = 1;

// Auto rebuild
this.build();
}

getName() {
return this.name;
}

/* eslint-disable class-methods-use-this */
getPresets() {
return Object.keys(Presets.lookuptables);
}

setPreset(name) {
this.colorTable = null;
this.controlPoints = [];

const colors = Presets.lookuptables[name].controlpoints;
const count = colors.length;

for (let i = 0; i < count; i++) {
this.controlPoints.push({
x: colors[i].x,
r: colors[i].r,
g: colors[i].g,
b: colors[i].b,
});
}

// Auto rebuild
this.build();

this.emit(CHANGE_TOPIC, { change: 'preset', lut: this });
}

updateControlPoints(controlPoints) {
this.colorTable = null;
this.controlPoints = [];

const count = controlPoints.length;

for (let i = 0; i < count; i++) {
this.controlPoints.push({
x: controlPoints[i].x,
r: controlPoints[i].r,
g: controlPoints[i].g,
b: controlPoints[i].b,
});
}

// Auto rebuild
this.build();

this.emit(CHANGE_TOPIC, { change: 'controlPoints', lut: this });
}

setColorForNaN(r = 0, g = 0, b = 0, a = 0) {
this.colorNaN = [r, g, b, a];
}

getColorForNaN() {
return this.colorNaN;
}

getScalarRange() {
return [Number(this.scalarRange[0]), Number(this.scalarRange[1])];
}

setScalarRange(min, max) {
this.scalarRange = [min, max];
this.delta = max - min;

this.emit(CHANGE_TOPIC, { change: 'scalarRange', lut: this });
}

build(trigger) {
let currentControlIdx = 0;

if (this.colorTable) {
return;
}

this.colorTable = [];
if (this.discrete) {
this.colorTableSize = this.controlPoints.length;
this.scale = 50;
for (let idx = 0; idx < this.colorTableSize; idx++) {
const color = this.controlPoints[idx];
this.colorTable.push([color.r, color.g, color.b, 255]);
}
} else {
this.scale = 1;
for (let idx = 0; idx < this.colorTableSize; idx++) {
const value = idx / (this.colorTableSize - 1);
let pointA = extractPoint(this.controlPoints, currentControlIdx);
let pointB = extractPoint(this.controlPoints, currentControlIdx + 1);

if (value > pointB[0]) {
currentControlIdx += 1;
pointA = extractPoint(this.controlPoints, currentControlIdx);
pointB = extractPoint(this.controlPoints, currentControlIdx + 1);
}

this.colorTable.push(interpolateColor(pointA, pointB, value));
}
}

if (trigger) {
this.emit(CHANGE_TOPIC, { change: 'controlPoints', lut: this });
}
}

setNumberOfColors(nbColors) {
this.colorTableSize = nbColors;
this.colorTable = null;

// Auto rebuild
this.build();

this.emit(CHANGE_TOPIC, { change: 'numberOfColors', lut: this });
}

getNumberOfControlPoints() {
return this.controlPoints ? this.controlPoints.length : 0;
}

removeControlPoint(idx) {
if (idx > 0 && idx < this.controlPoints.length - 1) {
this.controlPoints.splice(idx, 1);

// Auto rebuild and trigger change
this.colorTable = null;
this.build(true);

return true;
}
return false;
}

getControlPoint(idx) {
return this.controlPoints[idx];
}

updateControlPoint(idx, xrgb) {
this.controlPoints[idx] = xrgb;
const xValue = xrgb.x;

// Ensure order
this.controlPoints.sort(xrgbCompare);

// Auto rebuild and trigger change
this.colorTable = null;
this.build(true);

// Return the modified index of current control point
for (let i = 0; i < this.controlPoints.length; i++) {
if (this.controlPoints[i].x === xValue) {
return i;
}
}
return 0;
}

addControlPoint(xrgb) {
this.controlPoints.push(xrgb);
const xValue = xrgb.x;

// Ensure order
this.controlPoints.sort(xrgbCompare);

// Auto rebuild and trigger change
this.colorTable = null;
this.build(true);

// Return the modified index of current control point
for (let i = 0; i < this.controlPoints.length; i++) {
if (this.controlPoints[i].x === xValue) {
return i;
}
}
return -1;
}

drawToCanvas(canvas) {
const colors = this.colorTable;
const length = this.scale * colors.length;
const ctx = canvas.getContext('2d');
const canvasData = ctx.getImageData(0, 0, length, 1);

for (let i = 0; i < length; i++) {
const colorIdx = Math.floor(i / this.scale);
canvasData.data[i * 4 + 0] = Math.floor(255 * colors[colorIdx][0]);
canvasData.data[i * 4 + 1] = Math.floor(255 * colors[colorIdx][1]);
canvasData.data[i * 4 + 2] = Math.floor(255 * colors[colorIdx][2]);
canvasData.data[i * 4 + 3] = 255;
}
ctx.putImageData(canvasData, 0, 0);
}

getColor(scalar) {
if (Number.isNaN(scalar)) {
return this.colorNaN;
}
const idxValue = Math.floor(
(this.colorTableSize * (scalar - this.scalarRange[0])) / this.delta
);
if (idxValue < 0) {
return this.colorTable[0];
}
if (idxValue >= this.colorTableSize) {
return this.colorTable[this.colorTable.length - 1];
}
return this.colorTable[idxValue];
}

destroy() {
this.off();
}

onChange(callback) {
return this.on(CHANGE_TOPIC, callback);
}
}

// Add Observer pattern using Monologue.js
Monologue.mixInto(LookupTable);