-
Notifications
You must be signed in to change notification settings - Fork 130
added isochrone color palettes and opacity control #340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+228
−15
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7e3b545
added isochrone color palettes and opacity control
s4chinjha 79c71e8
Merge branch 'master' into isochrone-settings
s4chinjha 27fd413
fix comments,palette logic & added tests
s4chinjha abc3a9c
remove unnecessary comments
s4chinjha f345a75
Merge branch 'master' into isochrone-settings
nilsnolde File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
| import { getPaletteColor, DEFAULT_FILL } from './isochrone-palettes'; | ||
|
|
||
| const BINARY = ['#000000', '#ffffff']; | ||
|
|
||
| const VIRIDIS = ['#440154', '#3b528b', '#21918c', '#5ec962', '#fde725']; | ||
|
|
||
| describe('getPaletteColor', () => { | ||
| it('returns DEFAULT_FILL for an empty palette', () => { | ||
| expect(getPaletteColor([], 0)).toBe(DEFAULT_FILL); | ||
| }); | ||
|
|
||
| it('returns the only color when the palette has one entry', () => { | ||
| expect(getPaletteColor(['#aabbcc'], 0.5)).toBe('#aabbcc'); | ||
| }); | ||
|
|
||
| it('maps t=1 (largest contour) to the palette end', () => { | ||
| expect(getPaletteColor(VIRIDIS, 1)).toBe('#fde725'); | ||
| expect(getPaletteColor(BINARY, 1)).toBe('#ffffff'); | ||
| }); | ||
|
|
||
| it('maps t=0 to the palette start', () => { | ||
| expect(getPaletteColor(VIRIDIS, 0)).toBe('#440154'); | ||
| expect(getPaletteColor(BINARY, 0)).toBe('#000000'); | ||
| }); | ||
|
|
||
| it('is stable: same t always produces the same color', () => { | ||
| const t = 30 / 30; | ||
| expect(getPaletteColor(VIRIDIS, t)).toBe('#fde725'); | ||
|
|
||
| const tMid = 10 / 30; | ||
| expect(getPaletteColor(VIRIDIS, tMid)).toBe(getPaletteColor(VIRIDIS, tMid)); | ||
| }); | ||
|
|
||
| it('returns exact palette colors at t=0 and t=1', () => { | ||
| expect(getPaletteColor(BINARY, 0)).toBe('#000000'); | ||
| expect(getPaletteColor(BINARY, 1)).toBe('#ffffff'); | ||
| }); | ||
|
|
||
| it('interpolates correctly at t=0.5 on a two-stop palette', () => { | ||
| expect(getPaletteColor(BINARY, 0.5)).toBe('#808080'); | ||
| }); | ||
|
|
||
| it('largest contour is always palette end across different maxRange scenarios', () => { | ||
| expect(getPaletteColor(VIRIDIS, 30 / 30)).toBe('#fde725'); | ||
| expect(getPaletteColor(VIRIDIS, 60 / 60)).toBe('#fde725'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| export type PaletteId = 'default' | 'viridis' | 'plasma' | 'blues'; | ||
|
|
||
| export const DEFAULT_FILL = '#6200ea'; | ||
|
|
||
| export const DEFAULT_OPACITY = 0.4; | ||
|
|
||
| export interface IsochronePalette { | ||
| id: PaletteId; | ||
| label: string; | ||
| colors: string[] | null; | ||
| } | ||
|
|
||
| export const ISOCHRONE_PALETTES: IsochronePalette[] = [ | ||
| { | ||
| id: 'default', | ||
| label: 'Default', | ||
| colors: null, | ||
| }, | ||
| { | ||
| id: 'viridis', | ||
| label: 'Viridis (colorblind-friendly)', | ||
| colors: ['#440154', '#3b528b', '#21918c', '#5ec962', '#fde725'], | ||
| }, | ||
| { | ||
| id: 'plasma', | ||
| label: 'Plasma', | ||
| colors: ['#0d0887', '#7e03a8', '#cc4778', '#f89540', '#f0f921'], | ||
| }, | ||
| { | ||
| id: 'blues', | ||
| label: 'Blues', | ||
| colors: ['#084594', '#2171b5', '#4292c6', '#9ecae1', '#deebf7'], | ||
| }, | ||
| ]; | ||
|
|
||
| export function isPaletteId(value: string): value is PaletteId { | ||
| return ISOCHRONE_PALETTES.some((p) => p.id === value); | ||
| } | ||
|
|
||
| function interpolateHex(c1: string, c2: string, t: number): string { | ||
| const r1 = parseInt(c1.slice(1, 3), 16); | ||
| const g1 = parseInt(c1.slice(3, 5), 16); | ||
| const b1 = parseInt(c1.slice(5, 7), 16); | ||
| const r2 = parseInt(c2.slice(1, 3), 16); | ||
| const g2 = parseInt(c2.slice(3, 5), 16); | ||
| const b2 = parseInt(c2.slice(5, 7), 16); | ||
| const r = Math.round(r1 + (r2 - r1) * t); | ||
| const g = Math.round(g1 + (g2 - g1) * t); | ||
| const b = Math.round(b1 + (b2 - b1) * t); | ||
| return `#${r.toString(16).padStart(2, '0')}${g.toString(16).padStart(2, '0')}${b.toString(16).padStart(2, '0')}`; | ||
| } | ||
|
|
||
| export function getPaletteColor(colors: string[], t: number): string { | ||
| if (colors.length === 0) return DEFAULT_FILL; | ||
| if (colors.length === 1) return colors[0]!; | ||
| const rawIdx = t * (colors.length - 1); | ||
| const lower = Math.floor(rawIdx); | ||
| const upper = Math.min(lower + 1, colors.length - 1); | ||
| if (lower === upper) return colors[lower]!; | ||
| return interpolateHex(colors[lower]!, colors[upper]!, rawIdx - lower); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.