Skip to content

Commit e123867

Browse files
committed
Color Functions
1 parent 43cac96 commit e123867

File tree

5 files changed

+148
-0
lines changed

5 files changed

+148
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
title: Hex to HSL Color
3+
description: Converts HEX color values to HSL color values.
4+
author: JanluOfficial
5+
tags: validation,url
6+
---
7+
8+
```js
9+
function hexToHsl(hex) {
10+
// Remove the '#' if present
11+
hex = hex.replace(/^#/, '');
12+
13+
// Convert HEX to RGB
14+
let r = parseInt(hex.substring(0, 2), 16) / 255;
15+
let g = parseInt(hex.substring(2, 4), 16) / 255;
16+
let b = parseInt(hex.substring(4, 6), 16) / 255;
17+
18+
// Find the min, max, and delta
19+
const max = Math.max(r, g, b);
20+
const min = Math.min(r, g, b);
21+
const delta = max - min;
22+
23+
// Calculate Lightness
24+
let l = (max + min) / 2;
25+
26+
// Calculate Saturation
27+
let s = 0;
28+
if (delta !== 0) {
29+
s = delta / (1 - Math.abs(2 * l - 1));
30+
}
31+
32+
// Calculate Hue
33+
let h = 0;
34+
if (delta !== 0) {
35+
if (max === r) {
36+
h = ((g - b) / delta) % 6;
37+
} else if (max === g) {
38+
h = (b - r) / delta + 2;
39+
} else if (max === b) {
40+
h = (r - g) / delta + 4;
41+
}
42+
h = Math.round(h * 60); // Convert to degrees
43+
if (h < 0) h += 360;
44+
}
45+
46+
// Convert saturation and lightness to percentages
47+
s = +(s * 100).toFixed(2);
48+
l = +(l * 100).toFixed(2);
49+
50+
return { h, s, l };
51+
}
52+
53+
// Usage:
54+
hexToHsl("#3498db"); // Returns: { h: 204, s: 69.87, l: 53.14 }
55+
```
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
title: HSL to Hex Color
3+
description: Converts HSL color values to HEX color values.
4+
author: JanluOfficial
5+
tags: validation,url
6+
---
7+
8+
```js
9+
function hslToHex(h, s, l) {
10+
s /= 100;
11+
l /= 100;
12+
13+
let c = (1 - Math.abs(2 * l - 1)) * s;
14+
let x = c * (1 - Math.abs((h / 60) % 2 - 1));
15+
let m = l - c / 2;
16+
17+
let r = 0, g = 0, b = 0;
18+
19+
if (0 <= h && h < 60) {
20+
r = c; g = x; b = 0;
21+
} else if (60 <= h && h < 120) {
22+
r = x; g = c; b = 0;
23+
} else if (120 <= h && h < 180) {
24+
r = 0; g = c; b = x;
25+
} else if (180 <= h && h < 240) {
26+
r = 0; g = x; b = c;
27+
} else if (240 <= h && h < 300) {
28+
r = x; g = 0; b = c;
29+
} else if (300 <= h && h < 360) {
30+
r = c; g = 0; b = x;
31+
}
32+
33+
r = Math.round((r + m) * 255).toString(16).padStart(2, '0');
34+
g = Math.round((g + m) * 255).toString(16).padStart(2, '0');
35+
b = Math.round((b + m) * 255).toString(16).padStart(2, '0');
36+
37+
return `#${r}${g}${b}`;
38+
}
39+
40+
// Usage:
41+
hslToHex(204, 69.87, 53.14); // Returns: "#3498db"
42+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: Random Hex Color
3+
description: Generates a completely random hex color.
4+
author: JanluOfficial
5+
tags: color,hex,random
6+
---
7+
8+
```js
9+
function getRandomHexColor() {
10+
return '#' + Math.floor(Math.random() * 16777215).toString(16).padStart(6, '0');
11+
}
12+
13+
// Usage:
14+
getRandomHexColor(); // Returns: "#RRGGBB" (random)
15+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: Random HSL Color
3+
description: Generates a completely random HSL color.
4+
author: JanluOfficial
5+
tags: color,hsl,random
6+
---
7+
8+
```js
9+
function getRandomHslColor() {
10+
const h = Math.floor(Math.random() * 360);
11+
const s = Math.floor(Math.random() * 101);
12+
const l = Math.floor(Math.random() * 101);
13+
return [h, s, l];
14+
}
15+
16+
// Usage:
17+
getRandomHslColor(); // Returns: (h, s, l) (random)
18+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: Random RGB Color
3+
description: Generates a completely random RGB color.
4+
author: JanluOfficial
5+
tags: color,rgb,random
6+
---
7+
8+
```js
9+
function getRandomRgbColor() {
10+
const r = Math.floor(Math.random() * 256);
11+
const g = Math.floor(Math.random() * 256);
12+
const b = Math.floor(Math.random() * 256);
13+
return (r, g, b);
14+
}
15+
16+
// Usage:
17+
getRandomRgbColor(); // Returns: (r, g, b) (random)
18+
```

0 commit comments

Comments
 (0)