UNPKG

color-2-name

Version:

Finds the closest color name to a given hex, rgb and hsl color (with and without alpha). It uses the Euclidean distance formula to calculate the distance between colors in the RGB color space

75 lines (72 loc) 3.16 kB
export { getColor, getColors } from './color-utils.cjs'; import { COLORSTRING, RGBCOLORDEF, COLORDEF, HEX, ColorParsers, RGBVALUE } from './types.cjs'; declare const colorParsers: ColorParsers[]; declare function closest(color: string | COLORSTRING, set?: RGBCOLORDEF[] | undefined, args?: { info?: boolean; }): COLORDEF; /** * This function takes a string representing a color (color) and uses regular expressions to check if it matches any of the following formats: hex, hex+alpha, RGB, RGBA, HSL, or HSLA. * If the color string matches one of these formats, the function returns an object with the type of color and the value of the color. * If the color string does not match any of the formats, the function throws an error. * * @param {string} colorString - the color string to test and convert to rgb values * * @return {Object|Error} the object with rgb values of that color */ declare function parseColor(colorString: string): RGBVALUE; /** * Given a color returns if the color is light (by light is meant more mathematically closer to white) * * @param {string} color - The color to check * * @returns {boolean} true when the color is light, false otherwise * * @example isLight('#ddd'); // true */ declare function isLight(color: string): boolean; /** * Given a color returns if the color is dark (by dark is meant more mathematically closer to black) * * @param {string} color - The color to check * * @returns {boolean} true when the color is dark, false otherwise * * @example isDark('#333'); // true */ declare function isDark(color: string): boolean; /** * Given a color returns if the color is closer to "red", "green" or "blue". * * @param {string} color - The color to check * * @returns {string} light when the color is close to white, dark otherwise * * @example closestRGB('#f00'); // 'red' */ declare function closestRGB(color: string): string; /** * Compute the distance between the two RGB values * There are two modes: * fast = true -> the distance is calculated without using the Euclidean formula completely, it is reliable but its result is exponential * fast = false -> the distance is calculated with the Euclidean formula, its result is linear * * @param rgb1 - The RGB value of the first color to compare * @param rgb2 - The RGB value of the second color to compare * @param fast - If you want to calculate the distance without calculating the square root, the result will be exponential otherwise is linear * * @return {number} the distance between the two RGB values * * @example distance([10, 20, 30], [120, 120, 120]); // 173.78147196982766 */ declare function distance(rgb1: [number, number, number], rgb2: [number, number, number, string] | number[], fast?: boolean): number; /** * Given a color string it returns the hex representation * * @param rgbString - the rgb color string to convert to hex * * @return {string} the corresponding color hex * * @example rgbToHex("rgba(100% 0 0 / .5)"); // #FF0000 */ declare function rgbToHex(rgbString: string): HEX | Error; export { closest, closestRGB, colorParsers, distance, isDark, isLight, parseColor, rgbToHex };