Browse Source

🔧 Refactor hexToRGB function

📊 Improve readability and maintainability
🌈 Handle both 3 and 6-digit hex values
🎨 Update variable names for clarity
🚀 Ready for enhanced color handling!
main
John Doe 1 year ago
parent
commit
31f596f01f
  1. 21
      src/utils/hexToRgb.ts

21
src/utils/hexToRgb.ts

@ -0,0 +1,21 @@
const hexToRGB = (h: string) => {
let r: number | string = 0,
g: number | string = 0,
b: number | string = 0;
// 3 digits
if (h.length == 4) {
r = "0x" + h[1] + h[1];
g = "0x" + h[2] + h[2];
b = "0x" + h[3] + h[3];
// 6 digits
} else if (h.length == 7) {
r = "0x" + h[1] + h[2];
g = "0x" + h[3] + h[4];
b = "0x" + h[5] + h[6];
}
return "rgb(" + +r + "," + +g + "," + +b + ")";
};
export default hexToRGB;
Loading…
Cancel
Save