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

89.16% Statements 74/83
71.05% Branches 27/38
100% Functions 13/13
89.16% Lines 74/83

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
    223
    224
    225
    226
    227
    228
    229
    230
    231
    2324x
    4x
    4x

    4x

    4x

    4x

    4x

    4x
    897x

    4x
    758x

    4x
    751x

    4x
    451x

    4x
    311x

    4x
    148x

    4x
    8502x

    8502x

    8502x

    8502x
    4096x
    4096x
    4096x

    4096x

    8502x

    8502x

    8502x

    4x
    131368x

    131368x

    131368x

    131368x
    65536x
    65536x
    65536x
    65536x

    65536x

    131368x

    131368x

    131368x

    4x
    131220x

    131220x

    131220x

    131220x
    65536x
    65536x
    65536x
    65536x

    65536x

    131220x

    131220x

    131220x

    4x
    296x

    296x

    296x

    296x

    4x
    296x

    296x

    296x

    296x
    296x

    4x

    296x

    296x

    4x
    888x

    888x

     import { ColorRGBA64 } from "./color-rgba-64";
    import { normalize } from "./math-utilities";
    import { NamedColors, namedColorsConfigs } from "./named-colors";

    // Matches rgb(R, G, B) where R, G, and B are integers [0 - 255]
    const webRGBRegex: RegExp = /^rgb\(\s*((?:(?:25[0-5]|2[0-4]\d|1\d\d|\d{1,2})\s*,\s*){2}(?:25[0-5]|2[0-4]\d|1\d\d|\d{1,2})\s*)\)$/i;
    // Matches rgb(R, G, B, A) where R, G, and B are integers [0 - 255] and A is [0-1] floating
    const webRGBARegex: RegExp = /^rgba\(\s*((?:(?:25[0-5]|2[0-4]\d|1\d\d|\d{1,2})\s*,\s*){3}(?:0|1|0?\.\d*)\s*)\)$/i;
    // Matches #RGB and #RRGGBB, where R, G, and B are [0-9] or [A-F]
    const hexRGBRegex: RegExp = /^#((?:[0-9a-f]{6}|[0-9a-f]{3}))$/i;
    // Matches #RGB and #RRGGBBAA, where R, G, B, and A are [0-9] or [A-F]
    const hexRGBARegex: RegExp = /^#((?:[0-9a-f]{8}|[0-9a-f]{4}))$/i;

    /**
     * Test if a color matches #RRGGBB or #RGB
     */
    export function isColorStringHexRGB(raw: string): boolean {
        return hexRGBRegex.test(raw);
    }

    /**
     * Test if a color matches #AARRGGBB or #ARGB
     */
    export function isColorStringHexARGB(raw: string): boolean {
        return hexRGBARegex.test(raw);
    }

    /**
     * Test if a color matches #RRGGBBAA or #RGBA
     */
    export function isColorStringHexRGBA(raw: string): boolean {
        return isColorStringHexARGB(raw); // No way to differentiate these two formats, so just use the same test
    }

    /**
     * Test if a color matches rgb(rr, gg, bb)
     */
    export function isColorStringWebRGB(raw: string): boolean {
        return webRGBRegex.test(raw);
    }

    /**
     * Test if a color matches rgba(rr, gg, bb, aa)
     */
    export function isColorStringWebRGBA(raw: string): boolean {
        return webRGBARegex.test(raw);
    }

    export function isColorNamed(raw: string | NamedColors): raw is NamedColors {
        return namedColorsConfigs.hasOwnProperty(raw);
    }

    // Expects format #RRGGBB or #RGB
    export function parseColorHexRGB(raw: string): ColorRGBA64 | null {
        const result: string[] | null = hexRGBRegex.exec(raw);

        Iif (result === null) {
            return null;
        }

        let digits: string = result[1];

        if (digits.length === 3) {
            const r: string = digits.charAt(0);
            const g: string = digits.charAt(1);
            const b: string = digits.charAt(2);

            digits = r.concat(r, g, g, b, b);
        }

        const rawInt: number = parseInt(digits, 16);

        Iif (isNaN(rawInt)) {
            return null;
        }

        // Note the use of >>> rather than >> as we want JS to manipulate these as unsigned numbers
        return new ColorRGBA64(
            normalize((rawInt & 0xff0000) >>> 16, 0, 255),
            normalize((rawInt & 0x00ff00) >>> 8, 0, 255),
            normalize(rawInt & 0x0000ff, 0, 255),
            1
        );
    }

    // Expects format #AARRGGBB
    export function parseColorHexARGB(raw: string): ColorRGBA64 | null {
        const result: string[] | null = hexRGBARegex.exec(raw);

        Iif (result === null) {
            return null;
        }

        let digits: string = result[1];

        if (digits.length === 4) {
            const a: string = digits.charAt(0);
            const r: string = digits.charAt(1);
            const g: string = digits.charAt(2);
            const b: string = digits.charAt(3);

            digits = a.concat(a, r, r, g, g, b, b);
        }

        const rawInt: number = parseInt(digits, 16);

        Iif (isNaN(rawInt)) {
            return null;
        }

        // Note the use of >>> rather than >> as we want JS to manipulate these as unsigned numbers
        return new ColorRGBA64(
            normalize((rawInt & 0x00ff0000) >>> 16, 0, 255),
            normalize((rawInt & 0x0000ff00) >>> 8, 0, 255),
            normalize(rawInt & 0x000000ff, 0, 255),
            normalize((rawInt & 0xff000000) >>> 24, 0, 255)
        );
    }

    // Expects format #RRGGBBAA or #RGBA
    export function parseColorHexRGBA(raw: string): ColorRGBA64 | null {
        const result: string[] | null = hexRGBARegex.exec(raw);

        Iif (result === null) {
            return null;
        }

        let digits: string = result[1];

        if (digits.length === 4) {
            const r: string = digits.charAt(0);
            const g: string = digits.charAt(1);
            const b: string = digits.charAt(2);
            const a: string = digits.charAt(3);

            digits = r.concat(r, g, g, b, b, a, a);
        }

        const rawInt: number = parseInt(digits, 16);

        Iif (isNaN(rawInt)) {
            return null;
        }

        // Note the use of >>> rather than >> as we want JS to manipulate these as unsigned numbers
        return new ColorRGBA64(
            normalize((rawInt & 0xff000000) >>> 24, 0, 255),
            normalize((rawInt & 0x00ff0000) >>> 16, 0, 255),
            normalize((rawInt & 0x0000ff00) >>> 8, 0, 255),
            normalize(rawInt & 0x000000ff, 0, 255)
        );
    }

    // Expects format rgb(RR,GG,BB) where RR,GG,BB are [0,255]
    export function parseColorWebRGB(raw: string): ColorRGBA64 | null {
        const result: string[] | null = webRGBRegex.exec(raw);

        Iif (result === null) {
            return null;
        }

        const split: string[] = result[1].split(",");

        return new ColorRGBA64(
            normalize(Number(split[0]), 0, 255),
            normalize(Number(split[1]), 0, 255),
            normalize(Number(split[2]), 0, 255),
            1
        );
    }

    // Expects format rgba(RR,GG,BB,a) where RR,GG,BB are [0,255] and a is [0,1]
    export function parseColorWebRGBA(raw: string): ColorRGBA64 | null {
        const result: string[] | null = webRGBARegex.exec(raw);

        Iif (result === null) {
            return null;
        }

        const split: string[] = result[1].split(",");

        Eif (split.length === 4) {
            return new ColorRGBA64(
                normalize(Number(split[0]), 0, 255),
                normalize(Number(split[1]), 0, 255),
                normalize(Number(split[2]), 0, 255),
                Number(split[3])
            );
        }
        return null;
    }

    // Expects any of the CSS color names https://www.w3schools.com/colors/colors_names.asp
    export function parseColorNamed(
        raw: keyof typeof namedColorsConfigs
    ): ColorRGBA64 | null {
        // const rawLower: typeof raw =  raw.toLowerCase() : raw.toString();
        const config: typeof namedColorsConfigs[keyof typeof namedColorsConfigs] | void =
            namedColorsConfigs[raw.toLowerCase()];

        return config
            ? new ColorRGBA64(
                  config.r,
                  config.g,
                  config.b,
                  config.hasOwnProperty("a") ? config.a : void 0
              )
            : null;
    }

    // Expects any of the following and attempts to determine which is being used
    // #RRGGBB
    // #AARRGGBB
    // rgb(RR,GG,BB)
    // rgba(RR,GG,BB,a)
    // or any of the CSS color names https://www.w3schools.com/colors/colors_names.asp
    export function parseColor(raw: string): ColorRGBA64 | null {
        const rawLower: string = raw.toLowerCase();

        return isColorStringHexRGB(rawLower)
            ? parseColorHexRGB(rawLower)
            : isColorStringHexRGBA(rawLower)
            ? parseColorHexARGB(rawLower)
            : isColorStringWebRGB(rawLower)
            ? parseColorWebRGB(rawLower)
            : isColorStringWebRGBA(rawLower)
            ? parseColorWebRGBA(rawLower)
            : isColorNamed(rawLower)
            ? parseColorNamed(rawLower)
            : null;
    }