-
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #185 from pvictordev/snippets/js-color-manipulation
[Snippets] Added several new color snippets for JavaScript.
- Loading branch information
Showing
4 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
snippets/javascript/color-manipulation/hex-to-rgb-color.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
--- | ||
title: Hex to RGB Color | ||
description: Converts hexadecimal color code to RGB color values. | ||
author: pvictordev | ||
tags: color,conversion | ||
--- | ||
|
||
```js | ||
function hexToRgb(hex) { | ||
let sanitizedHex = hex.startsWith("#") ? hex.slice(1) : hex; | ||
|
||
if (sanitizedHex.length === 3) { | ||
sanitizedHex = [...sanitizedHex].map((char) => char + char).join(""); | ||
} | ||
|
||
const bigint = parseInt(sanitizedHex, 16); | ||
|
||
return { | ||
r: (bigint >> 16) & 0xff, | ||
g: (bigint >> 8) & 0xff, | ||
b: bigint & 0xff, | ||
}; | ||
} | ||
|
||
// Usage: | ||
console.log(hexToRgb("#ff5733")); // { r: 255, g: 87, b: 51 } | ||
console.log(hexToRgb("#ffff")); // { r: 0, g: 255, b: 255 } | ||
``` |
34 changes: 34 additions & 0 deletions
34
snippets/javascript/color-manipulation/hsl-to-rgb-color.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
--- | ||
title: HSL to RGB Color | ||
description: Converts HSL color values to RGB color values. | ||
author: pvictordev | ||
tags: color,conversion | ||
--- | ||
|
||
```js | ||
function hslToRgb(h, s, l) { | ||
s /= 100; | ||
l /= 100; | ||
const c = (1 - Math.abs(2 * l - 1)) * s; | ||
const x = c * (1 - Math.abs((h / 60) % 2 - 1)); | ||
const m = l - c / 2; | ||
|
||
const [r, g, b] = | ||
h < 60 ? [c, x, 0] : | ||
h < 120 ? [x, c, 0] : | ||
h < 180 ? [0, c, x] : | ||
h < 240 ? [0, x, c] : | ||
h < 300 ? [x, 0, c] : | ||
[c, 0, x]; | ||
|
||
return { | ||
r: Math.round((r + m) * 255), | ||
g: Math.round((g + m) * 255), | ||
b: Math.round((b + m) * 255), | ||
}; | ||
} | ||
|
||
// Usage: | ||
console.log(hslToRgb(14, 100, 60)); // { r: 255, g: 87, b: 51 } | ||
console.log(hslToRgb(0, 0, 100)); // { r: 255, g: 255, b: 255 } | ||
``` |
37 changes: 37 additions & 0 deletions
37
snippets/javascript/color-manipulation/rgb-to-hsl-color.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--- | ||
title: RGB to HSL Color | ||
description: Converts RGB color values to HSL color values. | ||
author: pvictordev | ||
tags: color,conversion | ||
--- | ||
|
||
```js | ||
function rgbToHsl(r, g, b) { | ||
[r, g, b] = [r, g, b].map((v) => v / 255); | ||
|
||
const max = Math.max(r, g, b); | ||
const min = Math.min(r, g, b); | ||
const delta = max - min; | ||
|
||
const l = (max + min) / 2; | ||
|
||
if (delta === 0) return { h: 0, s: 0, l: Math.round(l * 100) }; | ||
|
||
const s = l > 0.5 ? delta / (2 - max - min) : delta / (max + min); | ||
|
||
const h = | ||
max === r ? ((g - b) / delta + (g < b ? 6 : 0)) : | ||
max === g ? (b - r) / delta + 2 : | ||
(r - g) / delta + 4; | ||
|
||
return { | ||
h: Math.round(h * 60), | ||
s: Math.round(s * 100), | ||
l: Math.round(l * 100), | ||
}; | ||
} | ||
|
||
// Usage: | ||
console.log(rgbToHsl(255, 87, 51)); // { h: 14, s: 100, l: 60 } | ||
console.log(rgbToHsl(255, 255, 255)); // { h: 0, s: 0, l: 100 } | ||
``` |