Skip to content

Commit 1411199

Browse files
authored
Adding default classes to colormaps (#922)
1 parent 44a66d1 commit 1411199

File tree

2 files changed

+60
-6
lines changed

2 files changed

+60
-6
lines changed

packages/base/src/dialogs/symbology/colorRampUtils.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Object.assign(colorScale, cmocean);
1515

1616
export const COLOR_RAMP_NAMES = [
1717
'jet',
18-
// 'hsv', 11 steps min
18+
'hsv',
1919
'hot',
2020
'cool',
2121
'spring',
@@ -30,7 +30,7 @@ export const COLOR_RAMP_NAMES = [
3030
'YiOrRd',
3131
'bluered',
3232
'RdBu',
33-
// 'picnic', 11 steps min
33+
'picnic',
3434
'rainbow',
3535
'portland',
3636
'blackbody',
@@ -41,7 +41,7 @@ export const COLOR_RAMP_NAMES = [
4141
'magma',
4242
'plasma',
4343
'warm',
44-
// 'rainbow-soft', 11 steps min
44+
'rainbow-soft',
4545
'bathymetry',
4646
'cdom',
4747
'chlorophyll',
@@ -56,7 +56,7 @@ export const COLOR_RAMP_NAMES = [
5656
'turbidity',
5757
'velocity-blue',
5858
'velocity-green',
59-
// 'cubehelix' 16 steps min
59+
'cubehelix',
6060
'ice',
6161
'oxy',
6262
'matter',
@@ -71,6 +71,13 @@ export const COLOR_RAMP_NAMES = [
7171
'tarn',
7272
] as const;
7373

74+
export const COLOR_RAMP_DEFAULTS: Partial<Record<ColorRampName, number>> = {
75+
hsv: 11,
76+
picnic: 11,
77+
'rainbow-soft': 11,
78+
cubehelix: 16,
79+
} as const;
80+
7481
export type ColorRampName = (typeof COLOR_RAMP_NAMES)[number];
7582

7683
export const getColorMapList = (): IColorMap[] => {

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

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import React, { useEffect, useState } from 'react';
55
import { LoadingIcon } from '@/src/shared/components/loading';
66
import CanvasSelectComponent from './CanvasSelectComponent';
77
import ModeSelectRow from './ModeSelectRow';
8+
import { COLOR_RAMP_DEFAULTS, ColorRampName } from '../../colorRampUtils';
89

910
interface IColorRampProps {
1011
modeOptions: string[];
@@ -36,13 +37,46 @@ const ColorRamp: React.FC<IColorRampProps> = ({
3637
const [selectedMode, setSelectedMode] = useState('');
3738
const [numberOfShades, setNumberOfShades] = useState('');
3839
const [isLoading, setIsLoading] = useState(false);
40+
const [warning, setWarning] = useState<string | null>(null);
3941

4042
useEffect(() => {
4143
if (selectedRamp === '' && selectedMode === '' && numberOfShades === '') {
4244
populateOptions();
4345
}
4446
}, [layerParams]);
4547

48+
useEffect(() => {
49+
if (!selectedRamp) {
50+
return;
51+
}
52+
53+
const defaultClasses =
54+
COLOR_RAMP_DEFAULTS[selectedRamp as ColorRampName] ?? 9;
55+
56+
setNumberOfShades(defaultClasses.toString());
57+
setWarning(null);
58+
}, [selectedRamp]);
59+
60+
useEffect(() => {
61+
if (!selectedRamp || !numberOfShades) {
62+
return;
63+
}
64+
65+
const minRequired = COLOR_RAMP_DEFAULTS[selectedRamp as ColorRampName];
66+
const shades = parseInt(numberOfShades, 10);
67+
const rampLabel = selectedRamp
68+
.split('-')
69+
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
70+
.join('-');
71+
72+
if (minRequired && shades < minRequired) {
73+
setWarning(
74+
`${rampLabel} requires at least ${minRequired} classes (got ${shades})`,
75+
);
76+
} else {
77+
setWarning(null);
78+
}
79+
}, [selectedRamp, numberOfShades]);
4680
const populateOptions = () => {
4781
let nClasses, singleBandMode, colorRamp;
4882

@@ -51,9 +85,13 @@ const ColorRamp: React.FC<IColorRampProps> = ({
5185
singleBandMode = layerParams.symbologyState.mode;
5286
colorRamp = layerParams.symbologyState.colorRamp;
5387
}
54-
setNumberOfShades(nClasses ? nClasses : '9');
88+
const defaultRamp = colorRamp ? colorRamp : 'viridis';
89+
const defaultClasses =
90+
nClasses ?? COLOR_RAMP_DEFAULTS[defaultRamp as ColorRampName] ?? 9;
91+
92+
setNumberOfShades(defaultClasses.toString());
5593
setSelectedMode(singleBandMode ? singleBandMode : 'equal interval');
56-
setSelectedRamp(colorRamp ? colorRamp : 'viridis');
94+
setSelectedRamp(defaultRamp);
5795
};
5896

5997
return (
@@ -76,11 +114,20 @@ const ColorRamp: React.FC<IColorRampProps> = ({
76114
setSelectedMode={setSelectedMode}
77115
/>
78116
)}
117+
{warning && (
118+
<div
119+
className="jp-gis-warning"
120+
style={{ color: 'orange', marginTop: 4 }}
121+
>
122+
{warning}
123+
</div>
124+
)}
79125
{isLoading ? (
80126
<LoadingIcon />
81127
) : (
82128
<Button
83129
className="jp-Dialog-button jp-mod-accept jp-mod-styled"
130+
disabled={!!warning}
84131
onClick={() =>
85132
classifyFunc(
86133
selectedMode,

0 commit comments

Comments
 (0)