In Matlab, you can create a custom colormap using the colormap
function. For your case, you can define a colormap that maps negative values to red and positive values to blue:
main.m163 chars3 lines
This creates a 200x3 matrix cmap
, where each row corresponds to a color. The first 100 rows are red-to-blue colors, and the second 100 rows are blue-to-red colors. For example, the first row is [1 0 0]
(pure red), and the last row is [0 0 1]
(pure blue).
To use this colormap in JavaScript, you can convert the cmap
matrix into an array of RGB strings using the rgb2hex
function (which is included in some JavaScript libraries like Chroma.js). Here's an example:
index.tsx714 chars23 lines
This code creates a color scale that maps the range [-1, 1]
to the red-to-blue colormap we defined earlier, and then uses the chroma.scale
function to create a color scale object. The colors
array is then populated by calling the color scale object with each value in the data
array.
Note that this code requires the Chroma.js library, which you can download from https://vis4.net/chromajs/.
gistlibby LogSnag