Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions Conversions/HslToRgbConversion.js
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 }
44 changes: 44 additions & 0 deletions Conversions/test/HslToRgbConversion.test.js
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)
})
})