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

97.87% Statements 230/235
97.67% Branches 84/86
96% Functions 24/25
97.87% Lines 230/235

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
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    4245x
    5x
    5x
    5x
    5x
    5x
    5x

    5x
    4756x

    5x

    14268x
    6096x

    8172x

    4756x

    5x

    2304x

    5x
    2304x
    2304x
    2304x

    5x

    5x
    426x
    426x
    426x

    426x
    426x
    353x
    171x
    182x
    91x

    91x

    426x
    39x

    426x

    426x
    426x
    353x

    426x

    5x
    148x
    148x
    148x

    148x
    148x
    148x

    148x
    59x
    59x
    59x
    89x
    12x
    12x
    12x
    77x
    19x
    19x
    19x
    58x
    26x
    26x
    26x
    32x
    16x
    16x
    16x
    16x
    16x
    16x
    16x

    148x

    5x
    148x
    148x
    148x

    148x
    148x
    134x
    67x
    67x
    34x

    33x

    148x
    16x

    148x
    148x
    146x

    148x

    5x
    148x
    148x
    148x

    148x
    148x
    148x
    148x
    59x
    59x
    59x
    89x
    12x
    12x
    12x
    77x
    19x
    19x
    19x
    58x
    26x
    26x
    26x
    32x
    16x
    16x
    16x
    16x
    16x
    16x
    16x

    148x

    5x
    2944x
    2944x
    2944x
    2905x
    2905x

    2944x

    5x
    2944x
    2944x
    2905x

    2944x
    1148x

    2944x

    2944x

    5x
    5460x
    5460x
    5460x

    5460x
    5460x
    5460x

    5460x
    5460x
    5264x

    196x

    5460x
    5460x
    5226x

    234x

    5460x
    5460x
    5046x

    414x

    5460x
    5460x
    5460x

    5460x

    5x

    16380x
    16047x

    333x

    5460x
    5460x
    5460x

    5460x
    5460x
    5460x

    5460x

    5x

    16380x
    1744x

    14636x

    5460x
    5460x
    5460x

    5460x
    5460x
    5460x

    5460x

    5x

    16380x
    1934x

    14446x

    5460x

    5460x

    5460x

    5460x

    5x
    5312x

    5x
    5312x

    5x
    2796x

    5x
    2796x

    5x

    2917x
    2917x
    2917x

    2917x
    11x
    2906x
    1x

    2917x
    1104x

    1104x
    1104x

    1813x
    1813x

    1813x
    1813x

    2917x
    1813x
    1104x
    162x

    942x
    942x

    2917x

    5x
    148x
    148x
    148x
    148x
    2516x
    2516x
    2516x
    1168x

    1348x

    148x

     import { ColorHSL } from "./color-hsl";
    import { ColorHSV } from "./color-hsv";
    import { ColorLAB } from "./color-lab";
    import { ColorLCH } from "./color-lch";
    import { ColorRGBA64 } from "./color-rgba-64";
    import { ColorXYZ } from "./color-xyz";
    import { degreesToRadians, radiansToDegrees } from "./math-utilities";

    // All hue values are in degrees rather than radians or normalized
    // All conversions use the D65 2 degree white point for XYZ
    // Info on conversions and constants used can be found in the following:
    // https://en.wikipedia.org/wiki/CIELAB_color_space
    // https://en.wikipedia.org/wiki/Illuminant_D65
    // https://ninedegreesbelow.com/photography/xyz-rgb.html
    // http://user.engineering.uiowa.edu/~aip/Misc/ColorFAQ.html
    // https://web.stanford.edu/~sujason/ColorBalancing/adaptation.html
    // http://brucelindbloom.com/index.html

    /**
     * Get the luminance of a color in the linear RGB space.
     * This is not the same as the relative luminance in the sRGB space for WCAG contrast calculations. Use rgbToRelativeLuminance instead.
     * @param rgb The input color
     */
    export function rgbToLinearLuminance(rgb: ColorRGBA64): number {
        return rgb.r * 0.2126 + rgb.g * 0.7152 + rgb.b * 0.0722;
    }

    /**
     * Get the relative luminance of a color.
     * Adjusts the color to sRGB space, which is necessary for the WCAG contrast spec.
     * The alpha channel of the input is ignored.
     * @param rgb The input color
     */
    export function rgbToRelativeLuminance(rgb: ColorRGBA64): number {
        function luminanceHelper(i: number): number {
            if (i <= 0.03928) {
                return i / 12.92;
            }
            return Math.pow((i + 0.055) / 1.055, 2.4);
        }

        return rgbToLinearLuminance(
            new ColorRGBA64(
                luminanceHelper(rgb.r),
                luminanceHelper(rgb.g),
                luminanceHelper(rgb.b),
                1
            )
        );
    }

    const calculateContrastRatio: (a: number, b: number) => number = (
        a: number,
        b: number
    ): number => (a + 0.05) / (b + 0.05);

    // The alpha channel of the input is ignored
    export function contrastRatio(a: ColorRGBA64, b: ColorRGBA64): number {
        const luminanceA: number = rgbToRelativeLuminance(a);
        const luminanceB: number = rgbToRelativeLuminance(b);
        return luminanceA > luminanceB
            ? calculateContrastRatio(luminanceA, luminanceB)
            : calculateContrastRatio(luminanceB, luminanceA);
    }

    /**
     * Calculate an overlay color that uses rgba (rgb + alpha) that matches the appareance of a given solid color when placed on the same background
     * @param rgbMatch The solid color the overlay should match in appearance when placed over the rgbBackground
     * @param rgbBackground The background on which the overlay rests
     * @param rgbOverlay The rgb color of the overlay. Typically this is either pure white or pure black. This color will be used in the returned output
     * @returns The rgba (rgb + alpha) color of the overlay
     */
    export function calculateOverlayColor(
        rgbMatch: ColorRGBA64,
        rgbBackground: ColorRGBA64,
        rgbOverlay: ColorRGBA64
    ): ColorRGBA64 {
        const rChannel: number =
            (rgbMatch.r - rgbBackground.r) / (rgbOverlay.r - rgbBackground.r);
        const gChannel: number =
            (rgbMatch.g - rgbBackground.g) / (rgbOverlay.g - rgbBackground.g);
        const bChannel: number =
            (rgbMatch.b - rgbBackground.b) / (rgbOverlay.b - rgbBackground.b);
        const alpha: number = (rChannel + gChannel + bChannel) / 3;
        return new ColorRGBA64(rgbOverlay.r, rgbOverlay.g, rgbOverlay.b, alpha);
    }

    // The alpha channel of the input is ignored
    export function rgbToHSL(rgb: ColorRGBA64): ColorHSL {
        const max: number = Math.max(rgb.r, rgb.g, rgb.b);
        const min: number = Math.min(rgb.r, rgb.g, rgb.b);
        const delta: number = max - min;

        let hue: number = 0;
        if (delta !== 0) {
            if (max === rgb.r) {
                hue = 60 * (((rgb.g - rgb.b) / delta) % 6);
            } else if (max === rgb.g) {
                hue = 60 * ((rgb.b - rgb.r) / delta + 2);
            } else {
                hue = 60 * ((rgb.r - rgb.g) / delta + 4);
            }
        }
        if (hue < 0) {
            hue += 360;
        }

        const lum: number = (max + min) / 2;

        let sat: number = 0;
        if (delta !== 0) {
            sat = delta / (1 - Math.abs(2 * lum - 1));
        }

        return new ColorHSL(hue, sat, lum);
    }

    export function hslToRGB(hsl: ColorHSL, alpha: number = 1): ColorRGBA64 {
        const c: number = (1 - Math.abs(2 * hsl.l - 1)) * hsl.s;
        const x: number = c * (1 - Math.abs(((hsl.h / 60) % 2) - 1));
        const m: number = hsl.l - c / 2;

        let r: number = 0;
        let g: number = 0;
        let b: number = 0;

        if (hsl.h < 60) {
            r = c;
            g = x;
            b = 0;
        } else if (hsl.h < 120) {
            r = x;
            g = c;
            b = 0;
        } else if (hsl.h < 180) {
            r = 0;
            g = c;
            b = x;
        } else if (hsl.h < 240) {
            r = 0;
            g = x;
            b = c;
        } else if (hsl.h < 300) {
            r = x;
            g = 0;
            b = c;
        } else Eif (hsl.h < 360) {
            r = c;
            g = 0;
            b = x;
        }

        return new ColorRGBA64(r + m, g + m, b + m, alpha);
    }

    // The alpha channel of the input is ignored
    export function rgbToHSV(rgb: ColorRGBA64): ColorHSV {
        const max: number = Math.max(rgb.r, rgb.g, rgb.b);
        const min: number = Math.min(rgb.r, rgb.g, rgb.b);
        const delta: number = max - min;

        let hue: number = 0;
        if (delta !== 0) {
            if (max === rgb.r) {
                hue = 60 * (((rgb.g - rgb.b) / delta) % 6);
            } else if (max === rgb.g) {
                hue = 60 * ((rgb.b - rgb.r) / delta + 2);
            } else {
                hue = 60 * ((rgb.r - rgb.g) / delta + 4);
            }
        }
        if (hue < 0) {
            hue += 360;
        }

        let sat: number = 0;
        if (max !== 0) {
            sat = delta / max;
        }

        return new ColorHSV(hue, sat, max);
    }

    export function hsvToRGB(hsv: ColorHSV, alpha: number = 1): ColorRGBA64 {
        const c: number = hsv.s * hsv.v;
        const x: number = c * (1 - Math.abs(((hsv.h / 60) % 2) - 1));
        const m: number = hsv.v - c;

        let r: number = 0;
        let g: number = 0;
        let b: number = 0;
        if (hsv.h < 60) {
            r = c;
            g = x;
            b = 0;
        } else if (hsv.h < 120) {
            r = x;
            g = c;
            b = 0;
        } else if (hsv.h < 180) {
            r = 0;
            g = c;
            b = x;
        } else if (hsv.h < 240) {
            r = 0;
            g = x;
            b = c;
        } else if (hsv.h < 300) {
            r = x;
            g = 0;
            b = c;
        } else Eif (hsv.h < 360) {
            r = c;
            g = 0;
            b = x;
        }

        return new ColorRGBA64(r + m, g + m, b + m, alpha);
    }

    export function lchToLAB(lch: ColorLCH): ColorLAB {
        let a: number = 0;
        let b: number = 0;
        if (lch.h !== 0) {
            a = Math.cos(degreesToRadians(lch.h)) * lch.c;
            b = Math.sin(degreesToRadians(lch.h)) * lch.c;
        }

        return new ColorLAB(lch.l, a, b);
    }

    // The discontinuity in the C parameter at 0 means that floating point errors will often result in values near 0 giving unpredictable results.
    // EG: 0.0000001 gives a very different result than -0.0000001
    // More info about the atan2 function: https://en.wikipedia.org/wiki/Atan2
    export function labToLCH(lab: ColorLAB): ColorLCH {
        let h: number = 0;
        if (lab.b !== 0 || lab.a !== 0) {
            h = radiansToDegrees(Math.atan2(lab.b, lab.a));
        }
        if (h < 0) {
            h += 360;
        }
        const c: number = Math.sqrt(lab.a * lab.a + lab.b * lab.b);

        return new ColorLCH(lab.l, c, h);
    }

    export function labToXYZ(lab: ColorLAB): ColorXYZ {
        const fy: number = (lab.l + 16) / 116;
        const fx: number = fy + lab.a / 500;
        const fz: number = fy - lab.b / 200;

        const xcubed: number = Math.pow(fx, 3);
        const ycubed: number = Math.pow(fy, 3);
        const zcubed: number = Math.pow(fz, 3);

        let x: number = 0;
        if (xcubed > ColorLAB.epsilon) {
            x = xcubed;
        } else {
            x = (116 * fx - 16) / ColorLAB.kappa;
        }

        let y: number = 0;
        if (lab.l > ColorLAB.epsilon * ColorLAB.kappa) {
            y = ycubed;
        } else {
            y = lab.l / ColorLAB.kappa;
        }

        let z: number = 0;
        if (zcubed > ColorLAB.epsilon) {
            z = zcubed;
        } else {
            z = (116 * fz - 16) / ColorLAB.kappa;
        }

        x = ColorXYZ.whitePoint.x * x;
        y = ColorXYZ.whitePoint.y * y;
        z = ColorXYZ.whitePoint.z * z;

        return new ColorXYZ(x, y, z);
    }

    export function xyzToLAB(xyz: ColorXYZ): ColorLAB {
        function xyzToLABHelper(i: number): number {
            if (i > ColorLAB.epsilon) {
                return Math.pow(i, 1 / 3);
            }
            return (ColorLAB.kappa * i + 16) / 116;
        }

        const x: number = xyzToLABHelper(xyz.x / ColorXYZ.whitePoint.x);
        const y: number = xyzToLABHelper(xyz.y / ColorXYZ.whitePoint.y);
        const z: number = xyzToLABHelper(xyz.z / ColorXYZ.whitePoint.z);

        const l: number = 116 * y - 16;
        const a: number = 500 * (x - y);
        const b: number = 200 * (y - z);

        return new ColorLAB(l, a, b);
    }

    // The alpha channel of the input is ignored
    export function rgbToXYZ(rgb: ColorRGBA64): ColorXYZ {
        function rgbToXYZHelper(i: number): number {
            if (i <= 0.04045) {
                return i / 12.92;
            }
            return Math.pow((i + 0.055) / 1.055, 2.4);
        }

        const r: number = rgbToXYZHelper(rgb.r);
        const g: number = rgbToXYZHelper(rgb.g);
        const b: number = rgbToXYZHelper(rgb.b);

        const x: number = r * 0.4124564 + g * 0.3575761 + b * 0.1804375;
        const y: number = r * 0.2126729 + g * 0.7151522 + b * 0.072175;
        const z: number = r * 0.0193339 + g * 0.119192 + b * 0.9503041;

        return new ColorXYZ(x, y, z);
    }

    // Note that the xyz color space is significantly larger than sRGB. As such, this can return colors rgb values greater than 1 or less than 0
    export function xyzToRGB(xyz: ColorXYZ, alpha: number = 1): ColorRGBA64 {
        function xyzToRGBHelper(i: number): number {
            if (i <= 0.0031308) {
                return i * 12.92;
            }
            return 1.055 * Math.pow(i, 1 / 2.4) - 0.055;
        }

        const r: number = xyzToRGBHelper(
            xyz.x * 3.2404542 - xyz.y * 1.5371385 - xyz.z * 0.4985314
        );
        const g: number = xyzToRGBHelper(
            xyz.x * -0.969266 + xyz.y * 1.8760108 + xyz.z * 0.041556
        );
        const b: number = xyzToRGBHelper(
            xyz.x * 0.0556434 - xyz.y * 0.2040259 + xyz.z * 1.0572252
        );

        return new ColorRGBA64(r, g, b, alpha);
    }

    // The alpha channel of the input is ignored
    export function rgbToLAB(rgb: ColorRGBA64): ColorLAB {
        return xyzToLAB(rgbToXYZ(rgb));
    }

    // Note that the xyz color space (which the conversion from LAB uses) is significantly larger than sRGB. As such, this can return colors rgb values greater than 1 or less than 0
    export function labToRGB(lab: ColorLAB, alpha: number = 1): ColorRGBA64 {
        return xyzToRGB(labToXYZ(lab), alpha);
    }

    // The alpha channel of the input is ignored
    export function rgbToLCH(rgb: ColorRGBA64): ColorLCH {
        return labToLCH(rgbToLAB(rgb));
    }

    export function lchToRGB(lch: ColorLCH, alpha: number = 1): ColorRGBA64 {
        return labToRGB(lchToLAB(lch), alpha);
    }

    export function temperatureToRGB(tempKelvin: number, alpha: number = 1): ColorRGBA64 {
        // The constants I could find assumed a decimal range of [0,255] for each channel. Just going to put a /255.0 at the end
        let r: number = 0;
        let g: number = 0;
        let b: number = 0;

        if (tempKelvin <= 1000) {
            tempKelvin = 1000;
        } else if (tempKelvin >= 40000) {
            tempKelvin = 40000;
        }

        if (tempKelvin < 6600.0) {
            r = 255.0;

            g = tempKelvin / 100.0 - 2.0;
            g =
                -155.25485562709179 -
                0.44596950469579133 * g +
                104.49216199393888 * Math.log(g);
        } else {
            r = tempKelvin / 100.0 - 55.0;
            r = 351.97690566805693 + 0.114206453784165 * r - 40.25366309332127 * Math.log(r);

            g = tempKelvin / 100.0 - 50.0;
            g = 325.4494125711974 + 0.07943456536662342 * g - 28.0852963507957 * Math.log(g);
        }

        if (tempKelvin >= 6600.0) {
            b = 255.0;
        } else if (tempKelvin < 2000.0) {
            b = 0.0;
        } else {
            b = tempKelvin / 100.0 - 10;
            b =
                -254.76935184120902 +
                0.8274096064007395 * b +
                115.67994401066147 * Math.log(b);
        }

        return new ColorRGBA64(r / 255, g / 255, b / 255, alpha);
    }

    // The alpha channel of the input is ignored
    export function rgbToTemperature(rgb: ColorRGBA64): number {
        let t: number = 0;
        let min: number = 1000;
        let max: number = 40000;
        while (max - min > 0.4) {
            t = (max + min) / 2.0;
            const testColor: ColorRGBA64 = temperatureToRGB(t);
            if (testColor.b / testColor.r >= rgb.b / rgb.r) {
                max = t;
            } else {
                min = t;
            }
        }
        return Math.round(t);
    }