Skip to content

Commit

Permalink
refactor: improve parsing of color formats in parseColorToRgba
Browse files Browse the repository at this point in the history
The code for parsing different color formats has been refactored to significantly improve its coherence and maintainability. Each color type's parsing now uses a consistent structure of destructuring the parsed values and returning color in specific format. In addition, replaced the hexToRgb conversion function with the parseHex function to reduce complexity.
  • Loading branch information
mallikcheripally committed Jun 3, 2024
1 parent c45a610 commit f3b7049
Showing 1 changed file with 32 additions and 28 deletions.
60 changes: 32 additions & 28 deletions src/parser/parseColorToRgba.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { hexToRgb } from '@/conversions/hexToRgb';
import { hslToRgb } from '@/conversions/hslToRgb';
import { labToRgb } from '@/conversions/labToRgb';
import { lchToRgb } from '@/conversions/lchToRgb';
Expand All @@ -13,6 +12,7 @@ import { parseLch } from './parseLch';
import { parseXyz } from './parseXyz';
import { parseNamedColor } from './parseNamedColor';
import { parseHexAlpha } from './parseHexAlpha';
import { parseHex } from '@/parser/parseHex';

/**
* Converts any valid color format to RGB.
Expand All @@ -26,66 +26,70 @@ export function parseColorToRgba(color: string): { r: number; g: number; b: numb

switch (format) {
case 'hex': {
return hexToRgb(color, false);
const { r, g, b } = parseHex(color);
return { r, g, b };
}

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

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

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

case 'lab': {
const [lVal, a, b] = parseLab(color);
return labToRgb(lVal, a, b, false);
const { l, a, b, alphaNum } = parseLab(color);
const rgb = labToRgb(l, a, b, false);
return {
r: rgb.r,
g: rgb.g,
b: rgb.b,
a: alphaNum,
};
}

case 'lch': {
const [lchL, c, hVal] = parseLch(color);
return lchToRgb(lchL, c, hVal, false);
const { l, c, hDeg, alphaNum } = parseLch(color);
const rgb = lchToRgb(l, c, hDeg, false);
return {
r: rgb.r,
g: rgb.g,
b: rgb.b,
a: alphaNum,
};
}

case 'xyz': {
const [x, y, z] = parseXyz(color);
const { x, y, z } = parseXyz(color);
return xyzToRgb(x, y, z, false);
}

case 'rgb': {
const rgb = parseRgb(color);
return {
r: rgb[0],
g: rgb[1],
b: rgb[2],
};
const { rNum, gNum, bNum } = parseRgb(color);
return { r: rNum, g: gNum, b: bNum, };
}

case 'rgba': {
const rgba = parseRgba(color);
return {
r: rgba[0],
g: rgba[1],
b: rgba[2],
a: rgba[3],
};
const { rNum, gNum, bNum, aNum } = parseRgba(color);
return { r: rNum, g: gNum, b: bNum, a: aNum, };
}

case 'named': {
const [r, g, b] = parseNamedColor(color);
const { r, g, b } = parseNamedColor(color);
return { r, g, b };
}

Expand Down

0 comments on commit f3b7049

Please sign in to comment.