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

53.85% Statements 7/13
0% Branches 0/9
20% Functions 1/5
53.85% Lines 7/13

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
    455x

    5x
    5x
    5x

    11216x
    11216x
    11216x

     import { roundToPrecisionSmall } from "./math-utilities";

    // CIELAB color space
    // https://en.wikipedia.org/wiki/CIELAB_color_space
    // This implementation uses the D65 constants for 2 degrees. That determines the constants used for the pure white point of the XYZ space of 0.95047, 1.0, 1.08883.
    // https://en.wikipedia.org/wiki/Illuminant_D65
    // These constants determine how the XYZ, LCH and LAB colors convert to/from RGB.
    export class ColorLAB {
        public static readonly epsilon: number = 216 / 24389;
        public static readonly kappa: number = 24389 / 27;

        public static fromObject(data: { l: number; a: number; b: number }): ColorLAB | null {
            if (data && !isNaN(data.l) && !isNaN(data.a) && !isNaN(data.b)) {
                return new ColorLAB(data.l, data.a, data.b);
            }
            return null;
        }

        constructor(l: number, a: number, b: number) {
            this.l = l;
            this.a = a;
            this.b = b;
        }

        public readonly l: number;
        public readonly a: number;
        public readonly b: number;

        public equalValue(rhs: ColorLAB): boolean {
            return this.l === rhs.l && this.a === rhs.a && this.b === rhs.b;
        }

        public roundToPrecision(precision: number): ColorLAB {
            return new ColorLAB(
                roundToPrecisionSmall(this.l, precision),
                roundToPrecisionSmall(this.a, precision),
                roundToPrecisionSmall(this.b, precision)
            );
        }

        public toObject(): { l: number; a: number; b: number } {
            return { l: this.l, a: this.a, b: this.b };
        }
    }