Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.7k
feat: add HSL to RGB color space conversion algorithm#1518
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
piyushk77 wants to merge2 commits intoTheAlgorithms:masterChoose a base branch frompiyushk77:patch-4
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
Show all changes
2 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* Given a color in HSL format, convert it to RGB format. | ||
* | ||
* @see | ||
* For more info: https://www.niwa.nu/2013/05/math-behind-colorspace-conversions-rgb-hsl/ | ||
* | ||
* @param {number[]} colorHsl - One dimensional array of integers (HSL color format). | ||
* @returns {number[]} - One dimensional array of integers (RGB color format). | ||
* | ||
* @example | ||
* const colorHsl = [120, 50, 15] | ||
* | ||
* const result = hslToRgb(colorHsl) | ||
* | ||
* // The function returns the corresponding color in RGB format: | ||
* // result = [19, 57, 19] | ||
*/ | ||
const checkHslFormat = (colorHsl) => { | ||
return ( | ||
colorHsl[0] >= 0 && | ||
colorHsl[0] <= 360 && | ||
colorHsl[1] >= 0 && | ||
colorHsl[1] <= 100 && | ||
colorHsl[2] >= 0 && | ||
colorHsl[2] <= 100 | ||
) | ||
} | ||
const hslToRgb = (colorHsl) => { | ||
if (!checkHslFormat(colorHsl)) { | ||
throw new Error('Input is not a valid HSL color.') | ||
} | ||
let luminance = colorHsl[2] / 100 | ||
// Check if color is a shade of grey | ||
if (colorHsl[1] === 0) { | ||
let colorRgb = new Array(3).fill(255 * luminance) | ||
return colorRgb | ||
} | ||
let firstFormula = 0 | ||
if (luminance < 0.5) { | ||
firstFormula = luminance * (1 + colorHsl[1] / 100) | ||
} else { | ||
firstFormula = | ||
luminance + colorHsl[1] / 100 - (luminance * colorHsl[1]) / 100 | ||
} | ||
let secondFormula = 2 * luminance - firstFormula | ||
// Normalize Hue | ||
let hue = colorHsl[0] / 360 | ||
let colorRgb = colorHsl | ||
colorRgb[0] = hue + 1 / 3 | ||
colorRgb[1] = hue | ||
colorRgb[2] = hue - 1 / 3 | ||
for (let i = 0; i <= 2; i++) { | ||
if (colorRgb[i] < 0) { | ||
colorRgb[i] += 1 | ||
} else if (colorRgb[i] > 1) { | ||
colorRgb[i] -= 1 | ||
} | ||
if (6 * colorRgb[i] < 1) { | ||
colorRgb[i] = | ||
secondFormula + (firstFormula - secondFormula) * 6 * colorRgb[i] | ||
} else if (2 * colorRgb[i] < 1) { | ||
colorRgb[i] = firstFormula | ||
} else if (3 * colorRgb[i] < 2) { | ||
colorRgb[i] = | ||
secondFormula + | ||
(firstFormula - secondFormula) * 6 * (2 / 3 - colorRgb[i]) | ||
} else { | ||
colorRgb[i] = secondFormula | ||
} | ||
colorRgb[i] = Math.round(colorRgb[i] * 255) | ||
} | ||
return colorRgb | ||
} | ||
export { hslToRgb } |
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,44 @@ | ||
import { hslToRgb } from '../HslToRgbConversion' | ||
describe('HslToRgbConversion', () => { | ||
test.each([ | ||
[ | ||
[311, 84, 46], | ||
[216, 19, 180] | ||
], | ||
[ | ||
[119, 83, 41], | ||
[21, 191, 18] | ||
], | ||
[ | ||
[225, 33, 47], | ||
[80, 100, 159] | ||
], | ||
[ | ||
[349, 98, 16], | ||
[81, 1, 15] | ||
], | ||
[ | ||
[96, 100, 4], | ||
[8, 20, 0] | ||
], | ||
[ | ||
[0, 0, 0], | ||
[0, 0, 0] | ||
], | ||
[ | ||
[0, 0, 100], | ||
[255, 255, 255] | ||
] | ||
])('Should return the color in RGB format.', (colorHsl, expected) => { | ||
expect(hslToRgb(colorHsl)).toEqual(expected) | ||
}) | ||
test.each([ | ||
[[360, 180, 9], 'Input is not a valid HSL color.'], | ||
[[-90, 46, 8], 'Input is not a valid HSL color.'], | ||
[[1, 39, 900], 'Input is not a valid HSL color.'], | ||
[[1, 139, 100], 'Input is not a valid HSL color.'] | ||
])('Should return the error message.', (colorHsl, expected) => { | ||
expect(() => hslToRgb(colorHsl)).toThrowError(expected) | ||
}) | ||
}) |
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.