All files / fast-colors/src color-rgba-64.ts

88.24% Statements 15/17
92.86% Branches 13/14
83.33% Functions 10/12
88.24% Lines 15/17

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 977x                           7x   38344x           350035x 350035x 350035x 350035x                   79x             667x         148x         148x         148x               148x                             308x                         2741x      
import {
    clamp,
    denormalize,
    getHexStringForByte,
    roundToPrecisionSmall,
} from "./math-utilities";
 
export interface ColorRGBA64Config {
    r: number;
    g: number;
    b: number;
    a?: number;
}
 
export class ColorRGBA64 {
    public static fromObject(data: ColorRGBA64Config): ColorRGBA64 | null {
        return data && !isNaN(data.r) && !isNaN(data.g) && !isNaN(data.b)
            ? new ColorRGBA64(data.r, data.g, data.b, data.a)
            : null;
    }
 
    constructor(red: number, green: number, blue: number, alpha?: number) {
        this.r = red;
        this.g = green;
        this.b = blue;
        this.a = typeof alpha === "number" && !isNaN(alpha) ? alpha : 1;
    }
 
    // Scaled to the range [0.0 , 1.0]. Values outside this range are allowed but any methods that convert or tostring the values will also be clamped
    public readonly r: number;
    public readonly g: number;
    public readonly b: number;
    public readonly a: number;
 
    public equalValue(rhs: ColorRGBA64): boolean {
        return (
            this.r === rhs.r && this.g === rhs.g && this.b === rhs.b && this.a === rhs.a
        );
    }
 
    // #RRGGBB
    public toStringHexRGB(): string {
        return "#" + [this.r, this.g, this.b].map(this.formatHexValue).join("");
    }
 
    // #RRGGBBAA
    public toStringHexRGBA(): string {
        return this.toStringHexRGB() + this.formatHexValue(this.a);
    }
 
    // #AARRGGBB
    public toStringHexARGB(): string {
        return "#" + [this.a, this.r, this.g, this.b].map(this.formatHexValue).join("");
    }
 
    // rgb(0xRR, 0xGG, 0xBB)
    public toStringWebRGB(): string {
        return `rgb(${Math.round(denormalize(this.r, 0.0, 255.0))},${Math.round(
            denormalize(this.g, 0.0, 255.0)
        )},${Math.round(denormalize(this.b, 0.0, 255.0))})`;
    }
 
    // rgba(0xRR, 0xGG, 0xBB, a)
    // Note that this follows the convention of putting alpha in the range [0.0,1.0] while the other three channels are [0,255]
    public toStringWebRGBA(): string {
        return `rgba(${Math.round(denormalize(this.r, 0.0, 255.0))},${Math.round(
            denormalize(this.g, 0.0, 255.0)
        )},${Math.round(denormalize(this.b, 0.0, 255.0))},${clamp(this.a, 0, 1)})`;
    }
 
    public roundToPrecision(precision: number): ColorRGBA64 {
        return new ColorRGBA64(
            roundToPrecisionSmall(this.r, precision),
            roundToPrecisionSmall(this.g, precision),
            roundToPrecisionSmall(this.b, precision),
            roundToPrecisionSmall(this.a, precision)
        );
    }
 
    public clamp(): ColorRGBA64 {
        return new ColorRGBA64(
            clamp(this.r, 0, 1),
            clamp(this.g, 0, 1),
            clamp(this.b, 0, 1),
            clamp(this.a, 0, 1)
        );
    }
 
    public toObject(): Required<ColorRGBA64Config> {
        return { r: this.r, g: this.g, b: this.b, a: this.a };
    }
 
    private formatHexValue(value: number): string {
        return getHexStringForByte(denormalize(value, 0.0, 255.0));
    }
}