MCPcopy Create free account
hub / github.com/disler/benchy / stringToColor

Function stringToColor

client/src/utils.ts:10–27  ·  view source on GitHub ↗
(str: string)

Source from the content-addressed store, hash-verified

8 }
9}
10export function stringToColor(str: string): string {
11 // Generate hash from string
12 let hash = 0;
13 for (let i = 0; i < str.length; i++) {
14 hash = str.charCodeAt(i) + ((hash << 5) - hash);
15 }
16
17 // Convert to HSL for theme-appropriate colors
18 const h = Math.abs(hash) % 360; // Hue: 0-360
19 const s = 50 + (Math.abs(hash >> 8) % 30); // Saturation: 50-80%
20 const l = 25 + (Math.abs(hash >> 16) % 20); // Lightness: 25-45% for dark backgrounds
21
22 // Add secondary hue rotation for more variation
23 const h2 = (h + 137) % 360; // Golden angle rotation
24 const finalHue = hash % 2 === 0 ? h : h2;
25
26 return `hsl(${finalHue}, ${s}%, ${l}%)`;
27}
28
29// Get contrasting text color for a background
30export function getContrastTextColor(backgroundColor: string): string {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected