ddnet
Version:
A typescript npm package for interacting with data from ddnet.org
182 lines (181 loc) • 5.09 kB
TypeScript
/**
* Valid hex characters.
*
* @internal
*/
export type HexChar = 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
/**
* String length helper.
*
* @internal
*/
export type StringLength<Str extends string, Acc extends string[] = []> = Str extends `${infer S}${infer Rest}` ? StringLength<Rest, [...Acc, S]> : Acc['length'];
/**
* Validates if a string is of a certain length
*
* @internal
*/
export type ValidateLength<Str extends string, Length extends number> = StringLength<Str> extends Length ? Str : never;
/**
* Validates chars in a string to be valid hex.
*
* @internal
*/
export type ValidateHex<Color extends string, Cache extends string = ''> = Color extends `${infer A}${infer Rest}` ? A extends '' ? Cache : A extends HexChar ? ValidateHex<Rest, `${Cache}${A}`> : never : Cache;
/**
* Prepends `#` to a string.
*
* @internal
*/
export type AddHash<T extends string> = `#${T}`;
/**
* Validates if a string is a valid hex color.
*
* @internal
*/
export type ValidateHexStr<T extends string> = T & ValidateHex<T> & ValidateLength<T, 6 | 8 | 3 | 4>;
/**
* Represents a color type.
*/
export type ColorType = 'hex' | 'tw' | 'rgba' | 'hsla';
/**
* Represents an RGBA color.
*/
export type RGBA_Color = {
r: number;
g: number;
b: number;
a: number;
};
/**
* Represents an HSLA color.
*/
export type HSLA_Color = {
h: number;
s: number;
l: number;
a: number;
};
/**
* Represents a HEX color.
*/
export type HEX_Color<T extends string> = AddHash<ValidateHexStr<T>>;
/**
* Helper class to convert or pass around colors.
*
* @typeParam T used internally.
*/
export declare class Color<T extends string> {
/**
* Stored color in HSLA format.
*/
private color;
/**
* Converts an RGBA color to an HSLA color.
*/
private static RGBA_to_HSLA;
/**
* Construct a new {@link Color} instance from a teeworlds color code.
*/
static from(
/**
* A TeeWorlds color code.
*/
tw: number): Color<string>;
/**
* Construct a new {@link Color} instance from an rgba color.
*/
static from(
/**
* An RGBA color object with keys for Red (0-255), Green (0-255), Blue (0-255) and optionally Alpha (0-1)
*
* @example
* ```ts
* const col: RGBA_Color = { r: 169, g: 69, b: 42 }; // optionally add `a` for alpha
* ```
*/
rgba: RGBA_Color): Color<string>;
/**
* Construct a new {@link Color} instance from an hsla color.
*/
static from(
/**
* An HSLA color object with keys for Hue (0-360), Saturation (0-100), Lightness (0-100) and optionally Alpha (0-1)
*
* @example
* ```ts
* const col: HSLA_Color = { h: 169, s: 69, l: 42 }; // optionally add `a` for alpha
* ```
*/
hsla: HSLA_Color): Color<string>;
/**
* Construct a new {@link Color} instance from a hex color.
*/
static from<T extends string>(
/**
* A hex color in short or long form, a string starting with `#` followed by either 3, 4, 6 or 8 hex chars.
*
* @example
* ```ts
* const col1 = '#123'; // short form, no alpha
* const col2 = '#1234'; // short form, with alpha
* const col3 = '#123abc'; // long form, no alpha
* const col4 = '#123abc99'; // long form, with alpha
* ```
*/
hex: HEX_Color<T>): Color<string>;
/**
* Construct a new {@link Color} instance from a teeworlds color code.
*/
constructor(
/**
* A TeeWorlds color code.
*/
tw: number);
/**
* Construct a new {@link Color} instance from an rgba color.
*/
constructor(
/**
* An RGBA color object with keys for Red (0-255), Green (0-255), Blue (0-255) and optionally Alpha (0-1)
*
* @example
* ```ts
* const col: RGBA_Color = { r: 169, g: 69, b: 42 }; // optionally add `a` for alpha
* ```
*/
rgba: RGBA_Color);
/**
* Construct a new {@link Color} instance from an hsla color.
*/
constructor(
/**
* An HSLA color object with keys for Hue (0-360), Saturation (0-100), Lightness (0-100) and optionally Alpha (0-1)
*
* @example
* ```ts
* const col: HSLA_Color = { h: 169, s: 69, l: 42 }; // optionally add `a` for alpha
* ```
*/
hsla: HSLA_Color);
/**
* Construct a new {@link Color} instance from a hex color.
*/
constructor(
/**
* A hex color in short or long form, a string starting with `#` followed by either 3, 4, 6 or 8 hex chars.
*
* @example
* ```ts
* const col1 = '#123'; // short form, no alpha
* const col2 = '#1234'; // short form, with alpha
* const col3 = '#123abc'; // long form, no alpha
* const col4 = '#123abc99'; // long form, with alpha
* ```
*/
hex: HEX_Color<T>);
to(format: 'hex'): `#${string}`;
to(format: 'tw'): number;
to(format: 'rgba'): RGBA_Color;
to(format: 'hsla'): HSLA_Color;
}