Files
BRETT/app/lib/color.ts
T
2026-10-03 20:07:49 +02:00

21 lines
1004 B
TypeScript

export function hexToRgbTuple(hex: string): [number, number, number] {
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex)
if (!result) return [1, 1, 1]
return [Number.parseInt(result[1]!, 16) / 255, Number.parseInt(result[2]!, 16) / 255, Number.parseInt(result[3]!, 16) / 255]
}
/** Mischt zwei Hexfarben, t=0 → a, t=1 → b */
export function mixHex(a: string, b: string, t: number): string {
const ca = hexToRgbTuple(a)
const cb = hexToRgbTuple(b)
const r = Math.round((ca[0] + (cb[0] - ca[0]) * t) * 255)
const g = Math.round((ca[1] + (cb[1] - ca[1]) * t) * 255)
const bl = Math.round((ca[2] + (cb[2] - ca[2]) * t) * 255)
return `#${r.toString(16).padStart(2, '0')}${g.toString(16).padStart(2, '0')}${bl.toString(16).padStart(2, '0')}`
}
/** Hellt (amount > 0) oder dunkelt (amount < 0) eine Hexfarbe ab */
export function shadeHex(hex: string, amount: number): string {
return amount >= 0 ? mixHex(hex, '#ffffff', amount) : mixHex(hex, '#000000', -amount)
}