From d7c1b7baa665e4de154bb03d50b502f7a331cc36 Mon Sep 17 00:00:00 2001 From: Michael Puehringer Date: Thu, 11 Jan 2024 10:33:24 +0100 Subject: [PATCH] feat: add support for custom color palettes --- src/tracks/gosling-track/gosling-track-model.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/tracks/gosling-track/gosling-track-model.ts b/src/tracks/gosling-track/gosling-track-model.ts index a04c15ce..813609ab 100644 --- a/src/tracks/gosling-track/gosling-track-model.ts +++ b/src/tracks/gosling-track/gosling-track-model.ts @@ -794,11 +794,19 @@ export class GoslingTrackModel { break; case 'color': case 'stroke': - let interpolate = interpolateViridis; + if (Array.isArray(range) && range.every(d => typeof d === 'string')) { + // Support for custom color palettes, i.e. ["green", "red", "blue"] + this.channelScales[channelKey] = scaleLinear(range as string[]).domain( + scaleLinear(domain as [number, number]).ticks(range.length) + ); + break; + } + let interpolated = interpolateViridis; + if (Object.keys(PREDEFINED_COLOR_STR_MAP).includes(range as string)) { - interpolate = PREDEFINED_COLOR_STR_MAP[range as string]; + interpolated = PREDEFINED_COLOR_STR_MAP[range as string]; } - this.channelScales[channelKey] = scaleSequential(interpolate).domain( + this.channelScales[channelKey] = scaleSequential(interpolated).domain( domain as [number, number] ); break;