-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
57 lines (49 loc) · 1.32 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const makeColorAccessible = require('make-color-accessible')
const Color = require('color2')
const chain = require('lodash').chain
function profileColor (color, opts) {
color = Color(color)
return {
hex: color.hexString(),
saturation: color.saturation(),
lightness: color.lightness(),
contrast: color.contrast(Color(opts.background)),
background: opts.background
}
}
module.exports = function pickAGoodColor (hexes, opts) {
const defaults = {
background: 'white',
minContrast: 4.5 // https://www.w3.org/TR/WCAG20-TECHS/G18.html
}
opts = Object.assign(defaults, opts)
opts.background = Color(opts.background)
let best = null
const colors = chain(hexes)
.map(hex => profileColor(hex, opts))
.orderBy('saturation', 'desc')
// shoot for the stars
best = colors
.filter(color => color.contrast > opts.minContrast)
.filter(color => opts.background.isLight() ? color.lightness > 0.3 : color.lightness < 0.6)
.map('hex')
.first()
.value()
// shoot for the moon
if (!best) {
best = colors
.filter(color => color.contrast > opts.minContrast)
.map('hex')
.first()
.value()
}
// go for broke
if (!best) {
let hex = colors
.map('hex')
.first()
.value()
best = makeColorAccessible(hex, opts)
}
return best
}