@lueur/avatar
Version:
A TypeScript package to generate customizable SVG user avatars.
52 lines (51 loc) • 2.12 kB
TypeScript
/**
* A collection of utility functions for generating and customizing avatars.
* These functions handle color generation, text processing, and contrast calculations.
* @module avatar-utils
*/
/**
* Hashes a string into a numeric value.
* Used for deterministic color generation.
* @param {string} str The input string.
* @returns {number} A numeric hash.
* @example
* const hash = stringToHash('johndoe'); // Returns consistent numeric hash
*/
export declare function stringToHash(str: string): number;
/**
* Generates a color from a hash value.
* Uses a predefined palette for aesthetic consistency.
* @param {number} hash The numeric hash value.
* @returns {string} A hex color string (e.g., '#RRGGBB').
* @example
* const color = getColorFromHash(12345); // Returns '#4CAF50'
*/
export declare function getColorFromHash(hash: number): string;
/**
* Calculates the luminance of a hex color.
* @param {string} hex Color in hex format (e.g., '#RRGGBB').
* @returns {number} A number between 0 and 255 representing luminance.
* @example
* const luminance = getLuminance('#FFFFFF'); // Returns 255
*/
export declare function getLuminance(hex: string): number;
/**
* Determines a contrasting text color (black or white) based on background luminance.
* @param {string} hexBackgroundColor The background color in hex format.
* @returns {string} '#000000' (black) or '#FFFFFF' (white).
* @example
* const textColor = getContrastingTextColor('#FF0000'); // Returns '#FFFFFF'
*/
export declare function getContrastingTextColor(hexBackgroundColor: string): string;
/**
* Extracts initials from a username.
* Handles various name formats, including special characters and multiple words.
* @param {string} username The full username string.
* @param {number} length The desired number of initials.
* @returns The extracted initials.
* @example
* getInitials('John Doe', 2); // Returns 'JD'
* getInitials('mary.jane@email.com', 2); // Returns 'MJ'
* getInitials('Dr. Robert Smith Jr.', 2); // Returns 'RS'
*/
export declare function getInitials(username: string, length: number): string;