Skip to content

Commit ae02cb7

Browse files
nakul-pymfisher87
andauthored
Add cmocean colormaps, bugfixes (#886)
Co-authored-by: Matt Fisher <[email protected]>
1 parent f2a8c5b commit ae02cb7

File tree

14 files changed

+658
-117
lines changed

14 files changed

+658
-117
lines changed

.eslintrc.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ module.exports = {
2222
},
2323
},
2424
],
25-
"@typescript-eslint/no-unused-vars": ["warn", { args: "none" }],
25+
"@typescript-eslint/no-unused-vars": [
26+
"warn",
27+
{
28+
args: "none",
29+
varsIgnorePattern: "^_$"
30+
}
31+
],
2632
"@typescript-eslint/no-explicit-any": "off",
2733
"@typescript-eslint/no-namespace": "off",
2834
"@typescript-eslint/ban-ts-comment": "warn",
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import colormap from 'colormap';
2+
import colorScale from 'colormap/colorScale.js';
3+
import { useEffect } from 'react';
4+
5+
import rawCmocean from '@/src/dialogs/symbology/components/color_ramp/cmocean.json';
6+
7+
export interface IColorMap {
8+
name: ColorRampName;
9+
colors: string[];
10+
}
11+
12+
const { __license__: _, ...cmocean } = rawCmocean;
13+
14+
Object.assign(colorScale, cmocean);
15+
16+
export const COLOR_RAMP_NAMES = [
17+
'jet',
18+
// 'hsv', 11 steps min
19+
'hot',
20+
'cool',
21+
'spring',
22+
'summer',
23+
'autumn',
24+
'winter',
25+
'bone',
26+
'copper',
27+
'greys',
28+
'YiGnBu',
29+
'greens',
30+
'YiOrRd',
31+
'bluered',
32+
'RdBu',
33+
// 'picnic', 11 steps min
34+
'rainbow',
35+
'portland',
36+
'blackbody',
37+
'earth',
38+
'electric',
39+
'viridis',
40+
'inferno',
41+
'magma',
42+
'plasma',
43+
'warm',
44+
// 'rainbow-soft', 11 steps min
45+
'bathymetry',
46+
'cdom',
47+
'chlorophyll',
48+
'density',
49+
'freesurface-blue',
50+
'freesurface-red',
51+
'oxygen',
52+
'par',
53+
'phase',
54+
'salinity',
55+
'temperature',
56+
'turbidity',
57+
'velocity-blue',
58+
'velocity-green',
59+
// 'cubehelix' 16 steps min
60+
'ice',
61+
'oxy',
62+
'matter',
63+
'amp',
64+
'tempo',
65+
'rain',
66+
'topo',
67+
'balance',
68+
'delta',
69+
'curl',
70+
'diff',
71+
'tarn',
72+
] as const;
73+
74+
export type ColorRampName = (typeof COLOR_RAMP_NAMES)[number];
75+
76+
export const getColorMapList = (): IColorMap[] => {
77+
const colorMapList: IColorMap[] = [];
78+
79+
COLOR_RAMP_NAMES.forEach(name => {
80+
const colorRamp = colormap({
81+
colormap: name,
82+
nshades: 255,
83+
format: 'rgbaString',
84+
});
85+
86+
colorMapList.push({ name, colors: colorRamp });
87+
});
88+
89+
return colorMapList;
90+
};
91+
92+
/**
93+
* Hook that loads and sets color maps.
94+
*/
95+
export const useColorMapList = (setColorMaps: (maps: IColorMap[]) => void) => {
96+
useEffect(() => {
97+
setColorMaps(getColorMapList());
98+
}, [setColorMaps]);
99+
};
100+
101+
/**
102+
* Ensure we always get a valid hex string from either an RGB(A) array or string.
103+
*/
104+
export const ensureHexColorCode = (color: number[] | string): string => {
105+
if (typeof color === 'string') {
106+
return color;
107+
}
108+
109+
// color must be an RGBA array
110+
const hex = color
111+
.slice(0, -1) // Color input doesn't support hex alpha values so cut that out
112+
.map((val: { toString: (arg0: number) => string }) => {
113+
return val.toString(16).padStart(2, '0');
114+
})
115+
.join('');
116+
117+
return '#' + hex;
118+
};
119+
120+
/**
121+
* Convert hex to [r,g,b,a] array.
122+
*/
123+
export function hexToRgb(hex: string): [number, number, number, number] {
124+
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
125+
126+
if (!result) {
127+
console.warn('Unable to parse hex value, defaulting to black');
128+
return [0, 0, 0, 255];
129+
}
130+
return [
131+
parseInt(result[1], 16),
132+
parseInt(result[2], 16),
133+
parseInt(result[3], 16),
134+
255, // TODO: Make alpha customizable?
135+
];
136+
}

packages/base/src/dialogs/symbology/components/color_ramp/CanvasSelectComponent.tsx

Lines changed: 2 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Button } from '@jupyterlab/ui-components';
2-
import colormap from 'colormap';
32
import React, { useEffect, useRef, useState } from 'react';
43

4+
import { useColorMapList } from '@/src/dialogs/symbology/colorRampUtils';
55
import ColorRampEntry from './ColorRampEntry';
66

77
export interface IColorMap {
@@ -18,71 +18,11 @@ const CanvasSelectComponent: React.FC<ICanvasSelectComponentProps> = ({
1818
selectedRamp,
1919
setSelected,
2020
}) => {
21-
const colorRampNames = [
22-
'jet',
23-
// 'hsv', 11 steps min
24-
'hot',
25-
'cool',
26-
'spring',
27-
'summer',
28-
'autumn',
29-
'winter',
30-
'bone',
31-
'copper',
32-
'greys',
33-
'YiGnBu',
34-
'greens',
35-
'YiOrRd',
36-
'bluered',
37-
'RdBu',
38-
// 'picnic', 11 steps min
39-
'rainbow',
40-
'portland',
41-
'blackbody',
42-
'earth',
43-
'electric',
44-
'viridis',
45-
'inferno',
46-
'magma',
47-
'plasma',
48-
'warm',
49-
// 'rainbow-soft', 11 steps min
50-
'bathymetry',
51-
'cdom',
52-
'chlorophyll',
53-
'density',
54-
'freesurface-blue',
55-
'freesurface-red',
56-
'oxygen',
57-
'par',
58-
'phase',
59-
'salinity',
60-
'temperature',
61-
'turbidity',
62-
'velocity-blue',
63-
'velocity-green',
64-
// 'cubehelix' 16 steps min
65-
];
66-
6721
const containerRef = useRef<HTMLDivElement>(null);
6822
const [isOpen, setIsOpen] = useState(false);
6923
const [colorMaps, setColorMaps] = useState<IColorMap[]>([]);
7024

71-
useEffect(() => {
72-
const colorMapList: IColorMap[] = [];
73-
74-
colorRampNames.forEach(name => {
75-
const colorRamp = colormap({
76-
colormap: name,
77-
nshades: 255,
78-
format: 'rgbaString',
79-
});
80-
const colorMap = { name: name, colors: colorRamp };
81-
colorMapList.push(colorMap);
82-
83-
setColorMaps(colorMapList);
84-
});
85-
}, []);
25+
useColorMapList(setColorMaps);
8626

8727
useEffect(() => {
8828
if (colorMaps.length > 0) {

packages/base/src/dialogs/symbology/components/color_ramp/ColorRamp.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@ const ColorRamp: React.FC<IColorRampProps> = ({
3838
const [isLoading, setIsLoading] = useState(false);
3939

4040
useEffect(() => {
41-
populateOptions();
41+
if (selectedRamp === '' && selectedMode === '' && numberOfShades === '') {
42+
populateOptions();
43+
}
4244
}, [layerParams]);
4345

44-
const populateOptions = async () => {
46+
const populateOptions = () => {
4547
let nClasses, singleBandMode, colorRamp;
4648

4749
if (layerParams.symbologyState) {

0 commit comments

Comments
 (0)