diff --git a/src/renderer/viz/expressions/color/palettes/Palette.js b/src/renderer/viz/expressions/color/palettes/Palette.js index 0b8a9c5c3..65c0ff62f 100644 --- a/src/renderer/viz/expressions/color/palettes/Palette.js +++ b/src/renderer/viz/expressions/color/palettes/Palette.js @@ -3,8 +3,6 @@ import { hexToRgb } from '../../utils'; import { RGBA } from '../rgb'; import { constant } from '../../../expressions'; -const MIN_CARTOCOLOR_SUBPALETTE_SIZE = 2; - /** * Color palettes. * @@ -70,23 +68,33 @@ export default class Palette extends BaseExpression { } _getBestSubPalette (subPaletteIndex) { - subPaletteIndex = subPaletteIndex <= MIN_CARTOCOLOR_SUBPALETTE_SIZE - ? MIN_CARTOCOLOR_SUBPALETTE_SIZE - : subPaletteIndex; - const longestSubPalette = this.getLongestSubPalette(); - const subPalette = (subPaletteIndex < longestSubPalette.length - ? [...this.subPalettes[subPaletteIndex]] - : [...longestSubPalette]); + const longestSubPaletteIndex = this.getLongestSubPaletteIndex(); + const smallestSubPaletteIndex = this.getSmallestSubPaletteIndex(); + if (!Number.isInteger(subPaletteIndex) || subPaletteIndex > longestSubPaletteIndex) { + subPaletteIndex = longestSubPaletteIndex; + } else if (subPaletteIndex < smallestSubPaletteIndex) { + subPaletteIndex = smallestSubPaletteIndex; + } + const subPalette = [...this.subPalettes[subPaletteIndex]]; return subPalette.map(color => new RGBA(constant(color.r), constant(color.g), constant(color.b), constant(color.a)) ); } - getLongestSubPalette () { + getSmallestSubPaletteIndex () { + const s = this.subPalettes; + for (let i = 0; i <= 20; i++) { + if (s[i]) { + return i; + } + } + } + + getLongestSubPaletteIndex () { const s = this.subPalettes; for (let i = 20; i >= 0; i--) { if (s[i]) { - return s[i]; + return i; } } } diff --git a/test/integration/render/scenarios/regression/cartocolor-subpalette/reference.png b/test/integration/render/scenarios/regression/cartocolor-subpalette/reference.png new file mode 100644 index 000000000..0bb3efe72 Binary files /dev/null and b/test/integration/render/scenarios/regression/cartocolor-subpalette/reference.png differ diff --git a/test/integration/render/scenarios/regression/cartocolor-subpalette/scenario.js b/test/integration/render/scenarios/regression/cartocolor-subpalette/scenario.js new file mode 100644 index 000000000..01ac91116 --- /dev/null +++ b/test/integration/render/scenarios/regression/cartocolor-subpalette/scenario.js @@ -0,0 +1,19 @@ +const map = new CartoMap({ + container: 'map', + background: 'black' +}); + +const source = new carto.source.GeoJSON(sources['points3']); +const viz = new carto.Viz(` + color: ramp(top(@prop, @num), CB_BLUES) + width: 50 + @prop: $cat + @num: 2 +`); +const layer = new carto.Layer('layer', source, viz); + +viz.variables.num.blendTo(2, 0); +layer.addTo(map); +layer.on('loaded', () => { + window.loaded = true; +});