/
continuousColorScale.ts
112 lines (95 loc) · 3.3 KB
/
continuousColorScale.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { useMemo } from 'react'
import { ScaleDiverging, ScaleQuantize, ScaleSequential, scaleLinear } from 'd3-scale'
import {
SequentialColorScaleConfig,
SequentialColorScaleValues,
getSequentialColorScale,
} from './sequentialColorScale'
import {
DivergingColorScaleConfig,
DivergingColorScaleValues,
getDivergingColorScale,
} from './divergingColorScale'
import {
QuantizeColorScaleConfig,
QuantizeColorScaleValues,
getQuantizeColorScale,
} from './quantizeColorScale'
export type ContinuousColorScaleConfig =
| SequentialColorScaleConfig
| DivergingColorScaleConfig
| QuantizeColorScaleConfig
export type ContinuousColorScaleValues =
| SequentialColorScaleValues
| DivergingColorScaleValues
| QuantizeColorScaleValues
const isSequentialColorScaleConfig = (
config: ContinuousColorScaleConfig
): config is SequentialColorScaleConfig => config.type === 'sequential'
const isDivergingColorScaleConfig = (
config: ContinuousColorScaleConfig
): config is DivergingColorScaleConfig => config.type === 'diverging'
const isQuantizeColorScaleConfig = (
config: ContinuousColorScaleConfig
): config is QuantizeColorScaleConfig => config.type === 'quantize'
export const getContinuousColorScale = <Config extends ContinuousColorScaleConfig>(
config: Config,
values: ContinuousColorScaleValues
) => {
if (isSequentialColorScaleConfig(config)) {
return getSequentialColorScale(config, values)
}
if (isDivergingColorScaleConfig(config)) {
return getDivergingColorScale(config, values)
}
if (isQuantizeColorScaleConfig(config)) {
return getQuantizeColorScale(config, values)
}
throw new Error('Invalid continuous color scale config')
}
export const useContinuousColorScale = (
config: ContinuousColorScaleConfig,
values: ContinuousColorScaleValues
) => useMemo(() => getContinuousColorScale(config, values), [config, values])
export const computeContinuousColorScaleColorStops = (
scale: ScaleSequential<string> | ScaleDiverging<string> | ScaleQuantize<string>,
steps = 16
) => {
const domain = scale.domain()
// quantize
if ('thresholds' in scale) {
const stops: {
key: string
offset: string
stopColor: string
}[] = []
const normalizedScale = scaleLinear().domain(domain).range([0, 1])
scale.range().forEach((color, index) => {
const [start, end] = scale.invertExtent(color)
stops.push({
key: `${index}.0`,
offset: `${Math.round(normalizedScale(start) * 100)}%`,
stopColor: color,
})
stops.push({
key: `${index}.1`,
offset: `${Math.round(normalizedScale(end) * 100)}%`,
stopColor: color,
})
})
return stops
}
const colorStopsScale = scale.copy()
if (domain.length === 2) {
// sequential
colorStopsScale.domain([0, 1])
} else if (domain.length === 3) {
// diverging
colorStopsScale.domain([0, 0.5, 1])
}
return ((colorStopsScale as any).ticks(steps) as number[]).map((value: number) => ({
key: `${value}`,
offset: `${Math.round(value * 100)}%`,
stopColor: `${colorStopsScale(value)}`,
}))
}