# All files / fast-colors/src color-scale.ts

43.01% Statements 40/93
40.85% Branches 29/71
62.5% Functions 5/8
42.05% Lines 37/88

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
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222

    466x

    466x

    1668x

    1668x
    312x
    1356x
    312x

    1044x
    1044x
    5328x
    2756x

    1044x
    1044x

    1044x

    1044x

    154x

    154x

    154x

    154x
    462x

    160x

    154x

    154x
    151x

    154x
    151x

    154x
    154x
    154x
    462x

    154x

    466x
    968x
    968x

    968x

    968x
    968x

     import { contrastRatio } from "./color-converters";
    import { ColorInterpolationSpace, interpolateByColorSpace } from "./color-interpolation";
    import { ColorRGBA64 } from "./color-rgba-64";

    /**
     * @public
     */
    export interface ColorScaleStop {
        color: ColorRGBA64;
        position: number;
    }

    /**
     * A color scale created from linear stops
     * @public
     */
    export class ColorScale {
        public static createBalancedColorScale(colors: ColorRGBA64[]): ColorScale {
            if (colors == null || colors.length === 0) {
                throw new Error("The colors argument must be non-empty");
            }

            const stops: ColorScaleStop[] = new Array(colors.length);
            for (let i: number = 0; i < colors.length; i++) {
                // Special case first and last in order to avoid floating point jaggies
                if (i === 0) {
                    stops[i] = { color: colors[i], position: 0 };
                } else if (i === colors.length - 1) {
                    stops[i] = { color: colors[i], position: 1 };
                } else {
                    stops[i] = {
                        color: colors[i],
                        position: i * (1 / (colors.length - 1)),
                    };
                }
            }

            return new ColorScale(stops);
        }

        constructor(stops: ColorScaleStop[]) {
            Iif (stops == null || stops.length === 0) {
                throw new Error("The stops argument must be non-empty");
            } else {
                this.stops = this.sortColorScaleStops(stops);
            }
        }

        private readonly stops: ColorScaleStop[];

        public getColor(
            position: number,
            interpolationMode: ColorInterpolationSpace = ColorInterpolationSpace.RGB
        ): ColorRGBA64 {
            Iif (this.stops.length === 1) {
                return this.stops[0].color;
            } else if (position <= 0) {
                return this.stops[0].color;
            } else if (position >= 1) {
                return this.stops[this.stops.length - 1].color;
            }

            let lowerIndex: number = 0;
            for (let i: number = 0; i < this.stops.length; i++) {
                if (this.stops[i].position <= position) {
                    lowerIndex = i;
                }
            }
            let upperIndex: number = lowerIndex + 1;
            Iif (upperIndex >= this.stops.length) {
                upperIndex = this.stops.length - 1;
            }

            const scalePosition: number =
                (position - this.stops[lowerIndex].position) *
                (1.0 / (this.stops[upperIndex].position - this.stops[lowerIndex].position));

            return interpolateByColorSpace(
                scalePosition,
                interpolationMode,
                this.stops[lowerIndex].color,
                this.stops[upperIndex].color
            );
        }

        public trim(
            lowerBound: number,
            upperBound: number,
            interpolationMode: ColorInterpolationSpace = ColorInterpolationSpace.RGB
        ): ColorScale {
            Iif (lowerBound < 0 || upperBound > 1 || upperBound < lowerBound) {
                throw new Error("Invalid bounds");
            }
            Iif (lowerBound === upperBound) {
                return new ColorScale([
                    { color: this.getColor(lowerBound, interpolationMode), position: 0 },
                ]);
            }

            const containedStops: ColorScaleStop[] = [];

            for (let i: number = 0; i < this.stops.length; i++) {
                if (
                    this.stops[i].position >= lowerBound &&
                    this.stops[i].position <= upperBound
                ) {
                    containedStops.push(this.stops[i]);
                }
            }

            Iif (containedStops.length === 0) {
                return new ColorScale([
                    { color: this.getColor(lowerBound), position: lowerBound },
                    { color: this.getColor(upperBound), position: upperBound },
                ]);
            }

            if (containedStops[0].position !== lowerBound) {
                containedStops.unshift({
                    color: this.getColor(lowerBound),
                    position: lowerBound,
                });
            }
            if (containedStops[containedStops.length - 1].position !== upperBound) {
                containedStops.push({
                    color: this.getColor(upperBound),
                    position: upperBound,
                });
            }

            const range: number = upperBound - lowerBound;
            const finalStops: ColorScaleStop[] = new Array(containedStops.length);
            for (let i: number = 0; i < containedStops.length; i++) {
                finalStops[i] = {
                    color: containedStops[i].color,
                    position: (containedStops[i].position - lowerBound) / range,
                };
            }

            return new ColorScale(finalStops);
        }

        public findNextColor(
            position: number,
            contrast: number,
            searchDown: boolean = false,
            interpolationMode: ColorInterpolationSpace = ColorInterpolationSpace.RGB,
            contrastErrorMargin: number = 0.005,
            maxSearchIterations: number = 32
        ): number {
            if (isNaN(position) || position <= 0) {
                position = 0;
            } else if (position >= 1) {
                position = 1;
            }
            const startingColor: ColorRGBA64 = this.getColor(position, interpolationMode);
            const finalPosition: number = searchDown ? 0 : 1;
            const finalColor: ColorRGBA64 = this.getColor(finalPosition, interpolationMode);
            const finalContrast: number = contrastRatio(startingColor, finalColor);
            if (finalContrast <= contrast) {
                return finalPosition;
            }

            let testRangeMin: number = searchDown ? 0 : position;
            let testRangeMax: number = searchDown ? position : 0;
            let mid: number = finalPosition;
            let iterations: number = 0;

            while (iterations <= maxSearchIterations) {
                mid = Math.abs(testRangeMax - testRangeMin) / 2 + testRangeMin;
                const midColor: ColorRGBA64 = this.getColor(mid, interpolationMode);
                const midContrast: number = contrastRatio(startingColor, midColor);

                if (Math.abs(midContrast - contrast) <= contrastErrorMargin) {
                    return mid;
                } else if (midContrast > contrast) {
                    if (searchDown) {
                        testRangeMin = mid;
                    } else {
                        testRangeMax = mid;
                    }
                } else {
                    if (searchDown) {
                        testRangeMax = mid;
                    } else {
                        testRangeMin = mid;
                    }
                }

                iterations++;
            }

            return mid;
        }

        public clone(): ColorScale {
            const newStops: ColorScaleStop[] = new Array(this.stops.length);
            for (let i: number = 0; i < newStops.length; i++) {
                newStops[i] = {
                    color: this.stops[i].color,
                    position: this.stops[i].position,
                };
            }
            return new ColorScale(newStops);
        }

        private sortColorScaleStops(stops: ColorScaleStop[]): ColorScaleStop[] {
            return stops.sort((a: ColorScaleStop, b: ColorScaleStop): number => {
                const A: number = a.position;
                const B: number = b.position;

                Iif (A < B) {
                    return -1;
                } else Eif (A > B) {
                    return 1;
                } else {
                    return 0;
                }
            });
        }
    }