Skip to content

Commit

Permalink
feat: improve color tiles (#497)
Browse files Browse the repository at this point in the history
  • Loading branch information
fufeck authored Jan 13, 2025
1 parent c019203 commit ddbff61
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as turf from '@turf/turf';
import { Feature as FeatureTurf } from '@turf/helpers';
import * as randomColor from 'randomcolor';
import { FeatureCollection } from 'geojson';

import { Numero } from '@/shared/entities/numero.entity';
Expand All @@ -10,7 +9,7 @@ import { getPriorityPosition } from '@/lib/utils/positions.util';
import { GeoJsonCollectionType } from '@/modules/base_locale/sub_modules/tiles/types/features.type';

// Paul Tol's vibrant palette for accessibility
const colorblindFriendlyHues = [
const colorblindFriendlyPalette = [
'#EE7733',
'#0077BB',
'#33BBEE',
Expand All @@ -19,23 +18,25 @@ const colorblindFriendlyHues = [
'#009988',
];

function getColorById(id: string): string {
return randomColor({
luminosity: 'dark',
seed: id,
});
}

// Returns a color of the palette based on the bal ID
function getColorblindFriendlyHue(id: string): string {
const slicedId = id.slice(16).replace(/-/g, '');
return colorblindFriendlyHues[
Number.parseInt(slicedId, 16) % colorblindFriendlyHues.length
];
}
const colorPalette = [
'#8B1A1A',
'#1874CD',
'#00875A',
'#E6C200',
'#805BA5',
'#D47300',
'#36B3B3',
'#D1127A',
];

function getFeatureColor(id: string, colorblindMode: boolean = false): string {
return colorblindMode ? getColorblindFriendlyHue(id) : getColorById(id);
const slicedId = id.slice(16).replace(/-/g, '');
if (colorblindMode) {
return colorblindFriendlyPalette[
Number.parseInt(slicedId, 16) % colorblindFriendlyPalette.length
];
}
return colorPalette[Number.parseInt(slicedId, 16) % colorPalette.length];
}

function numeroToPointFeature(n: Numero, colorblindMode: boolean): FeatureTurf {
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
"nodemailer": "^6.9.9",
"pg": "^8.11.5",
"pumpify": "^2.0.1",
"randomcolor": "^0.6.2",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1",
"ts-loader": "^9.4.3",
Expand Down Expand Up @@ -145,4 +144,4 @@
"engines": {
"node": "22"
}
}
}
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8893,11 +8893,6 @@ randombytes@^2.1.0:
dependencies:
safe-buffer "^5.1.0"

randomcolor@^0.6.2:
version "0.6.2"
resolved "https://registry.yarnpkg.com/randomcolor/-/randomcolor-0.6.2.tgz#7a57362ae1a1278439aeed2c15e5deb8ea33f56d"
integrity sha512-Mn6TbyYpFgwFuQ8KJKqf3bqqY9O1y37/0jgSK/61PUxV4QfIMv0+K2ioq8DfOjkBslcjwSzRfIDEXfzA9aCx7A==

range-parser@~1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/range-parser/-/range-parser-1.2.1.tgz#3cf37023d199e1c24d1a55b84800c2f3e6468031"
Expand Down

0 comments on commit ddbff61

Please sign in to comment.