Skip to content

Commit d7a09bc

Browse files
committed
Adding default classes to colormaps (#922)
1 parent 0b02f7b commit d7a09bc

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/ColorRampControls.tsx

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import React, { useEffect, useState } from 'react';
2424
import { LoadingIcon } from '@/src/shared/components/loading';
2525
import ColorRampSelector from './ColorRampSelector';
2626
import ModeSelectRow from './ModeSelectRow';
27+
import { COLOR_RAMP_DEFAULTS, ColorRampName } from '../../colorRampUtils';
2728

2829
interface IColorRampControlsProps {
2930
modeOptions: string[];
@@ -55,13 +56,46 @@ const ColorRampControls: React.FC<IColorRampControlsProps> = ({
5556
const [selectedMode, setSelectedMode] = useState('');
5657
const [numberOfShades, setNumberOfShades] = useState('');
5758
const [isLoading, setIsLoading] = useState(false);
59+
const [warning, setWarning] = useState<string | null>(null);
5860

5961
useEffect(() => {
6062
if (selectedRamp === '' && selectedMode === '' && numberOfShades === '') {
6163
populateOptions();
6264
}
6365
}, [layerParams]);
6466

67+
useEffect(() => {
68+
if (!selectedRamp) {
69+
return;
70+
}
71+
72+
const defaultClasses =
73+
COLOR_RAMP_DEFAULTS[selectedRamp as ColorRampName] ?? 9;
74+
75+
setNumberOfShades(defaultClasses.toString());
76+
setWarning(null);
77+
}, [selectedRamp]);
78+
79+
useEffect(() => {
80+
if (!selectedRamp || !numberOfShades) {
81+
return;
82+
}
83+
84+
const minRequired = COLOR_RAMP_DEFAULTS[selectedRamp as ColorRampName];
85+
const shades = parseInt(numberOfShades, 10);
86+
const rampLabel = selectedRamp
87+
.split('-')
88+
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
89+
.join('-');
90+
91+
if (minRequired && shades < minRequired) {
92+
setWarning(
93+
`${rampLabel} requires at least ${minRequired} classes (got ${shades})`,
94+
);
95+
} else {
96+
setWarning(null);
97+
}
98+
}, [selectedRamp, numberOfShades]);
6599
const populateOptions = () => {
66100
let nClasses, singleBandMode, colorRamp;
67101

@@ -70,9 +104,13 @@ const ColorRampControls: React.FC<IColorRampControlsProps> = ({
70104
singleBandMode = layerParams.symbologyState.mode;
71105
colorRamp = layerParams.symbologyState.colorRamp;
72106
}
73-
setNumberOfShades(nClasses ? nClasses : '9');
107+
const defaultRamp = colorRamp ? colorRamp : 'viridis';
108+
const defaultClasses =
109+
nClasses ?? COLOR_RAMP_DEFAULTS[defaultRamp as ColorRampName] ?? 9;
110+
111+
setNumberOfShades(defaultClasses.toString());
74112
setSelectedMode(singleBandMode ? singleBandMode : 'equal interval');
75-
setSelectedRamp(colorRamp ? colorRamp : 'viridis');
113+
setSelectedRamp(defaultRamp);
76114
};
77115

78116
return (
@@ -95,11 +133,20 @@ const ColorRampControls: React.FC<IColorRampControlsProps> = ({
95133
setSelectedMode={setSelectedMode}
96134
/>
97135
)}
136+
{warning && (
137+
<div
138+
className="jp-gis-warning"
139+
style={{ color: 'orange', marginTop: 4 }}
140+
>
141+
{warning}
142+
</div>
143+
)}
98144
{isLoading ? (
99145
<LoadingIcon />
100146
) : (
101147
<Button
102148
className="jp-Dialog-button jp-mod-accept jp-mod-styled"
149+
disabled={!!warning}
103150
onClick={() =>
104151
classifyFunc(
105152
selectedMode,

0 commit comments

Comments
 (0)