@electrovir/color - v1.7.8
    Preparing search index...

    Class Color

    A Color class with state and the following features:

    • Color coordinates do not change when they become 'none', they stay at their previous value. This makes for a much smoother UI experience.
    • All relevant color spaces and formats are always set (no extra conversions necessary).
    • Matching CSS color names are tracked.
    • This class is exported correctly and the types aren't a mess.
    Index

    Constructors

    • Parameters

      • initValue:
            | string
            | Readonly<
                _RequireExactlyOne<
                    {
                        hex: PartialWithUndefined<Record<"r" | "g" | "b", number>>;
                        hsl: PartialWithUndefined<Record<"h" | "s" | "l", number>>;
                        hwb: PartialWithUndefined<Record<"b" | "h" | "w", number>>;
                        lab: PartialWithUndefined<Record<"b" | "l" | "a", number>>;
                        lch: PartialWithUndefined<Record<"h" | "l" | "c", number>>;
                        oklab: PartialWithUndefined<Record<"b" | "l" | "a", number>>;
                        oklch: PartialWithUndefined<Record<"h" | "l" | "c", number>>;
                        rgb: PartialWithUndefined<Record<"r" | "g" | "b", number>>;
                    } & { hexString: `#${string}`; name: string },

                        | "rgb"
                        | "hex"
                        | "hexString"
                        | "hsl"
                        | "hwb"
                        | "lab"
                        | "lch"
                        | "oklab"
                        | "oklch"
                        | "name",
                >,
            >

        Any valid CSS color string or an object of color coordinate values.

      Returns Color

    Properties

    _allColors: {
        hex: { b: number; g: number; r: number };
        hexString: `#${string}`;
        hsl: { h: number; l: number; s: number };
        hwb: { b: number; h: number; w: number };
        lab: { a: number; b: number; l: number };
        lch: { c: number; h: number; l: number };
        name: string;
        names: string[];
        oklab: { a: number; b: number; l: number };
        oklch: { c: number; h: number; l: number };
        rgb: { b: number; g: number; r: number };
    } = ...

    All current color values. These are updated whenever Color.set is called.

    originalColorSyntax: ColorSyntaxName = ColorSyntaxName.hex

    The color syntax the this color was set with.

    Accessors

    • get hex(): { b: number; g: number; r: number }

      The current color expressed as an RGB hex coordinates.

      Returns { b: number; g: number; r: number }

    • get hsl(): { h: number; l: number; s: number }

      The current color expressed as its HSL coordinate values.

      Returns { h: number; l: number; s: number }

    • get hwb(): { b: number; h: number; w: number }

      The current color expressed as its HWB coordinate values.

      Returns { b: number; h: number; w: number }

    • get lab(): { a: number; b: number; l: number }

      The current color expressed as its LAB coordinate values.

      Returns { a: number; b: number; l: number }

    • get lch(): { c: number; h: number; l: number }

      The current color expressed as its LCH coordinate values.

      Returns { c: number; h: number; l: number }

    • get name(): string

      The current color expressed as a single CSS color name string. If there is no color name that matches the current color, this will be an empty string.

      Returns string

    • get names(): string[]

      The current color expressed as hardcoded CSS color keywords. If no CSS color keywords match the current color, this array will be empty.

      Returns string[]

    • get oklab(): { a: number; b: number; l: number }

      The current color expressed as its Oklab coordinate values.

      Returns { a: number; b: number; l: number }

    • get oklch(): { c: number; h: number; l: number }

      The current color expressed as its Oklch coordinate values.

      Returns { c: number; h: number; l: number }

    • get rgb(): { b: number; g: number; r: number }

      The current color expressed as its RGB coordinate values.

      Returns { b: number; g: number; r: number }

    Methods

    • Get the closest named CSS color to this Color class instance's current color.

      Returns string

    • Get the distance from this Color class instance to the given CSS color string.

      Parameters

      • distanceFrom: string

      Returns number

    • Update the current color by setting a whole new color, a single coordinate in a single color format, or multiple coordinates in a single color format. This mutates the current Color instance.

      Parameters

      • newValue:
            | string
            | Readonly<
                _RequireExactlyOne<
                    {
                        hex: PartialWithUndefined<Record<"r" | "g" | "b", number>>;
                        hsl: PartialWithUndefined<Record<"h" | "s" | "l", number>>;
                        hwb: PartialWithUndefined<Record<"b" | "h" | "w", number>>;
                        lab: PartialWithUndefined<Record<"b" | "l" | "a", number>>;
                        lch: PartialWithUndefined<Record<"h" | "l" | "c", number>>;
                        oklab: PartialWithUndefined<Record<"b" | "l" | "a", number>>;
                        oklch: PartialWithUndefined<Record<"h" | "l" | "c", number>>;
                        rgb: PartialWithUndefined<Record<"r" | "g" | "b", number>>;
                    } & { hexString: `#${string}`; name: string },

                        | "rgb"
                        | "hex"
                        | "hexString"
                        | "hsl"
                        | "hwb"
                        | "lab"
                        | "lch"
                        | "oklab"
                        | "oklch"
                        | "name",
                >,
            >

      Returns void

    • Update the color to match the given string.

      Parameters

      • cssColorString: string

      Returns void

      Error if cssColorString is not able to be parsed.

    • Converts the color class to a CSS string format in the color space and format that it was originally set with.

      Returns string

    • Checks, with a type guard, that a given input is a Color class instance.

      Type Parameters

      • T

      Parameters

      • this: void
      • value: T

      Returns value is Extract<T, Readonly<Color>>

    • Checks if the input string can be converted into a color.

      Parameters

      • this: void
      • value: string

      Returns boolean