Skip to content

Commit

Permalink
feat(config): improve the default color ramp with more color ranges (#…
Browse files Browse the repository at this point in the history
…3172)

#### Motivation

The current default color ramp doesn't use very many colors and makes
new zealand very green, use another color ramp with more stops and
colors

#### Modification

adjusts the default color ramp to a more complex color ramp.

#### Checklist

_If not applicable, provide explanation of why._

- [ ] Tests updated
- [ ] Docs updated
- [ ] Issue linked in Title
  • Loading branch information
blacha committed Mar 7, 2024
1 parent ced60bc commit 2c7b8e5
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 31 deletions.
47 changes: 46 additions & 1 deletion packages/config/src/config/tile.set.output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,50 @@ export const DefaultColorRampOutput: ConfigTileSetRasterOutput = {
title: 'Color ramp',
name: 'color-ramp',
pipeline: [{ type: 'color-ramp' }],
background: { r: 188, g: 222, b: 254, alpha: 1 },
// Taken from 0 of DefaultColorRamp
background: { r: 167, g: 205, b: 228, alpha: 1 },
} as const;

export const DefaultColorRamp = `-8764 0 0 0 255
-4000 3 45 85 255
-100 0 101 199 255
-3 167 205 228 255
0 167 205 228 255
10 245 234 163 255
20 236 232 157 255
30 227 229 150 255
40 218 227 144 255
50 209 225 138 255
75 174 215 112 255
100 147 208 93 255
150 69 179 53 255
200 21 151 47 255
250 18 130 63 255
300 81 144 58 255
350 132 158 47 255
400 181 171 35 255
450 233 181 17 255
500 235 149 2 255
550 209 97 2 255
600 177 54 2 255
700 148 20 1 255
800 124 6 1 255
900 117 21 4 255
1000 113 30 6 255
1100 109 39 9 255
1200 106 45 12 255
1300 117 64 30 255
1400 133 88 60 255
1500 149 113 93 255
1600 162 144 135 255
1700 173 173 172 255
1800 190 190 190 255
2000 197 196 197 255
2200 203 203 203 255
2400 207 206 207 255
2800 210 209 210 255
3200 214 212 214 255
3600 217 215 217 255
4000 220 219 220 255
4400 224 222 224 255
4800 227 225 227 255`;
2 changes: 1 addition & 1 deletion packages/config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export {
TileResizeKernel,
TileSetType,
} from './config/tile.set.js';
export { DefaultColorRampOutput, DefaultTerrainRgbOutput } from './config/tile.set.output.js';
export { DefaultColorRamp, DefaultColorRampOutput, DefaultTerrainRgbOutput } from './config/tile.set.output.js';
export { ConfigTileSetOutputParser } from './config/tile.set.pipeline.js';
export { ConfigVectorStyle, Layer, Sources, StyleJson } from './config/vector.style.js';
export { ConfigBundled, ConfigProviderMemory } from './memory/memory.config.js';
Expand Down
1 change: 1 addition & 0 deletions packages/tiler-sharp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"test": "node --test"
},
"dependencies": {
"@basemaps/config": "^7.0.0",
"@basemaps/geo": "^7.0.0",
"@basemaps/tiler": "^7.0.0",
"@linzjs/metrics": "^7.0.0",
Expand Down
37 changes: 8 additions & 29 deletions packages/tiler-sharp/src/pipeline/pipeline.color.ramp.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,32 @@
import { DefaultColorRamp } from '@basemaps/config';
import { Tiff } from '@cogeotiff/core';

import { DecompressedInterleaved, Pipeline } from './decompressor.js';

export class ColorRamp {
noData: { v: number; color: [number, number, number, number] } = { v: NaN, color: [0, 0, 0, 0] };
ramps: { v: number; color: [number, number, number, number] }[] = [];
constructor(ramp: string, noDataValue: number) {
const ramps = ramp.split('\n');
constructor(ramp: string) {
const ramps = ramp.trim().split('\n');

for (const ramp of ramps) {
const parts = ramp.trim().split(' ');
if (parts[0] === 'nv') {
this.noData = { v: noDataValue, color: parts.slice(1).map(Number) as [number, number, number, number] };
continue;
}
const numbers = parts.map(Number);
this.ramps.push({ v: numbers[0], color: numbers.slice(1) as [number, number, number, number] });
}
}

get(num: number): [number, number, number, number] {
if (num === this.noData.v) return this.noData.color;

const first = this.ramps[0];
if (num < first.v) return first.color;
if (num <= first.v) return first.color;

for (let i = 0; i < this.ramps.length - 1; i++) {
const ramp = this.ramps[i];
const rampNext = this.ramps[i + 1];
if (num >= rampNext.v) continue;
if (num < ramp.v) continue;
if (ramp.v === num) return ramp.color;

const rampNext = this.ramps[i + 1];
if (num >= rampNext.v) continue;

const range = rampNext.v - ramp.v;
const offset = num - ramp.v;
const scale = offset / range;
Expand All @@ -47,23 +42,7 @@ export class ColorRamp {
}
}

// Stolen from https://github.com/andrewharvey/srtm-stylesheets/blob/master/stylesheets/color-ramps/srtm-Australia-color-ramp.gdaldem.txt
export const ramp = new ColorRamp(
`nv 0 0 0 0
-8764 0 0 0 255
-4000 3 45 85 255
-100 0 101 199 255
0 192 224 255 255
1 108 220 108 255
55 50 180 50 255
390 240 250 150 255
835 190 185 135 255
1114 180 128 107 255
1392 235 220 220 255
4000 215 244 244 255
7000 255 255 255 255`,
-9999,
);
export const ramp = new ColorRamp(DefaultColorRamp);

export const PipelineColorRamp: Pipeline = {
type: 'color-ramp',
Expand Down

0 comments on commit 2c7b8e5

Please sign in to comment.