uuid-color
Version:
A lightweight package to generate unique and uniformly sampled colors from UUIDs.
65 lines (64 loc) • 3.01 kB
TypeScript
declare type SupportedColorNotations = "hex" | "rgb" | "hsl";
declare type Receivers = {
hex?: (hexString: string) => void;
rgb?: (red: number, green: number, blue: number, alpha?: number) => void;
hsl?: (hue: number, saturation: number, lightness: number, alpha?: number) => void;
};
export interface Options {
/**
* Determines whether to skip rounding the generated color components.
*
* @remarks
* Only applies when the specified format is `"hsl"`, as this is the only output format that involves a lossy conversion (from RGB model components).
*
* @defaultValue `false`
*/
raw?: boolean;
/**
* Determines the alpha of the generated color as a number between 0 and 1. Passed through to the final generated color without any involvement in the generation process.
*
* @remarks
* Specifying any value (even if it is equal to or greater than 1) will cause the function to return the generated color as a notation string including the alpha component (see the alpha variants in {@link Options.format | Options.format}), and cause the alpha to be included in the parameter(s) of the call to the supplied {@link Receivers} value(s).
* Values are not validated, but are clamped to between 0 and 1.
*
* @defaultValue `1`
*/
alpha?: number;
/**
* Determines the output format of the generated color.
*
* hex: #rrggbb[aa]
* rgb: rgb[a](R, G, B[, A])
* hsl: hsl[a](H, S, L[, A])
* @see { @link https://developer.mozilla.org/en-US/docs/Web/CSS/color_value | \<color\> }
*
* @remarks
* Colors are returned as strings in the CSS <color> data type hexadecimal or comma-separated functional notation corresponding to the specified format.
*
* @defaultValue `"hex"`
*/
format?: SupportedColorNotations;
/**
* Attach callbacks to {@link SupportedColorNotations | supported color notations} as keys that will be called with the corresponding generated color components or code. Eliminates the need to parse the returned string if further manipulation is desired.
*
* @remarks
* Does not affect the return value of {@link colorFromUuid | the `colorFromUuid` function}.
*
* @see {@link Receivers | the Receivers type}
*/
receivers?: Receivers;
}
/**
* Returns the generated color associated with the given uuid.
*
* @param uuid - The uuid for which to generate a color
* @param options - An optional object to configure the color generation, and attach callbacks that directly receive the generated color code or components in various formats
* @returns The generated color as a CSS `<color>` notation string
*
* @throws {@link https://developer.mozilla.org/en-US/docs/web/javascript/reference/global_objects/error | Error}
* This exception is thrown if the input uuid string is not a valid UUID.
*
* @public
*/
export declare function colorFromUuid(uuid: string, options?: Options): string;
export {};