forked from instructure/color-slicer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
74 lines (62 loc) · 1.88 KB
/
Copy pathindex.js
File metadata and controls
74 lines (62 loc) · 1.88 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// This uses fairSlicer to provide arbitrarily fine
// divisions of the hue space. It uses the lab color
// space to maintain legibility and distinctiveness.
// x here is a hue angle in degrees
var fairSlicer = require('./lib/fair-slicer');
var converter = require("color-convert");
module.exports = {
hueToLch: function(options, h) {
h = Math.round(h);
var l, c;
if (options.l) {
l = options.l;
} else if (options.bright) {
l = 74;
} else if (options.unsafe) {
l = 60;
} else {
l = 49;
}
if (options.c) {
c = options.c;
} else {
c = 3 + l / 2;
// vary chroma to roughly match boundary of darkest RGB-expressible colors
var delta = 5 + l/4;
var most_constrained_hue = 210;
var hr = (h - most_constrained_hue) / 360 * 2 * Math.PI;
c += Math.floor(delta - delta * Math.cos(hr));
// constrain chroma by lightest RGB-expressible colors
var overpower = Math.max(Math.floor(160 - (l * 8 / 5)), 0);
c = Math.min(c, overpower);
}
return [l, c, h]
},
lchToRgb: function(lch) {
return converter.lch2rgb.apply(converter, lch);
},
lchToCss: function(lch) {
return "rgb("+this.lchToRgb(lch).join(',')+")";
},
getLchColors: function(limit, startX, options) {
if (startX === undefined) {
startX = 330;
}
if (!options) {
options = {};
}
var hueToLch = function(h) {
return this.hueToLch(options, h);
}.bind(this);
var slices = fairSlicer(limit, 0, 360, startX);
return slices.map(hueToLch);
},
getRgbColors: function(limit, startX, options) {
var lchColors = this.getLchColors(limit, startX, options);
return lchColors.map(this.lchToRgb);
},
getColors: function(limit, startX, options) {
var lchColors = this.getLchColors(limit, startX, options);
return lchColors.map(this.lchToCss.bind(this));
}
};