Re-scoped from @lilith/ui-theme to @cocotte/ui-theme. In-set cross-package deps re-pointed to @cocotte; out-of-set @lilith deps preserved (same Verdaccio). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
51 lines
No EOL
1.6 KiB
JavaScript
51 lines
No EOL
1.6 KiB
JavaScript
import { hexToRgb } from './contrast';
|
|
function clamp(value, min, max) {
|
|
return Math.min(Math.max(value, min), max);
|
|
}
|
|
function rgbToHex(r, g, b) {
|
|
const toHex = (c) => clamp(Math.round(c), 0, 255).toString(16).padStart(2, '0');
|
|
return `#${toHex(r)}${toHex(g)}${toHex(b)}`;
|
|
}
|
|
/**
|
|
* Mix two hex colors together.
|
|
* weight=0 returns color1, weight=1 returns color2.
|
|
*/
|
|
export function mix(color1, color2, weight = 0.5) {
|
|
const [r1, g1, b1] = hexToRgb(color1);
|
|
const [r2, g2, b2] = hexToRgb(color2);
|
|
return rgbToHex(r1 * (1 - weight) + r2 * weight, g1 * (1 - weight) + g2 * weight, b1 * (1 - weight) + b2 * weight);
|
|
}
|
|
/**
|
|
* Lighten a hex color by mixing it toward white.
|
|
* amount is 0-100 (percentage).
|
|
*/
|
|
export function lighten(hex, amount) {
|
|
return mix(hex, '#ffffff', amount / 100);
|
|
}
|
|
/**
|
|
* Darken a hex color by mixing it toward black.
|
|
* amount is 0-100 (percentage).
|
|
*/
|
|
export function darken(hex, amount) {
|
|
return mix(hex, '#000000', amount / 100);
|
|
}
|
|
/**
|
|
* Convert a hex color to an rgba() CSS string.
|
|
* alpha is clamped to 0-1.
|
|
*/
|
|
export function rgba(hex, alpha) {
|
|
const [r, g, b] = hexToRgb(hex);
|
|
const a = clamp(alpha, 0, 1);
|
|
return `rgba(${r}, ${g}, ${b}, ${a})`;
|
|
}
|
|
/**
|
|
* Append an alpha channel to a hex color, returning an 8-digit hex string.
|
|
* alpha is clamped to 0-1 and converted to a 0-255 byte.
|
|
*/
|
|
export function withAlpha(hex, alpha) {
|
|
const [r, g, b] = hexToRgb(hex);
|
|
const a = clamp(alpha, 0, 1);
|
|
const aa = Math.round(a * 255).toString(16).padStart(2, '0');
|
|
return rgbToHex(r, g, b) + aa;
|
|
}
|
|
//# sourceMappingURL=color.js.map
|