https://dev.kronoz.win/r/marzneshin.git
1/**
2 * @module Color range generator
3 *
4 */
5
6function calculatePoint(i: number, intervalSize: number, colorRangeInfo: ColorRange) {
7 const { colorStart, colorEnd, useEndAsStart } = colorRangeInfo;
8 return (useEndAsStart
9 ? (colorEnd - (i * intervalSize))
10 : (colorStart + (i * intervalSize)));
11}
12
13/* Must use an interpolated color scale, which has a range of [0, 1] */
14interface ColorRange {
15 colorStart: number;
16 colorEnd: number;
17 useEndAsStart: boolean;
18}
19
20/**
21 * @param dataLength - Number of colors to be generated
22 * @param colorScale - The D3 interpolate color scale
23 * @param colorRangeInfo - Color range spectrum
24 * @returns An array of colors within the choosen spectrum
25 *
26 * @description
27 * - Must use an interpolated color scale, which has a range of [0, 1].
28 * - D3 Interpolators are interpolateInfero, interpolateCool, interpolateRainbow, interpolateSpectral, interpolateRdYlBu
29 */
30export function interpolateColors(dataLength: number, colorScale: (c: number) => string, colorRangeInfo: ColorRange) {
31 const { colorStart, colorEnd } = colorRangeInfo;
32 const colorRange = colorEnd - colorStart;
33 const intervalSize = colorRange / dataLength;
34 let i, colorPoint;
35 const colorArray = [];
36
37 for (i = 0; i < dataLength; i++) {
38 colorPoint = calculatePoint(i, intervalSize, colorRangeInfo);
39 colorArray.push(colorScale(colorPoint));
40 }
41
42 return colorArray;
43}