Skip to content
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

Allow arbitrary (hex) colors in color range #1032

Open
puehringer opened this issue Jan 12, 2024 · 0 comments
Open

Allow arbitrary (hex) colors in color range #1032

puehringer opened this issue Jan 12, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@puehringer
Copy link

Currently, only fixed colors or color scales in PREDEFINED_COLOR_STR_MAP are supported. It would be great if we could extend this to support arbitrary range, like ["green", "red", "blue"] or so.

A very simple (and possibly incomplete) implementation in gosling-track-model.ts could look like this:

let interpolate = interpolateViridis;
if (Object.keys(PREDEFINED_COLOR_STR_MAP).includes(range as string)) {
    interpolate = PREDEFINED_COLOR_STR_MAP[range as string];
} else if (Array.isArray(range) && range.every(d => typeof d === 'string')) {
    // Support for custom color palettes, i.e. ["green", "red", "blue"]
    const scaler = scaleLinear(range as string[]).domain(
        // Map the range to [0, 0.5, 1] in the above example
        range.map((_, i, arr) => i / (arr.length - 1))
    );
    interpolate = (t: number) => scaler(t);
}
this.channelScales[channelKey] = scaleSequential(interpolate).domain(
    domain as [number, number]
);
break;

Note that this assume that the domain remains a [number, number], hence we interpolate all colors (even more than 2) linearly between the domain. If we want to support [string, string, string] as range and [number, number, number] as domain, more adjustments are required.

@puehringer puehringer added the enhancement New feature or request label Jan 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant