From 31f596f01f5d0aeccded223724c4f414aa580568 Mon Sep 17 00:00:00 2001 From: John Doe Date: Sat, 9 Sep 2023 16:49:56 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Refactor=20hexToRGB=20function?= =?UTF-8?q?=20=F0=9F=93=8A=20Improve=20readability=20and=20maintainability?= =?UTF-8?q?=20=F0=9F=8C=88=20Handle=20both=203=20and=206-digit=20hex=20val?= =?UTF-8?q?ues=20=F0=9F=8E=A8=20Update=20variable=20names=20for=20clarity?= =?UTF-8?q?=20=F0=9F=9A=80=20Ready=20for=20enhanced=20color=20handling!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/hexToRgb.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/utils/hexToRgb.ts diff --git a/src/utils/hexToRgb.ts b/src/utils/hexToRgb.ts new file mode 100644 index 0000000..bfb2c31 --- /dev/null +++ b/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;