import { scaleLinear } from 'd3-scale' ; import { interpolateHcl , interpolateNumber , piecewise , quantize , } from 'd3-interpolate' ; import ColorScheme , { ColorSchemeConfig } from './ColorScheme' ; export interface SequentialSchemeConfig extends ColorSchemeConfig { isDiverging ? : boolean ; } export default class SequentialScheme extends ColorScheme { isDiverging : boolean ; constructor ( config ) { super ( config ) ; const { isDiverging = false } = config ; this . isDiverging = isDiverging ; } param domain param modifyRange createLinearScale ( domain = [ 0 , 1 ] , modifyRange = false ) { const scale = scaleLinear < string > ( ) . interpolate ( interpolateHcl ) . clamp ( true ) ; return modifyRange || domain . length === this . colors . length ? scale . domain ( domain ) . range ( this . getColors ( domain . length ) ) : scale . domain ( quantize ( piecewise ( interpolateNumber , domain ) , this . colors . length ) ) . range ( this . colors ) ; } param numColors param extent getColors ( numColors = this . colors . length , extent = [ 0 , 1 ] ) { if ( numColors === this . colors . length && extent [ 0 ] === 0 && extent [ 1 ] === 1 ) { return this . colors ; } const piecewiseScale = piecewise ( interpolateHcl , this . colors ) ; const adjustExtent = scaleLinear ( ) . range ( extent ) . clamp ( true ) ; return quantize < string > ( t => piecewiseScale ( adjustExtent ( t ) as number ) , numColors ) ; } }