JSPM

string-to-color-gradient

1.0.8
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 13
  • Score
    100M100P100Q27599F
  • License MIT

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 the same every time.

Package Exports

  • string-to-color-gradient

Readme

๐ŸŽจ string-to-color-gradient

Live Interactive Demo - Click Here

Demo 1 Demo 2

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 the same every time.

โœจ Features

  • ๐Ÿ” Deterministic output โ€“ same input always gives the same result
  • ๐Ÿ’ก Color Brightness control โ€“ choose from light, normal, or dark styles
  • ๐ŸŽจ Hex colors or full CSS gradients
  • ๐Ÿ”’ Hash-based uniqueness using MD5, SHA-1, or SHA-256
  • ๐Ÿ“ฆ Tiny footprint โ€“ zero config, ready to use
  • โšก Fast and dependency-light โ€“ built for performance

๐Ÿ“ฆ Installation

npm install string-to-color-gradient

or with Yarn:

yarn add string-to-color-gradient

๐Ÿš€ Quick Start

import {
  stringToColor,
  stringToGradient,
  stringToCssGradient,
} from 'string-to-color-gradient';

const color = stringToColor('hello world');
// => "#d87c3a"

const [start, end] = stringToGradient('hello world');
// => ["#d87c3a", "#4e92bf"]

const cssGradient = stringToCssGradient('hello world');
// => "linear-gradient(123deg, #d87c3a, #4e92bf)"

โš™๏ธ Options

Customize the output using the optional GradientOptions object.

interface GradientOptions {
  brightness?: 'dark' | 'normal' | 'light'; // default: 'normal'
  angle?: 'auto' | number; // default: 'auto'
}

๐Ÿ’ก Brightness

Value Description Visual Style
light Softer, pastel tones ๐ŸŒค๏ธ Light & airy
normal Balanced default โ˜๏ธ Neutral tones
dark Rich and saturated ๐ŸŒ‘ Deep contrast

๐Ÿ“ Angle

  • 'auto' โ€“ angle is generated from the string hash (default)
  • number โ€“ specify a custom angle in degrees (e.g., 45, 120)

๐Ÿงฉ API Reference

stringToColor(str: string, brightness?: Brightness): string

๐Ÿ”น Converts a string to a single hex color.

stringToColor('Hello World', 'light');
// => "#c4a2e1"

stringToGradient(str: string, options?: GradientOptions): [string, string]

๐Ÿ”น Returns a tuple of two hex colors to create gradients.

stringToGradient('Hello World', { brightness: 'dark' });
// => ["#7a2b7c", "#2c8d4d"]

stringToCssGradient(str: string, options?: GradientOptions): string

๐Ÿ”น Returns a complete CSS linear-gradient(...) string.

stringToCssGradient('Hello World', { angle: 45 });
// => "linear-gradient(45deg, #a1b2c3, #d4e5f6)"

๐Ÿ›  Use Cases

Make your UI more vibrant, consistent, and automatic โ€” no color picking required:

  • ๐Ÿ‘ค Avatar backgrounds โ€“ assign users a unique, consistent color
  • ๐Ÿท๏ธ Tag & label colors โ€“ color-code categories, topics, or tags
  • ๐Ÿ’ฌ Chat bubbles โ€“ visually distinguish different senders
  • ๐Ÿ“Š Data visualizations โ€“ generate consistent palette from labels or IDs
  • ๐Ÿ–ผ๏ธ Theming UI elements โ€“ change styling based on user/project/content
  • ๐Ÿงพ Color-coded identifiers โ€“ visually distinguish IDs, usernames, emails
  • ๐Ÿ“ฐ Blog post or card backgrounds โ€“ create dynamic, unique color cards for each post
  • ๐Ÿ—‚๏ธ Kanban boards or dashboards โ€“ assign colors to tasks or projects by title
  • ๐ŸŽŸ๏ธ Event/ticketing systems โ€“ auto-assign color badges per attendee or ticket

๐Ÿค Contributing

Contributions are welcome! Feel free to open issues, suggest features, or submit pull requests.

๐Ÿ“„ License

MIT License ยฉ [Prajwal Dhungana]