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
133 lines (91 loc) โข 4.08 kB
Markdown
# ๐จ string-to-color-gradient
### **[Live Interactive Demo - Click Here](https://stackblitz.com/edit/vitejs-vite-ljuk5lkm?file=src%2FApp.tsx)**
<p align="center">
<img src="https://github.com/prajwl-dh/string-to-color-gradient/blob/main/Str2HexDemo.gif?raw=true" alt="Demo 1" width="45%" />
<img src="https://github.com/prajwl-dh/string-to-color-gradient/blob/main/Str2CssGradDemo.gif?raw=true" alt="Demo 2" width="45%" />
</p>
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
```bash
npm install string-to-color-gradient
```
or with Yarn:
```bash
yarn add string-to-color-gradient
```
## ๐ Quick Start
```ts
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.
```ts
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**.
```ts
stringToColor('Hello World', 'light');
// => "#c4a2e1"
```
### `stringToGradient(str: string, options?: GradientOptions): [string, string]`
๐น Returns a **tuple of two hex colors** to create gradients.
```ts
stringToGradient('Hello World', { brightness: 'dark' });
// => ["#7a2b7c", "#2c8d4d"]
```
---
### `stringToCssGradient(str: string, options?: GradientOptions): string`
๐น Returns a complete **CSS `linear-gradient(...)` string**.
```ts
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]