Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.

Commit 42d1c23

Browse files
sergixnetKent C. Dodds
authored and
Kent C. Dodds
committed
feat: Added hex to HSL color conversion function (#231)
1 parent 25bd8a5 commit 42d1c23

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

Diff for: src/hex2hsl.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/* eslint-disable no-unused-vars */
2+
export default hex2hsl
3+
4+
/**
5+
* Original Source: https://stackoverflow.com/questions/46432335/hex-to-hsl-convert-javascript
6+
*
7+
* This method will convert colors in Hex to HSL format.
8+
*
9+
* @param {String} hex - The Hex value to be converted
10+
* @return {String} - The HSL value of the color
11+
*/
12+
13+
14+
// eslint-disable-next-line complexity
15+
function hex2hsl(hex) {
16+
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex)
17+
18+
let r = parseInt(result[1], 16)
19+
let g = parseInt(result[2], 16)
20+
let b = parseInt(result[3], 16)
21+
22+
r /= 255
23+
g /= 255
24+
b /= 255
25+
26+
const max = Math.max(r, g, b)
27+
const min = Math.min(r, g, b)
28+
29+
let h = (max + min) / 2
30+
let s = (max + min) / 2
31+
let l = (max + min) / 2
32+
33+
if (max === min) {
34+
h = s = 0 // achromatic
35+
} else {
36+
const d = max - min
37+
s = l > 0.5 ? d / (2 - max - min) : d / (max + min)
38+
// eslint-disable-next-line default-case
39+
switch (max) {
40+
case r: h = (g - b) / d + (g < b ? 6 : 0)
41+
break
42+
case g: h = (b - r) / d + 2
43+
break
44+
case b: h = (r - g) / d + 4
45+
break
46+
}
47+
h /= 6
48+
}
49+
50+
s = s * 100
51+
s = Math.round(s)
52+
l = l * 100
53+
l = Math.round(l)
54+
h = Math.round(360 * h)
55+
return `hsl(${h}, ${s}%, ${l}%)`
56+
}

Diff for: src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import getOrdinalSuffix from './get-ordinal-suffix'
2727
import getQueryStringParam from './get-query-string-param'
2828
import getQueryStringValue from './get-query-string-value'
2929
import getMiddle from './getMiddle'
30+
import hex2hsl from './hex2hsl'
3031
import hex2rgb from './hex2rgb'
3132
import inchesToMetric from './inches-to-metric'
3233
import initArray from './init-array'
@@ -178,4 +179,5 @@ export {
178179
inchesToMetric,
179180
numberToString,
180181
largest,
182+
hex2hsl,
181183
}

Diff for: test/hex2hsl.test.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import test from 'ava'
2+
import {hex2hsl} from '../src'
3+
4+
test('test red color', t => {
5+
const hex = '#ff0000'
6+
const expected = 'hsl(0, 100%, 50%)'
7+
const actual = hex2hsl(hex)
8+
t.deepEqual(actual, expected)
9+
})
10+
11+
test('test green color', t => {
12+
const hex = '#00ff00'
13+
const expected = 'hsl(120, 100%, 50%)'
14+
const actual = hex2hsl(hex)
15+
t.deepEqual(actual, expected)
16+
})
17+
18+
test('test blue color', t => {
19+
const hex = '#0000ff'
20+
const expected = 'hsl(240, 100%, 50%)'
21+
const actual = hex2hsl(hex)
22+
t.deepEqual(actual, expected)
23+
})
24+
25+
test('test fabada color', t => {
26+
const hex = '#fabada'
27+
const expected = 'hsl(330, 86%, 85%)'
28+
const actual = hex2hsl(hex)
29+
t.deepEqual(actual, expected)
30+
})
31+
32+
test('test black color', t => {
33+
const hex = '#000000'
34+
const expected = 'hsl(0, 0%, 0%)'
35+
const actual = hex2hsl(hex)
36+
t.deepEqual(actual, expected)
37+
})

0 commit comments

Comments
 (0)