string-to-color-gradient
Version:
A lightweight npm library to convert any string into consistent hex colors and CSS gradients — perfect for avatars, tags, themes, blog cards, and visual identifiers. Turn names, emails, or any string into beautiful, deterministic color values that stay th
52 lines • 1.69 kB
TypeScript
//#region src/model/model.d.ts
/**
* The brightness level to influence color saturation and lightness.
*
* - 'dark' => deeper colors
* - 'normal' => balanced tones (default)
* - 'light' => lighter, more pastel-like colors
*/
type Brightness = 'dark' | 'normal' | 'light';
/**
* Options to customize the gradient generation.
*/
interface GradientOptions {
/**
* Brightness level used to determine color saturation/lightness.
* @default 'normal'
*/
brightness?: Brightness;
/**
* Gradient angle in degrees or 'auto' to derive it from the string hash.
* @default 'auto'
*/
angle?: 'auto' | number;
}
//#endregion
//#region src/index.d.ts
/**
* Converts a string into a single HSL-based hex color.
*
* @param str - Input string to hash
* @param brightness - Brightness setting (default: 'normal')
* @returns A hex color string (e.g., "#a1b2c3")
*/
declare function stringToColor(str: string, brightness?: Brightness): string;
/**
* Converts a string into a gradient composed of two colors.
*
* @param str - Input string to hash
* @param options - Gradient options (brightness)
* @returns A tuple of two hex color strings
*/
declare function stringToGradient(str: string, options?: GradientOptions): [string, string];
/**
* Converts a string into a complete CSS linear-gradient string.
*
* @param str - Input string to hash
* @param options - Gradient options including angle and brightness
* @returns A CSS gradient string (e.g., `linear-gradient(45deg, #a1b2c3, #d4e5f6)`)
*/
declare function stringToCssGradient(str: string, options?: GradientOptions): string;
//#endregion
export { stringToColor, stringToCssGradient, stringToGradient };