Skip to content

Commit 57d6f98

Browse files
Merge pull request #185 from pvictordev/snippets/js-color-manipulation
[Snippets] Added several new color snippets for JavaScript.
2 parents 80c096e + 755ae17 commit 57d6f98

File tree

4 files changed

+132
-0
lines changed

4 files changed

+132
-0
lines changed

public/consolidated/javascript.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,28 @@
8080
{
8181
"name": "Color Manipulation",
8282
"snippets": [
83+
{
84+
"title": "Hex to RGB Color",
85+
"description": "Converts hexadecimal color code to RGB color values.",
86+
"author": "pvictordev",
87+
"tags": [
88+
"color",
89+
"conversion"
90+
],
91+
"contributors": [],
92+
"code": "function hexToRgb(hex) {\n let sanitizedHex = hex.startsWith(\"#\") ? hex.slice(1) : hex;\n\n if (sanitizedHex.length === 3) {\n sanitizedHex = [...sanitizedHex].map((char) => char + char).join(\"\");\n }\n\n const bigint = parseInt(sanitizedHex, 16);\n\n return {\n r: (bigint >> 16) & 0xff, \n g: (bigint >> 8) & 0xff, \n b: bigint & 0xff, \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"
93+
},
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+
},
83105
{
84106
"title": "RGB to Hex Color",
85107
"description": "Converts RGB color values to hexadecimal color code.",
@@ -90,6 +112,17 @@
90112
],
91113
"contributors": [],
92114
"code": "function rgbToHex(r, g, b) {\n const toHex = (n) => {\n const hex = n.toString(16);\n return hex.length === 1 ? '0' + hex : hex;\n };\n \n return '#' + toHex(r) + toHex(g) + toHex(b);\n}\n\n// Usage:\nconsole.log(rgbToHex(255, 128, 0)); // Output: \"#ff8000\"\nconsole.log(rgbToHex(0, 255, 0)); // Output: \"#00ff00\"\n"
115+
},
116+
{
117+
"title": "RGB to HSL Color",
118+
"description": "Converts RGB color values to HSL color values.",
119+
"author": "pvictordev",
120+
"tags": [
121+
"color",
122+
"conversion"
123+
],
124+
"contributors": [],
125+
"code": "function rgbToHsl(r, g, b) {\n [r, g, b] = [r, g, b].map((v) => v / 255);\n\n const max = Math.max(r, g, b);\n const min = Math.min(r, g, b);\n const delta = max - min;\n\n const l = (max + min) / 2;\n\n if (delta === 0) return { h: 0, s: 0, l: Math.round(l * 100) };\n\n const s = l > 0.5 ? delta / (2 - max - min) : delta / (max + min);\n\n const h = \n max === r ? ((g - b) / delta + (g < b ? 6 : 0)) :\n max === g ? (b - r) / delta + 2 :\n (r - g) / delta + 4;\n\n return {\n h: Math.round(h * 60), \n s: Math.round(s * 100),\n l: Math.round(l * 100), \n };\n}\n\n// Usage:\nconsole.log(rgbToHsl(255, 87, 51)); // { h: 14, s: 100, l: 60 }\nconsole.log(rgbToHsl(255, 255, 255)); // { h: 0, s: 0, l: 100 }\n"
93126
}
94127
]
95128
},
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
title: Hex to RGB Color
3+
description: Converts hexadecimal color code to RGB color values.
4+
author: pvictordev
5+
tags: color,conversion
6+
---
7+
8+
```js
9+
function hexToRgb(hex) {
10+
let sanitizedHex = hex.startsWith("#") ? hex.slice(1) : hex;
11+
12+
if (sanitizedHex.length === 3) {
13+
sanitizedHex = [...sanitizedHex].map((char) => char + char).join("");
14+
}
15+
16+
const bigint = parseInt(sanitizedHex, 16);
17+
18+
return {
19+
r: (bigint >> 16) & 0xff,
20+
g: (bigint >> 8) & 0xff,
21+
b: bigint & 0xff,
22+
};
23+
}
24+
25+
// Usage:
26+
console.log(hexToRgb("#ff5733")); // { r: 255, g: 87, b: 51 }
27+
console.log(hexToRgb("#ffff")); // { r: 0, g: 255, b: 255 }
28+
```
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+
```
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
title: RGB to HSL Color
3+
description: Converts RGB color values to HSL color values.
4+
author: pvictordev
5+
tags: color,conversion
6+
---
7+
8+
```js
9+
function rgbToHsl(r, g, b) {
10+
[r, g, b] = [r, g, b].map((v) => v / 255);
11+
12+
const max = Math.max(r, g, b);
13+
const min = Math.min(r, g, b);
14+
const delta = max - min;
15+
16+
const l = (max + min) / 2;
17+
18+
if (delta === 0) return { h: 0, s: 0, l: Math.round(l * 100) };
19+
20+
const s = l > 0.5 ? delta / (2 - max - min) : delta / (max + min);
21+
22+
const h =
23+
max === r ? ((g - b) / delta + (g < b ? 6 : 0)) :
24+
max === g ? (b - r) / delta + 2 :
25+
(r - g) / delta + 4;
26+
27+
return {
28+
h: Math.round(h * 60),
29+
s: Math.round(s * 100),
30+
l: Math.round(l * 100),
31+
};
32+
}
33+
34+
// Usage:
35+
console.log(rgbToHsl(255, 87, 51)); // { h: 14, s: 100, l: 60 }
36+
console.log(rgbToHsl(255, 255, 255)); // { h: 0, s: 0, l: 100 }
37+
```

0 commit comments

Comments
 (0)