# All files / fast-colors/src palette-extractor.ts

97.44% Statements 38/39
94.12% Branches 16/17
100% Functions 1/1
97.06% Lines 33/34

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

    4x

    2x

    2x
    2x
    2x
    88x
    88x

    2x
    2x
    2x
    12x
    12x

    2x

    2x
    88x
    88x

    528x

    125x
    125x

    125x

    125x

    125x

    125x

    39x
    39x
    124x

    6x
    6x

    39x
    33x
    33x
    33x

    2x

     import { QuantizedColor } from "./color-quantization";
    import { ColorHSL } from "./color-hsl";
    import { rgbToHSL } from "./color-converters";

    /**
     * @public
     */
    export interface PaletteEntryConstraint {
        id: string;
        targetSaturation: number;
        minSaturation: number;
        maxSaturation: number;
        targetLuminosity: number;
        minLuminosity: number;
        maxLuminosity: number;
    }

    /**
     * @public
     */
    export interface PaletteEntry {
        found: boolean;
        constraint: PaletteEntryConstraint;
        color?: QuantizedColor;
    }

    /**
     * Configuration structure for palette extraction.
     * @public
     */
    export interface PaletteExtractionConfig {
        saturationWeight: number;
        luminosityWeight: number;
        populationWeight: number;
        volumeWeight: number;
        constraints: PaletteEntryConstraint[];
    }

    /**
     * The default configuration for palette extraction.
     * @public
     */
    export const defaultPaletteExtractionConfig: PaletteExtractionConfig = {
        saturationWeight: 6,
        luminosityWeight: 3,
        populationWeight: 1,
        volumeWeight: 0.1,
        constraints: [
            {
                id: "Vibrant",
                targetSaturation: 1,
                minSaturation: 0.35,
                maxSaturation: 1,
                targetLuminosity: 0.5,
                minLuminosity: 0.3,
                maxLuminosity: 0.7,
            },
            {
                id: "LightVibrant",
                targetSaturation: 1,
                minSaturation: 0.35,
                maxSaturation: 1,
                targetLuminosity: 0.74,
                minLuminosity: 0.55,
                maxLuminosity: 1,
            },
            {
                id: "DarkVibrant",
                targetSaturation: 1,
                minSaturation: 0.35,
                maxSaturation: 1,
                targetLuminosity: 0.26,
                minLuminosity: 0,
                maxLuminosity: 0.45,
            },
            {
                id: "Muted",
                targetSaturation: 0.3,
                minSaturation: 0,
                maxSaturation: 0.4,
                targetLuminosity: 0.5,
                minLuminosity: 0.3,
                maxLuminosity: 0.7,
            },
            {
                id: "LightMuted",
                targetSaturation: 0.3,
                minSaturation: 0,
                maxSaturation: 0.4,
                targetLuminosity: 0.74,
                minLuminosity: 0.55,
                maxLuminosity: 1,
            },
            {
                id: "DarkMuted",
                targetSaturation: 0.3,
                minSaturation: 0,
                maxSaturation: 0.4,
                targetLuminosity: 0.26,
                minLuminosity: 0,
                maxLuminosity: 0.45,
            },
        ],
    };

    /**
     * Extracts a palette.
     * @param colors - the quantized colors
     * @param config - the extraction config
     * @public
     */
    export function extractPalette(
        colors: QuantizedColor[],
        config: PaletteExtractionConfig = defaultPaletteExtractionConfig
    ): PaletteEntry[] {
        Iif (config.constraints.length === 0) {
            return [];
        }

        let totalPixelCount: number = 0;
        let totalVolume: number = 0;
        for (let i: number = 0; i < colors.length; i++) {
            totalPixelCount += colors[i].pixelCount;
            totalVolume += colors[i].colorVolume;
        }

        const retVal: PaletteEntry[] = new Array(config.constraints.length);
        const bestFitValues: number[] = new Array(config.constraints.length);
        for (let i: number = 0; i < retVal.length; i++) {
            bestFitValues[i] = 0;
            retVal[i] = {
                found: false,
                constraint: config.constraints[i],
            };
        }

        const totalWeight: number =
            config.saturationWeight +
            config.luminosityWeight +
            config.populationWeight +
            config.volumeWeight;
        for (let i: number = 0; i < colors.length; i++) {
            const hsl: ColorHSL = rgbToHSL(colors[i].color);
            for (let j: number = 0; j < config.constraints.length; j++) {
                // Check for min and max saturation / luminosity
                if (
                    hsl.s >= config.constraints[j].minSaturation &&
                    hsl.s <= config.constraints[j].maxSaturation &&
                    hsl.l >= config.constraints[j].minLuminosity &&
                    hsl.l <= config.constraints[j].maxLuminosity
                ) {
                    const populationFactor: number = colors[i].pixelCount / totalPixelCount;
                    const volumeFactor: number = colors[i].colorVolume / totalVolume;
                    const saturationFactor: number =
                        1 - Math.abs(hsl.s - config.constraints[j].targetSaturation);
                    const luminosityFactor: number =
                        1 - Math.abs(hsl.l - config.constraints[j].targetLuminosity);
                    const fitValue: number =
                        (populationFactor * config.populationWeight +
                            volumeFactor * config.volumeWeight +
                            saturationFactor * config.saturationWeight +
                            luminosityFactor * config.luminosityWeight) /
                        totalWeight;

                    if (fitValue > bestFitValues[j]) {
                        // Check if this color is already in use
                        let dupe: boolean = false;
                        for (let k: number = 0; k < j; k++) {
                            if (
                                retVal[k].found &&
                                retVal[k].color!.color.equalValue(colors[i].color)
                            ) {
                                dupe = true;
                                break;
                            }
                        }

                        if (!dupe) {
                            bestFitValues[j] = fitValue;
                            retVal[j].found = true;
                            retVal[j].color = colors[i];
                        }
                    }
                }
            }
        }

        return retVal;
    }