-
-
Notifications
You must be signed in to change notification settings - Fork 130
[Snippets] Added several new color snippets for JavaScript. #185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Mathys-Gasnier
merged 5 commits into
quicksnip-dev:main
from
pvictordev:snippets/js-color-manipulation
Jan 6, 2025
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
24 changes: 24 additions & 0 deletions
24
snippets/javascript/color-manipulation/hex-to-rgb-color.md
This file contains hidden or 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,24 @@ | ||
--- | ||
title: Hex to RGB Color | ||
description: Converts hexadecimal color code to RGB color values. | ||
author: pvictordev | ||
tags: color,conversion | ||
--- | ||
|
||
```js | ||
function hexToRgb(r, g, b) { | ||
return ( | ||
"#" + | ||
[r, g, b] | ||
.map((x) => { | ||
const hex = x.toString(16); | ||
return hex.length === 1 ? "0" + hex : hex; | ||
}) | ||
.join("") | ||
); | ||
}, | ||
|
||
// 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 hidden or 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 hidden or 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 } | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.