Data Source to Pastel Color CSS Directive
Generate deterministic pastel colors from any data source string. Produces Angular directive code that hashes input to HSL pastel and applies it to CSS.
About
Assigning consistent, visually distinct colors to dynamic data (usernames, categories, tags) is a common UI requirement that developers often solve with random palettes or hardcoded lookup tables. Both approaches fail at scale. Random colors produce jarring, non-reproducible results. Lookup tables require maintenance and break when new data appears. This tool implements a deterministic hash-to-pastel pipeline: any input string is fed through a DJB2 hash function, producing a stable 32-bit integer. That integer maps to the HSL color space with saturation clamped to 55 - 75% and lightness to 75 - 88%, guaranteeing pastel output regardless of input. The same string always produces the same color. No collisions table. No maintenance.
The tool generates a production-ready Angular directive (NgPastelDirective) that accepts a data source binding and a target CSS property. It calculates the pastel color at runtime and applies it via Renderer2.setStyle. Contrast ratios against white and black backgrounds are computed per WCAG 2.1 so you can verify text readability before shipping. Note: the hash distribution is uniform across hue but not cryptographically secure. Two very similar strings (e.g., "user1" vs "user2") may produce noticeably different hues, which is a feature for distinguishability, not a bug.
Formulas
The core transformation pipeline converts an arbitrary string s into a pastel HSL triplet through three stages.
where hash is initialized to 5381 and s[i] is the character code at position i. The resulting integer maps to hue, saturation, and lightness:
where H โ [0, 360)ยฐ, S โ [55, 75]%, L โ [75, 88]%. The clamped saturation and lightness guarantee pastel output. WCAG 2.1 contrast ratio uses relative luminance:
where L1 is the lighter luminance and L2 is the darker. For each sRGB channel c: if c โค 0.03928, linearize as c12.92, otherwise ((c + 0.055) รท 1.055)2.4. Relative luminance L = 0.2126R + 0.7152G + 0.0722B.
Reference Data
| Data Source Example | DJB2 Hash | Hue (ยฐ) | Saturation (%) | Lightness (%) | Hex Color | Contrast vs White |
|---|---|---|---|---|---|---|
| john.doe | 2090616081 | 321 | 65 | 82 | #E6B8DC | 1.48:1 |
| [email protected] | 3521848527 | 167 | 60 | 80 | #ADD8CC | 1.38:1 |
| category:finance | 1472983610 | 250 | 70 | 85 | #C4BFF0 | 1.42:1 |
| tag-urgent | 988254712 | 352 | 72 | 86 | #F0C4CA | 1.35:1 |
| project-alpha | 2734829156 | 196 | 58 | 78 | #9FD0D8 | 1.45:1 |
| uuid-550e8400 | 3198274561 | 41 | 68 | 81 | #E6D0AD | 1.32:1 |
| status:active | 1187459832 | 112 | 55 | 80 | #B8D8AD | 1.30:1 |
| role:editor | 2845102937 | 77 | 62 | 83 | #D5DEB5 | 1.25:1 |
| team:backend | 614829103 | 223 | 66 | 84 | #B5CEE8 | 1.40:1 |
| region:eu-west | 3729482055 | 295 | 60 | 86 | #D6C2ED | 1.36:1 |
| env:production | 1924581370 | 130 | 57 | 79 | #ADD4B5 | 1.33:1 |
| widget-7 | 2291045813 | 13 | 73 | 84 | #F0C4B8 | 1.34:1 |
| alice | 253389994 | 354 | 63 | 87 | #EDCDD2 | 1.28:1 |
| bob | 193456789 | 29 | 69 | 82 | #E6CDB0 | 1.31:1 |
| department:hr | 3045821976 | 56 | 61 | 80 | #D8D1A8 | 1.26:1 |