UNPKG

tiny-essentials

Version:

Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.

452 lines 19.5 kB
export default TinyColorValidator; /** * Represents the allowed angle unit types for CSS color functions. * * - `deg` → Degrees (0–360). * - `grad` → Gradians (0–400). * - `rad` → Radians (0–2π). * - `turn` → Turns (0–1). */ export type AngleUnit = "deg" | "grad" | "rad" | "turn"; /** * Represents the type of a CSS color. * * - `'hex'` → HEX color (`#RGB` / `#RRGGBB`) * - `'hexa'` → HEX with alpha (`#RRGGBBAA`) * - `'rgb'` → RGB color (`rgb(r, g, b)`) * - `'rgba'` → RGB with alpha (`rgba(r, g, b, a)`) * - `'hsl'` → HSL color (`hsl(h, s%, l%)`) * - `'hsla'` → HSL with alpha (`hsla(h, s%, l%, a)`) * - `'hwb'` → HWB color (`hwb(hue, whiteness%, blackness%)`) * - `'lab'` → CIELAB color (`lab(L a b)`) * - `'lch'` → LCH color (`lch(L C H)`) * - `'name'` → Standard HTML color name (e.g., `red`, `blue`) * - `'specialName'` → CSS special keywords (`transparent`, `currentColor`) */ export type ColorTypes = "hex" | "hexa" | "rgb" | "rgba" | "hsl" | "hsla" | "hwb" | "lab" | "lch" | "name" | "specialName"; /** * Represents the allowed angle unit types for CSS color functions. * * - `deg` → Degrees (0–360). * - `grad` → Gradians (0–400). * - `rad` → Radians (0–2π). * - `turn` → Turns (0–1). * * @typedef {'deg' | 'grad' | 'rad' | 'turn'} AngleUnit */ /** * Represents the type of a CSS color. * * - `'hex'` → HEX color (`#RGB` / `#RRGGBB`) * - `'hexa'` → HEX with alpha (`#RRGGBBAA`) * - `'rgb'` → RGB color (`rgb(r, g, b)`) * - `'rgba'` → RGB with alpha (`rgba(r, g, b, a)`) * - `'hsl'` → HSL color (`hsl(h, s%, l%)`) * - `'hsla'` → HSL with alpha (`hsla(h, s%, l%, a)`) * - `'hwb'` → HWB color (`hwb(hue, whiteness%, blackness%)`) * - `'lab'` → CIELAB color (`lab(L a b)`) * - `'lch'` → LCH color (`lch(L C H)`) * - `'name'` → Standard HTML color name (e.g., `red`, `blue`) * - `'specialName'` → CSS special keywords (`transparent`, `currentColor`) * * @typedef {'hex'|'hexa'|'rgb'|'rgba'|'hsl'|'hsla'|'hwb'|'lab'|'lch'|'name'|'specialName'} ColorTypes */ /** * Utility class for validating and parsing CSS color values. * * Supports multiple CSS color syntaxes: * - **Hexadecimal**: `#RGB`, `#RRGGBB`, `#RRGGBBAA` * - **RGB/RGBA**: `rgb(r, g, b)`, `rgba(r, g, b, a)` * - **HSL/HSLA**: `hsl(h, s%, l%)`, `hsla(h, s%, l%, a)` * - **HWB**: `hwb(hue whiteness% blackness%)` * - **CIELAB**: `lab(L a b)` * - **LCH**: `lch(L C H)` * - **Named colors**: standard HTML color names (e.g., `red`, `blue`, `rebeccapurple`) * - **Special keywords**: `transparent`, `currentColor` * * Features: * - Provides validation (`isHex`, `isRgb`, `isHsl`, etc.) for each format. * - Provides parsing methods (`parseRgb`, `parseHsl`, etc.) returning numeric components. * - Allows extending and managing HTML color names and special keywords dynamically. * * Example: * ```js * const validator = new TinyColorValidator("rgb(255, 0, 0)"); * console.log(validator.isRgb()); // true * console.log(validator.parseRgb()); // [255, 0, 0] * ``` * * @class * @template {string} TinyColorValidatorT */ declare class TinyColorValidator<TinyColorValidatorT extends string> { static "__#private@#HEX_REGEX": RegExp; static "__#private@#HEXA_REGEX": RegExp; static "__#private@#ALPHA": string; static "__#private@#NUM_0_255": string; static "__#private@#NUM_0_360": string; static "__#private@#NUM_0_100": string; static "__#private@#RGB_REGEX": RegExp; static "__#private@#RGBA_REGEX": RegExp; static "__#private@#HSL_REGEX": RegExp; static "__#private@#HSLA_REGEX": RegExp; static "__#private@#HWB_REGEX": RegExp; static "__#private@#LAB_AB": string; static "__#private@#LAB_REGEX": RegExp; static "__#private@#LCH_REGEX": RegExp; static "__#private@#HTML_COLOR_NAMES": Set<string>; static "__#private@#SPECIAL_COLOR_NAMES": Set<string>; /** * Returns all HTML color names as an array. * @returns {string[]} */ static getNames(): string[]; /** * Adds a new HTML color name. * @param {string} name * @returns {boolean} True if added, false if it already existed. */ static addName(name: string): boolean; /** * Removes an HTML color name. * @param {string} name * @returns {boolean} True if removed, false if not found. */ static removeName(name: string): boolean; /** * Checks if an HTML color name exists. * @param {string} name * @returns {boolean} */ static hasName(name: string): boolean; /** * Returns all special color names as an array. * @returns {string[]} */ static getSpecialNames(): string[]; /** * Adds a new special color name. * @param {string} name * @returns {boolean} True if added, false if it already existed. */ static addSpecialName(name: string): boolean; /** * Removes a special color name. * @param {string} name * @returns {boolean} True if removed, false if not found. */ static removeSpecialName(name: string): boolean; /** * Checks if a special color name exists. * @param {string} name * @returns {boolean} */ static hasSpecialName(name: string): boolean; /** * Validates if a string is a valid HEX color (#RGB, #RRGGBB). * @param {string} input - The input string to validate. * @returns {boolean} True if the input is a valid HEX color. * @throws {TypeError} If input is not a string. */ static isHex(input: string): boolean; /** * Validates if a string is a valid HEX color (#RRGGBBAA). * @param {string} input - The input string to validate. * @returns {boolean} True if the input is a valid HEXA color. * @throws {TypeError} If input is not a string. */ static isHexa(input: string): boolean; /** * Validates if a string is a valid RGB color. * @param {string} input - The input string to validate. * @returns {boolean} True if the input is a valid RGB color. * @throws {TypeError} If input is not a string. */ static isRgb(input: string): boolean; /** * Validates if a string is a valid RGBA color. * @param {string} input - The input string to validate. * @returns {boolean} True if the input is a valid RGBA color. * @throws {TypeError} If input is not a string. */ static isRgba(input: string): boolean; /** * Validates if a string is a valid HSL color. * @param {string} input - The input string to validate. * @returns {boolean} True if the input is a valid HSL color. * @throws {TypeError} If input is not a string. */ static isHsl(input: string): boolean; /** * Validates if a string is a valid HSLA color. * @param {string} input - The input string to validate. * @returns {boolean} True if the input is a valid HSLA color. * @throws {TypeError} If input is not a string. */ static isHsla(input: string): boolean; /** * Validates if a string is a valid HWB color. * @param {string} input - The input string to validate. * @returns {boolean} True if the input is a valid HWB color. * @throws {TypeError} If input is not a string. */ static isHwb(input: string): boolean; /** * Validates if a string is a valid CIELAB color. * @param {string} input - The input string to validate. * @returns {boolean} True if the input is a valid Lab color. * @throws {TypeError} If input is not a string. */ static isLab(input: string): boolean; /** * Validates if a string is a valid LCH color. * @param {string} input - The input string to validate. * @returns {boolean} True if the input is a valid LCH color. * @throws {TypeError} If input is not a string. */ static isLch(input: string): boolean; /** * Validates if a string matches a standard HTML color name. * @param {string} input - The input string to validate. * @returns {boolean} True if the input is a valid HTML color name. * @throws {TypeError} If input is not a string. */ static isName(input: string): boolean; /** * Validates if a string matches a special CSS color keyword. * @param {string} input - The input string to validate. * @returns {boolean} True if the input is a special color keyword. * @throws {TypeError} If input is not a string. */ static isSpecialName(input: string): boolean; /** * Validates if a string is any valid CSS color (HEX, RGB, HSL, HWB, Lab, LCH, name, or special name). * @param {string} input - The input string to validate. * @returns {ColorTypes|null} if the input is a valid CSS color. * @throws {TypeError} If input is not a string. */ static isColor(input: string): ColorTypes | null; /** * Parses a HEX color string (#RGB or #RRGGBB). * Returns the regex match array with captured groups or null if invalid. * @param {string} input - The input string to parse. * @returns {string|null} Regex match result with captured groups, or null if not valid. * @throws {TypeError} If input is not a string. */ static parseHex(input: string): string | null; /** * Parses a HEXA color string (#RRGGBBAA). * Returns the regex match array with captured groups or null if invalid. * @param {string} input - The input string to parse. * @returns {string|null} Regex match result with captured groups, or null if not valid. * @throws {TypeError} If input is not a string. */ static parseHexa(input: string): string | null; /** * Parses an RGB color string (rgb(r, g, b)). * Returns the regex match array with captured groups for r, g, and b or null if invalid. * @param {string} input - The input string to parse. * @returns {[number, number, number]|null} Regex match result with groups [r, g, b], or null if not valid. * @throws {TypeError} If input is not a string. */ static parseRgb(input: string): [number, number, number] | null; /** * Parses an RGBA color string (rgba(r, g, b, a)). * Returns the regex match array with captured groups for r, g, b, and a or null if invalid. * @param {string} input - The input string to parse. * @returns {[number, number, number, number]|null} Regex match result with groups [r, g, b, a], or null if not valid. * @throws {TypeError} If input is not a string. */ static parseRgba(input: string): [number, number, number, number] | null; /** * Parses an HSL color string (hsl(h, s%, l%)). * Returns the regex match array with captured groups for h, s, and l or null if invalid. * @param {string} input - The input string to parse. * @returns {[number, number, number]|null} Regex match result with groups [h, s, l], or null if not valid. * @throws {TypeError} If input is not a string. */ static parseHsl(input: string): [number, number, number] | null; /** * Parses an HSLA color string (hsla(h, s%, l%, a)). * Returns the regex match array with captured groups for h, s, l, and a or null if invalid. * @param {string} input - The input string to parse. * @returns {[number, number, number, number]|null} Regex match result with groups [h, s, l, a], or null if not valid. * @throws {TypeError} If input is not a string. */ static parseHsla(input: string): [number, number, number, number] | null; /** * Parses an HWB color string (hwb(hue, whiteness%, blackness%[, alpha])). * Returns the regex match array with captured groups or null if invalid. * @param {string} input - The input string to parse. * @returns {[number, AngleUnit|null, number, number]|null} Regex match result with captured groups, or null if not valid. * @throws {TypeError} If input is not a string. */ static parseHwb(input: string): [number, AngleUnit | null, number, number] | null; /** * Parses a CIELAB color string (lab(L a b[/alpha])). * Returns the regex match array with captured groups or null if invalid. * @param {string} input - The input string to parse. * @returns {[number, number, number]|null} Regex match result with captured groups, or null if not valid. * @throws {TypeError} If input is not a string. */ static parseLab(input: string): [number, number, number] | null; /** * Parses an LCH color string (lch(L C H[/alpha])). * Returns the regex match array with captured groups or null if invalid. * @param {string} input - The input string to parse. * @returns {[number, number, number, AngleUnit|null]|null} Regex match result with captured groups, or null if not valid. * @throws {TypeError} If input is not a string. */ static parseLch(input: string): [number, number, number, AngleUnit | null] | null; /** * Creates a new instance of Example. * @param {TinyColorValidatorT} code - The code to be stored. */ constructor(code: TinyColorValidatorT); /** * Gets the current code. * @returns {string} The stored code. */ get code(): string; /** * Gets the current code. * @returns {ColorTypes|null} The stored code type. */ get type(): ColorTypes | null; /** * Validates if a string is a valid HEX color (#RGB, #RRGGBB). * @returns {boolean} True if the input is a valid HEX color. * @throws {TypeError} If input is not a string. */ isHex(): boolean; /** * Validates if a string is a valid HEX color (#RRGGBBAA). * @returns {boolean} True if the input is a valid HEXA color. * @throws {TypeError} If input is not a string. */ isHexa(): boolean; /** * Validates if a string is a valid RGB color. * @returns {boolean} True if the input is a valid RGB color. * @throws {TypeError} If input is not a string. */ isRgb(): boolean; /** * Validates if a string is a valid RGBA color. * @returns {boolean} True if the input is a valid RGBA color. * @throws {TypeError} If input is not a string. */ isRgba(): boolean; /** * Validates if a string is a valid HSL color. * @returns {boolean} True if the input is a valid HSL color. * @throws {TypeError} If input is not a string. */ isHsl(): boolean; /** * Validates if a string is a valid HSLA color. * @returns {boolean} True if the input is a valid HSLA color. * @throws {TypeError} If input is not a string. */ isHsla(): boolean; /** * Validates if a string is a valid HWB color. * @returns {boolean} True if the input is a valid HWB color. * @throws {TypeError} If input is not a string. */ isHwb(): boolean; /** * Validates if a string is a valid CIELAB color. * @returns {boolean} True if the input is a valid Lab color. * @throws {TypeError} If input is not a string. */ isLab(): boolean; /** * Validates if a string is a valid LCH color. * @returns {boolean} True if the input is a valid LCH color. * @throws {TypeError} If input is not a string. */ isLch(): boolean; /** * Validates if a string matches a standard HTML color name. * @returns {boolean} True if the input is a valid HTML color name. * @throws {TypeError} If input is not a string. */ isName(): boolean; /** * Validates if a string matches a special CSS color keyword. * @returns {boolean} True if the input is a special color keyword. * @throws {TypeError} If input is not a string. */ isSpecialName(): boolean; /** * Automatically parses the stored code based on its detected type. * @returns {any[]|string|null} Parsed color components according to type, or null if invalid. */ parse(): any[] | string | null; /** * Parses a HEX color string (#RGB or #RRGGBB). * Returns the regex match array with captured groups or null if invalid. * @returns {string|null} Regex match result with captured groups, or null if not valid. * @throws {TypeError} If input is not a string. */ parseHex(): string | null; /** * Parses a HEXA color string (#RRGGBBAA). * Returns the regex match array with captured groups or null if invalid. * @returns {string|null} Regex match result with captured groups, or null if not valid. * @throws {TypeError} If input is not a string. */ parseHexa(): string | null; /** * Parses an RGB color string (rgb(r, g, b)). * Returns the regex match array with captured groups for r, g, and b or null if invalid. * @returns {[number, number, number]|null} Regex match result with groups [r, g, b], or null if not valid. * @throws {TypeError} If input is not a string. */ parseRgb(): [number, number, number] | null; /** * Parses an RGBA color string (rgba(r, g, b, a)). * Returns the regex match array with captured groups for r, g, b, and a or null if invalid. * @returns {[number, number, number, number]|null} Regex match result with groups [r, g, b, a], or null if not valid. * @throws {TypeError} If input is not a string. */ parseRgba(): [number, number, number, number] | null; /** * Parses an HSL color string (hsl(h, s%, l%)). * Returns the regex match array with captured groups for h, s, and l or null if invalid. * @returns {[number, number, number]|null} Regex match result with groups [h, s, l], or null if not valid. * @throws {TypeError} If input is not a string. */ parseHsl(): [number, number, number] | null; /** * Parses an HSLA color string (hsla(h, s%, l%, a)). * Returns the regex match array with captured groups for h, s, l, and a or null if invalid. * @returns {[number, number, number, number]|null} Regex match result with groups [h, s, l, a], or null if not valid. * @throws {TypeError} If input is not a string. */ parseHsla(): [number, number, number, number] | null; /** * Parses an HWB color string (hwb(hue, whiteness%, blackness%[, alpha])). * Returns the regex match array with captured groups or null if invalid. * @returns {[number, AngleUnit|null, number, number]|null} Regex match result with captured groups, or null if not valid. * @throws {TypeError} If input is not a string. */ parseHwb(): [number, AngleUnit | null, number, number] | null; /** * Parses a CIELAB color string (lab(L a b[/alpha])). * Returns the regex match array with captured groups or null if invalid. * @returns {[number, number, number]|null} Regex match result with captured groups, or null if not valid. * @throws {TypeError} If input is not a string. */ parseLab(): [number, number, number] | null; /** * Parses an LCH color string (lch(L C H[/alpha])). * Returns the regex match array with captured groups or null if invalid. * @returns {[number, number, number, AngleUnit|null]|null} Regex match result with captured groups, or null if not valid. * @throws {TypeError} If input is not a string. */ parseLch(): [number, number, number, AngleUnit | null] | null; #private; } //# sourceMappingURL=TinyColorValidator.d.mts.map