All files colorXYZ.ts

54.55% Statements 6/11
0% Branches 0/7
20% Functions 1/5
54.55% Lines 6/11

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  1x             1x   1x                   5x 5x 5x                                              
// tslint:disable:member-ordering
import { roundToPrecisionSmall } from "./mathUtils";
 
// XYZ color space
// https://en.wikipedia.org/wiki/CIE_1931_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 ColorXYZ {
    // D65 2 degree white point
    public static readonly whitePoint: ColorXYZ = new ColorXYZ(0.95047, 1.0, 1.08883);
 
    public static fromObject(data: { x: number; y: number; z: number }): ColorXYZ | null {
        if (data && !isNaN(data.x) && !isNaN(data.y) && !isNaN(data.z)) {
            return new ColorXYZ(data.x, data.y, data.z);
        }
        return null;
    }
 
    constructor(x: number, y: number, z: number) {
        this.x = x;
        this.y = y;
        this.z = z;
    }
 
    public readonly x: number;
    public readonly y: number;
    public readonly z: number;
 
    public equalValue(rhs: ColorXYZ): boolean {
        return this.x === rhs.x && this.y === rhs.y && this.z === rhs.z;
    }
 
    public roundToPrecision(precision: number): ColorXYZ {
        return new ColorXYZ(
            roundToPrecisionSmall(this.x, precision),
            roundToPrecisionSmall(this.y, precision),
            roundToPrecisionSmall(this.z, precision)
        );
    }
 
    public toObject(): { x: number; y: number; z: number } {
        return { x: this.x, y: this.y, z: this.z };
    }
}