Skip to content

Commit

Permalink
Clean up colormap finding function
Browse files Browse the repository at this point in the history
  • Loading branch information
hanbyul-here committed Sep 5, 2024
1 parent fe2ab2a commit 40649df
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions app/scripts/components/common/map/layer-legend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,6 @@ export function LayerLegendContainer(props: LayerLegendContainerProps) {

export function LayerCategoricalGraphic(props: LayerLegendCategorical) {
const { stops } = props;

return (
<LegendList>
{stops.map((stop) => (
Expand Down Expand Up @@ -349,24 +348,23 @@ export const LayerGradientGraphic = (props: LayerLegendGradient) => {

export const LayerGradientColormapGraphic = (props: Omit<LayerLegendGradient, 'stops' | 'type'>) => {
const { colorMap, ...otherProps } = props;

const colormap = findColormapByName(colorMap ?? 'viridis');
if (!colormap) {
const colormapResult = findColormapByName(colorMap ?? 'viridis');
if (!colormapResult) {
return null;
}
const { foundColorMap, isReversed } = colormapResult;

const stops = Object.values(colormap).map((value) => {
if (Array.isArray(value) && value.length === 4) {
return `rgba(${value.join(',')})`;
} else {
return `rgba(0, 0, 0, 1)`;
}
const stops = Object.values(foundColorMap)
.filter(value => Array.isArray(value) && value.length === 4)
.map((value) => {
return `rgba(${(value as Array<number>).join(',')})`;
});

const processedStops = colormap.isReversed
const processedStops = isReversed
? stops.reduceRight((acc, stop) => [...acc, stop], [])
: stops;

return <LayerGradientGraphic type='gradient' stops={processedStops} {...otherProps} />;
};

Expand All @@ -380,5 +378,5 @@ export const findColormapByName = (name: string) => {
return null;
}

return { ...colormap, isReversed };
return { foundColorMap: {...colormap}, isReversed };
};

0 comments on commit 40649df

Please sign in to comment.