Palette in evalscript

Hi everyone.

I’m gonna trying to create a Index Vegetation palette that range from red for low values to green to high values in % classes, where doesn’t matter the value range, the image must to exibit all color ranges. Anyone knows how can I apply it?
In addition, In low values, I’m getting white color exibition instead red color and I thing this is also an assues to solve.
I am sharing the eval script below and an example of image exibition with this white color issues.

//VERSION=3
function setup() {
  return {
    input: [{
      bands:["B04", "B08", "dataMask"],
    }],
    output: {
      id: "default",
      bands: 4,
    }
  }
}
function evaluatePixel(sample) {
    let ndvi = (((sample.B08 - sample.B04) / (sample.B08 + sample.B04)+1)*0.5) * 100
    
   if (ndvi==0) return [2.15, 0.25, 0.28, sample.dataMask]
    else if (ndvi<5) return [ 2.23, 0.56, 0.42, sample.dataMask]
    else if (ndvi<10) return [2.31, 0.88, 0.57, sample.dataMask]
    else if (ndvi<15) return [2.39, 1.19, 0.71, sample.dataMask]
    else if (ndvi<20) return [2.47, 1.51, 0.86, sample.dataMask]
    else if (ndvi<25) return [2.54, 1.78, 1.02, sample.dataMask]
    else if (ndvi<30) return [2.54, 1.96, 1.22, sample.dataMask]
    else if (ndvi<35) return [2.54, 2.13, 1.42, sample.dataMask]
    else if (ndvi<40) return [2.55, 2.30, 1.62, sample.dataMask]
    else if (ndvi<45) return [2.55, 2.47, 1.82, sample.dataMask]
    else if (ndvi<50) return [2.46, 2.51, 1.83, sample.dataMask]
    else if (ndvi<55) return [2.27, 2.43, 1.65, sample.dataMask]
    else if (ndvi<60) return [2.08, 2.35, 1.47, sample.dataMask]
    else if (ndvi<65) return [1.90, 2.27, 1.29, sample.dataMask]
    else if (ndvi<70) return [1.71, 2.19, 1.10, sample.dataMask]
    else if (ndvi<75) return [1.44, 2.07, 0.99, sample.dataMask]
    else if (ndvi<80) return [1.14, 193, 0.91, sample.dataMask]
    else if (ndvi<85) return [0.85, 1.78, 0.82, sample.dataMask]
    else if (ndvi<90) return [0.55, 1.64, 0.73, sample.dataMask]
    else if (ndvi<=100) return [0.26, 1.50, 0.65, sample.dataMask]
    else return [0,0.27,0, sample.dataMask]
}

Note that I have no RGB White color in palette.
response

I’vejust solved the issue about white color. I’m doing a mistake dividing RGB values per 100 instead 255.
So, remained only the question about join all the values of the images in all ramp color from red to green.

Hi,

Have you seen the documentation page about creating colour palettes in Evalscripts? You can either create discrete pairs of colours/values or a gradient which could apply in your case. There are also other methods that you may find interesting in that page.

Essentially the method to make your own colour ramp is the following:

  1. Define the colours, e.g.:
const ramps = [
  [200, 0xff0000],
  [300, 0x0000ff ],
];
  1. Apply the type of ramp (discrete or gradient or other), e.g.:
const visualizer = new ColorRampVisualizer(ramps);
  1. The two steps above can be done outside of the EvaluatePixel function. Then, in your Evalscript, you can call the function, e.g.:
visualizer.process(sample.B02);

This will return a colour triplet, e.g. [ 0.5019607843137255, 0, 0.5019607843137255 ].

You can make the list manually for a colour ramp, but to make it easier, we have also made a Python script that converts matplotlib standard ramps to a list ready for the Evalscript:

1 Like

Could you give me an example in an Evalscript?

You have all the elements in my post and in the Jupyter Notebook to integrate this code in an Evalscript if you read the instructions carefully. That being said, here is an example Evalscript (link to EOB):

//VERSION=3

function setup() {
  return {
    input: ["B04", "B08", "dataMask"],
    output: { bands: 4 }
  };
}

function evaluatePixel(sample) {
  let ndviP = index(sample.B08, sample.B04) * 100;

  return [...visualizer.process(ndviP), sample.dataMask]

  // To return an RGB without adding dataMask (set output bands to 3):
  // return visualizer.process(ndviP)
}

const ramp = [[0.0, '0xffffe5'],
 [10.0, '0xf9fdc2'],
 [20.0, '0xe5f5ac'],
 [30.0, '0xc7e89b'],
 [40.0, '0xa2d88a'],
 [50.0, '0x78c679'],
 [60.0, '0x4cb063'],
 [70.0, '0x2f944d'],
 [80.0, '0x15793e'],
 [90.0, '0x006134'],
 [100.0, '0x004529']];

 const visualizer = new ColorRampVisualizer(ramp);
1 Like

Thanks, Maxim. It worked well.

In a case of colorBlend function, where I insert dataMask to transparency in the return?

There is plenty of examples in our documentation here, that will help you understand the dataMask functionality. As you can read in the documentation, you can apply the dataMask in the EvaluatePixel() function.

Example 2 shows you how to use the dataMask as a transparency band. This should give you all the information you require to achieve what you need to do.

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.