Skip to content

Commit 20f8f19

Browse files
pvictordevACR1209
authored andcommitted
snippets: hsl to rgb color
1 parent 282b259 commit 20f8f19

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

public/consolidated/javascript.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,17 @@
9191
"contributors": [],
9292
"code": "function hexToRgb(r, g, b) {\n return (\n \"#\" +\n [r, g, b]\n .map((x) => {\n const hex = x.toString(16);\n return hex.length === 1 ? \"0\" + hex : hex;\n })\n .join(\"\")\n );\n},\n\n// Usage:\nconsole.log(hexToRgb(\"#ff5733\")); // { r: 255, g: 87, b: 51 }\nconsole.log(hexToRgb(\"#ffff\")); // { r: 0, g: 255, b: 255 }\n"
9393
},
94+
{
95+
"title": "HSL to RGB Color",
96+
"description": "Converts HSL color values to RGB color values.",
97+
"author": "pvictordev",
98+
"tags": [
99+
"color",
100+
"conversion"
101+
],
102+
"contributors": [],
103+
"code": "function hslToRgb(h, s, l) {\n s /= 100;\n l /= 100;\n const c = (1 - Math.abs(2 * l - 1)) * s;\n const x = c * (1 - Math.abs((h / 60) % 2 - 1));\n const m = l - c / 2;\n\n const [r, g, b] = \n h < 60 ? [c, x, 0] :\n h < 120 ? [x, c, 0] :\n h < 180 ? [0, c, x] :\n h < 240 ? [0, x, c] :\n h < 300 ? [x, 0, c] :\n [c, 0, x];\n\n return {\n r: Math.round((r + m) * 255),\n g: Math.round((g + m) * 255),\n b: Math.round((b + m) * 255),\n };\n}\n\n// Usage:\nconsole.log(hslToRgb(14, 100, 60)); // { r: 255, g: 87, b: 51 }\nconsole.log(hslToRgb(0, 0, 100)); // { r: 255, g: 255, b: 255 }\n"
104+
},
94105
{
95106
"title": "RGB to Hex Color",
96107
"description": "Converts RGB color values to hexadecimal color code.",
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
title: HSL to RGB Color
3+
description: Converts HSL color values to RGB color values.
4+
author: pvictordev
5+
tags: color,conversion
6+
---
7+
8+
```js
9+
function hslToRgb(h, s, l) {
10+
s /= 100;
11+
l /= 100;
12+
const c = (1 - Math.abs(2 * l - 1)) * s;
13+
const x = c * (1 - Math.abs((h / 60) % 2 - 1));
14+
const m = l - c / 2;
15+
16+
const [r, g, b] =
17+
h < 60 ? [c, x, 0] :
18+
h < 120 ? [x, c, 0] :
19+
h < 180 ? [0, c, x] :
20+
h < 240 ? [0, x, c] :
21+
h < 300 ? [x, 0, c] :
22+
[c, 0, x];
23+
24+
return {
25+
r: Math.round((r + m) * 255),
26+
g: Math.round((g + m) * 255),
27+
b: Math.round((b + m) * 255),
28+
};
29+
}
30+
31+
// Usage:
32+
console.log(hslToRgb(14, 100, 60)); // { r: 255, g: 87, b: 51 }
33+
console.log(hslToRgb(0, 0, 100)); // { r: 255, g: 255, b: 255 }
34+
```

0 commit comments

Comments
 (0)