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