fix(cli): handle 3-digit hex codes in getLuminance

This commit is contained in:
Abhijit Balaji
2026-01-30 12:00:02 -08:00
parent dd64d24725
commit 24ca1bd6f2
2 changed files with 13 additions and 1 deletions

View File

@@ -299,6 +299,15 @@ describe('Color Utils', () => {
it('should handle colors without # prefix', () => {
expect(getLuminance('ffffff')).toBeCloseTo(255);
});
it('should handle 3-digit hex codes', () => {
// #fff -> #ffffff -> 255
expect(getLuminance('#fff')).toBeCloseTo(255);
// #000 -> #000000 -> 0
expect(getLuminance('#000')).toBeCloseTo(0);
// #f00 -> #ff0000 -> 54.213
expect(getLuminance('#f00')).toBeCloseTo(54.213);
});
});
describe('parseX11Rgb', () => {

View File

@@ -298,7 +298,10 @@ export function getThemeTypeFromBackgroundColor(
* @returns Luminance value (0-255)
*/
export function getLuminance(backgroundColor: string): number {
const hex = backgroundColor.replace(/^#/, '');
let hex = backgroundColor.replace(/^#/, '');
if (hex.length === 3) {
hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
}
const r = parseInt(hex.substring(0, 2), 16);
const g = parseInt(hex.substring(2, 4), 16);
const b = parseInt(hex.substring(4, 6), 16);