All files / owid-grapher/grapher/color ColorScheme.ts

75.82% Statements 69/91
80% Branches 12/15
80% Functions 4/5
75.82% Lines 69/91

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 1321x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 1x 1x                                     1x 1x 3x 3x 3x 3x 3x 3x 6x 6x 3x 3x 1x 1x 4x 4x 4x 1x 1x 1x 1x 4x 1x 1x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x         1x 1x 1x 21x 1x 1x                                                                                  
import { Color } from "../../coreTable/CoreTableConstants"
import { rgb } from "d3-color"
import { interpolate } from "d3-interpolate"
import { lastOfNonEmptyArray, clone } from "../../clientUtils/Util"
import { ColorSchemeInterface } from "./ColorConstants"
import { interpolateArray } from "./ColorUtils"
 
export class ColorScheme implements ColorSchemeInterface {
    name: string
    colorSets: Color[][]
    singleColorScale: boolean
    isDistinct: boolean
 
    constructor(
        name: string,
        colorSets: Color[][],
        singleColorScale?: boolean,
        isDistinct?: boolean
    ) {
        this.name = name
        this.colorSets = []
        this.singleColorScale = !!singleColorScale
        this.isDistinct = !!isDistinct
        colorSets.forEach((set) => (this.colorSets[set.length] = set))
    }
 
    improviseGradientFromShorter(
        shortColors: Color[],
        numColors: number
    ): Color[] {
        const newColors = clone(shortColors)

        while (newColors.length < numColors) {
            for (let index = newColors.length - 1; index > 0; index -= 1) {
                const startColor = rgb(newColors[index - 1])
                const endColor = rgb(newColors[index])
                const newColor = interpolate(startColor, endColor)(0.5)
                newColors.splice(index, 0, newColor)

                if (newColors.length >= numColors) break
            }
        }

        return newColors
    }
 
    improviseGradientFromLonger(
        knownColors: Color[],
        numColors: number
    ): Color[] {
        const newColors = []
        const scale = interpolateArray(knownColors)
        for (let index = 0; index < numColors; index++) {
            newColors.push(scale(index / numColors))
        }
        return newColors
    }
 
    getGradientColors(numColors: number): Color[] {
        const { colorSets } = this
 
        if (colorSets[numColors]) return clone(colorSets[numColors])
 
        const prevColors = clone(colorSets)
            .reverse()
            .find((set) => set && set.length < numColors)
        if (prevColors)
            return this.improviseGradientFromShorter(prevColors, numColors)
        else
            return this.improviseGradientFromLonger(
                colorSets.find((set) => !!set) as Color[],
                numColors
            )
    }
 
    getDistinctColors(numColors: number): Color[] {
        const { colorSets } = this
        if (colorSets.length === 0) return []
        if (colorSets[numColors]) return clone(colorSets[numColors])
 
        if (numColors > colorSets.length - 1) {
            // If more colors are wanted than we have defined, have to improvise
            const colorSet = lastOfNonEmptyArray(colorSets)
            return this.improviseGradientFromShorter(colorSet, numColors)
        }
 
        // We have enough colors but not a specific set for this number-- improvise from the closest longer set
        for (let i = numColors; i < colorSets.length; i++) {
            if (colorSets[i]) {
                return colorSets[i].slice(0, numColors)
            }
        }
 
        return []
    }
 
    getColors(numColors: number): Color[] {
        return this.isDistinct
            ? this.getDistinctColors(numColors)
            : this.getGradientColors(numColors)
    }
 
    getUniqValueColorMap(
        uniqValues: any[],
        inverseOrder?: boolean
    ): Map<number, string> {
        const colors = this.getColors(uniqValues.length) || []
        if (inverseOrder) colors.reverse()
 
        // We want to display same values using the same color, e.g. two values of 100 get the same shade of green
        // Therefore, we create a map from all possible (unique) values to the corresponding color
        const colorByValue = new Map<number, Color>()
        uniqValues.forEach((value, index) =>
            colorByValue.set(value, colors[index])
        )
        return colorByValue
    }
 
    static fromObject(
        name: string,
        colorSets: { [key: string]: Color[] },
        singleColorScale?: boolean
    ): ColorScheme {
        const colorSetsArray: Color[][] = []
        Object.keys(colorSets).forEach(
            (numColors): string[] =>
                (colorSetsArray[+numColors] = colorSets[numColors])
        )
        return new ColorScheme(name, colorSetsArray, singleColorScale)
    }
}