UNPKG

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

128 lines (125 loc) 4.21 kB
//#region rolldown:runtime var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) { key = keys[i]; if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: ((k) => from[k]).bind(null, key), enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod)); //#endregion let crypto_js = require("crypto-js"); crypto_js = __toESM(crypto_js); let tinycolor2 = require("tinycolor2"); tinycolor2 = __toESM(tinycolor2); //#region src/index.ts /** * Hashes a string to a 32-bit integer using MD5. * @param str - Input string * @returns A 32-bit integer derived from the string hash */ function hashStringToInt(str) { const hash = crypto_js.default.MD5(str).toString(); return parseInt(hash.slice(0, 12), 16); } /** * Returns a predefined lightness value based on brightness level. * @param brightness - Brightness setting * @returns Lightness value (0–100) */ function getLightness(brightness = "normal") { switch (brightness) { case "dark": return 35; case "light": return 70; default: return 60; } } /** * Returns a predefined saturation value based on brightness level. * @param brightness - Brightness setting * @returns Saturation value (0–100) */ function getSaturation(brightness = "normal") { switch (brightness) { case "dark": return 80; case "light": return 60; default: return 70; } } /** * Generates a consistent gradient angle from a string using SHA-1. * @param str - Input string * @returns Angle between 0–359 degrees */ function getAngleFromString(str) { const hash = crypto_js.default.SHA1(str).toString(); return parseInt(hash.slice(0, 6), 16) % 360; } /** * 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") */ function stringToColor(str, brightness = "normal") { return (0, tinycolor2.default)({ h: hashStringToInt(str) % 360, s: getSaturation(brightness), l: getLightness(brightness) }).toHexString(); } /** * 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 */ function stringToGradient(str, options = {}) { const { brightness = "normal" } = options; const hash = crypto_js.default.SHA256(str).toString(); const hue1 = parseInt(hash.slice(0, 8), 16) % 360; let hue2 = parseInt(hash.slice(8, 16), 16) % 360; const MIN_HUE_DIFF = 30; if (Math.abs(hue1 - hue2) < MIN_HUE_DIFF) hue2 = (hue2 + MIN_HUE_DIFF) % 360; const saturation = getSaturation(brightness); const lightness = getLightness(brightness); return [(0, tinycolor2.default)({ h: hue1, s: saturation, l: lightness }).toHexString(), (0, tinycolor2.default)({ h: hue2, s: saturation, l: lightness }).toHexString()]; } /** * 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)`) */ function stringToCssGradient(str, options = {}) { const { angle = "auto" } = options; const [color1, color2] = stringToGradient(str, options); return `linear-gradient(${angle === "auto" ? getAngleFromString(str) : angle}deg, ${color1}, ${color2})`; } //#endregion exports.stringToColor = stringToColor; exports.stringToCssGradient = stringToCssGradient; exports.stringToGradient = stringToGradient;