-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
91 lines (91 loc) · 2.68 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const {colorGenerator} = require ('../generator/color');
// Self-Built Palette Generation Package
const Matercolor = require ('matercolors');
const Blinder = require ('color-blind');
const {cymk, rgb, hwb, hsl} = require ('../utility');
module.exports.Cono = function (lang = 'en') {
this.lang = lang;
this.colors = colorGenerator (this.lang);
this.chosenColors = {};
this.currentColor;
this.currentName;
this._nearestCono = require ('nearest-color').from (
colorGenerator (this.lang)
);
// Define Getters
Object.defineProperty (this, 'hex', {
get: function () {
return this.currentColor;
},
});
Object.defineProperty (this, 'rgb', {
get: function () {
return rgb (this.currentColor);
},
});
Object.defineProperty (this, 'hwb', {
get: function () {
return hwb (this.currentColor);
},
});
Object.defineProperty (this, 'hsl', {
get: function () {
return hsl (this.currentColor);
},
});
Object.defineProperty (this, 'cymk', {
get: function () {
return cymk (this.currentColor);
},
});
// Define Color Setter
this.withColor = function (string) {
if (this.colors.hasOwnProperty (string)) {
this.currentColor = this.colors[string];
this.currentName = string;
return this;
} else {
throw new SyntaxError (
'Input Colour String does not follow Naming Convention.'
);
}
};
// Get Palette of Current Color
this.makePalette = function () {
return new Matercolor (this.currentColor);
};
// Based on availability, apply Color Blindness Transformations to Chosen Colors.
this.withColorBlindness = function (blindnessType) {
return this.findNearest (Blinder[blindnessType] (this.currentColor));
};
this.withSubstring = function (substring) {
let selected = Object.keys (this.colors).filter (function (key) {
return key.includes (substring);
});
for (let i = 0, len = selected.length; i < len; i++) {
this.chosenColors[selected[i]] = this.colors[selected[i]];
}
return this;
};
this.makePalettes = function () {
return Object.keys (this.chosenColors)
.map (key => {
let obj = {};
obj[key] = new Matercolor (this.chosenColors[key]);
return obj;
})
.reduce ((r, c) => Object.assign (r, c), {});
};
this.withColorBlindnesses = function (blindnessType) {
for (const [key, value] of Object.entries (this.chosenColors)) {
this.chosenColors[key] = Blinder[blindnessType] (value);
}
return this;
};
this.findNearest = function (hex) {
let cono = this._nearestCono (hex);
this.currentColor = cono.value;
this.currentName = cono.name;
return this;
};
};