Skip to content

Commit

Permalink
feat(parser): add alpha support for color decompose tests
Browse files Browse the repository at this point in the history
Updated the decompose color tests to handle alpha values for HEX and HSL color formats. Also, parser functions have been updated to handle alpha values correctly during color decomposition. Additional tests for RGBA have been added to verify this functionality.
  • Loading branch information
mallikcheripally committed May 29, 2024
1 parent 2c6b7eb commit efb4c15
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
16 changes: 12 additions & 4 deletions src/parser/decomposeColor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { parseHsla } from './parseHsla';
import { parseLch } from './parseLch';
import { parseXyz } from './parseXyz';
import { parseNamedColor } from './parseNamedColor';
import {parseHexAlpha} from "@/parser/parseHexAlpha";

/**
* Converts any valid color format to RGB.
Expand All @@ -24,19 +25,26 @@ export function decomposeColor(color: string): { r: number; g: number; b: number
const format = detectColorFormat(color);

switch (format) {
case 'hex':
case 'hex-alpha': {
case 'hex': {
return hexToRgb(color, false);
}

case 'hex-alpha': {
const rgb = parseHexAlpha(color);
return { r: rgb[0], g: rgb[1], b: rgb[2], a: rgb[3] };
}

case 'hsl': {
const [h, s, l] = parseHsl(color);
return hslToRgb(h, s, l, false);
}

case 'hsla': {
const [hslaH, hslaS, hslaL] = parseHsla(color);
return hslToRgb(hslaH, hslaS, hslaL, false);
const [hslaH, hslaS, hslaL, hslaA] = parseHsla(color);
const rgb = hslToRgb(hslaH, hslaS, hslaL, false);
return {
r: rgb.r, g: rgb.g, b: rgb.b, a: hslaA,
}
}

case 'lab': {
Expand Down
35 changes: 25 additions & 10 deletions tests/parser/decomposeColor.test.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,48 @@
import { decomposeColor } from '@/parser/decomposeColor';

describe('decomposeColor', () => {
test('converts hex to rgb', () => {
test('decomposes HEX color', () => {
expect(decomposeColor('#ff0000')).toEqual({ r: 255, g: 0, b: 0 });
expect(decomposeColor('#00ff00')).toEqual({ r: 0, g: 255, b: 0 });
});

test('converts hsl to rgb', () => {
test('decomposes HEX-ALPHA color', () => {
expect(decomposeColor('#ff000080')).toEqual({ r: 255, g: 0, b: 0, a: 0.502 });
});

test('decomposes HSL color', () => {
expect(decomposeColor('hsl(0, 100%, 50%)')).toEqual({ r: 255, g: 0, b: 0 });
expect(decomposeColor('hsl(120, 100%, 50%)')).toEqual({ r: 0, g: 255, b: 0 });
});

test('converts lab to rgb', () => {
expect(decomposeColor('lab(53.23 80.09 67.2)')).toEqual({ r: 255, g: 0, b: 0 });
test('decomposes HSLA color', () => {
expect(decomposeColor('hsla(0, 100%, 50%, 0.5)')).toEqual({ r: 255, g: 0, b: 0, a: 0.5 });
});

test('converts lch to rgb', () => {
expect(decomposeColor('lch(53.23 104.55 40.00)')).toEqual({ r: 255, g: 0, b: 0 });
test('decomposes LAB color', () => {
expect(decomposeColor('lab(53.23288 80.10933 67.2201)')).toEqual({ r: 255, g: 0, b: 0 });
});

test('converts xyz to rgb', () => {
test('decomposes LCH color', () => {
expect(decomposeColor('lch(53.23288 104.551 40.0)')).toEqual({ r: 255, g: 0, b: 0 });
});

test('decomposes XYZ color', () => {
expect(decomposeColor('xyz(41.24, 21.26, 1.93)')).toEqual({ r: 255, g: 0, b: 0 });
});

test('converts named color to rgb', () => {
test('decomposes RGB color', () => {
expect(decomposeColor('rgb(255, 0, 0)')).toEqual({ r: 255, g: 0, b: 0 });
});

test('decomposes RGBA color', () => {
expect(decomposeColor('rgba(255, 0, 0, 0.5)')).toEqual({ r: 255, g: 0, b: 0, a: 0.5 });
});

test('decomposes named color', () => {
expect(decomposeColor('red')).toEqual({ r: 255, g: 0, b: 0 });
});

test('throws error for invalid color format', () => {
expect(() => decomposeColor('invalid-color')).toThrow('Invalid color format invalid-color');
});
});

0 comments on commit efb4c15

Please sign in to comment.