forked from jxnblk/colorable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (65 loc) · 1.74 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
var _ = require('lodash');
var Color = require('color');
var minimums = {
aa: 4.5,
aaLarge: 3,
aaa: 7,
aaaLarge: 4.5
};
module.exports = function(colors, options) {
var arr = [];
var results = [];
var combinations = [];
var options = options || {};
_.defaults(options, {
threshold: 0,
compact: false,
uniq: true
});
if (!Array.isArray(colors)) {
if (typeof colors === 'object') {
_.forIn(colors, function(val, key) {
var color = Color(val);
color.name = key;
arr.push(color);
});
if (options.uniq) {
arr = _.uniq(arr);
}
} else {
console.error('Must provide an array or object');
return false;
}
} else {
if (options.uniq) {
colors = _.uniq(colors);
}
colors.forEach(function(color) {
arr.push(Color(color));
});
}
arr.forEach(function(color) {
var result = options.compact ? {} : _.clone(color);
result.hex = color.hexString();
if (color.name) { result.name = color.name; }
result.combinations = [];
arr.forEach(function(bg) {
if (color === bg) { return false; }
var combination = options.compact ? {} : _.clone(bg);
combination.hex = bg.hexString();
if (bg.name) { combination.name = bg.name; }
combination.contrast = color.contrast(bg);
combination.accessibility = {
aa: combination.contrast >= minimums.aa,
aaLarge: combination.contrast >= minimums.aaLarge,
aaa: combination.contrast >= minimums.aaa,
aaaLarge: combination.contrast >= minimums.aaaLarge,
};
if (combination.contrast > options.threshold) {
result.combinations.push(combination);
}
});
results.push(result);
});
return results;
};