This repository has been archived by the owner on Jan 10, 2024. It is now read-only.
forked from dagthomas/hexSorter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hexSorter.js
171 lines (141 loc) · 5.23 KB
/
hexSorter.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
module.exports = {
hexValueSanitize: function(color) {
return color.replace(/^#?([a-f\d])([a-f\d])([a-f\d])$/i
,(m, r, g, b) => r + r + g + g + b + b).replace('#', '');
},
hexToDec: function(hex) {
return parseInt((hex + '').replace(/[^a-f0-9]/gi, ''), 16);
},
decToHex: function(number) {
return number < 0 ? 0xFFFFFFFF + number + 1 : parseInt(number, 10).toString(16);
},
hexToRgb: function(hex) {
hex = this.hexValueSanitize(hex);
return hex.length == 3 ? [this.hexToDec(hex[0] + hex[0]), this.hexToDec(hex[1] + hex[1]), this.hexToDec(hex[2] + hex[2])] : [this.hexToDec(hex[0] + hex[1]), this.hexToDec(hex[2] + hex[3]), this.hexToDec(hex[4] + hex[5])];
},
hexBrightness: function(hex, type) {
let conversion;
if (type == 'BT601') {
conversion = [0.299, 0.587, 0.114]; //BT601
} else if (type == 'BT709') {
conversion = [0.2126, 0.7152, 0.0722]; //BT709
} else if (type == 'BT2020') {
conversion = [0.2627, 0.6780, 0.0593]; //BT2020
} else {
conversion = [0.299, 0.587, 0.114]; //BT601
};
hex = this.hexValueSanitize(hex);
return (this.hexToDec(hex[0] + hex[1]) * conversion[0] + this.hexToDec(hex[2] + hex[3]) * conversion[1] + this.hexToDec(hex[4] + hex[5]) * conversion[2]);
},
rgbToHsv: function(color) {
let r = color[0] / 255;
let g = color[1] / 255;
let b = color[2] / 255;
let h,s,min,max,del,dR,dG,dB,hsl;
hsl = [];
min = Math.min(r, g, b);
max = Math.max(r, g, b);
del = max - min;
if (del == 0) {
h = 0;
s = 0;
} else {
s = del / max;
dR = (((max - r) / 6) + (del / 2)) / del;
dG = (((max - g) / 6) + (del / 2)) / del;
dB = (((max - b) / 6) + (del / 2)) / del;
if (r == max) {
h = dB - dG;
} else if (g == max) {
h = (1 / 3) + dR - dB;
} else if (b == max) {
h = (2 / 3) + dG - dR;
};
if (h < 0) {
h++;
};
if (h > 1) {
h--;
};
};
hsl['h'] = h;
hsl['s'] = s;
hsl['v'] = 0.9;
return hsl;
},
hexToHsv: function(hex) {
let rgb, hsv;
hex = this.hexValueSanitize(hex);
rgb = this.hexToRgb(hex);
hsv = this.rgbToHsv(rgb);
return hsv;
},
mostBrightColor: function(colors, type) {
let mostBright = false;
let hex;
colors.forEach((color) => {
hex = this.hexValueSanitize(color);
brightness = this.hexBrightness(hex, type);
if (!mostBright || this.hexBrightness(hex, type) > this.hexBrightness(mostBright, type)) {
mostBright = hex;
};
});
return `#${mostBright}`;
},
mostSaturatedColor: function(colors) {
let mostSaturated = false;
let hex, hsv, saturation, oldHsv;
colors.forEach((color) => {
hex = this.hexValueSanitize(color);
hsv = this.hexToHsv(hex);
saturation = hsv['s'];
if (mostSaturated) {
oldHsv = this.hexToHsv(mostSaturated);
};
if (!mostSaturated || saturation > oldHsv['s']) {
mostSaturated = hex;
};
});
return `#${mostSaturated}`
},
colorMixer: function(hex1, hex2, percent) {
hex1 = this.hexValueSanitize(hex1);
hex2 = this.hexValueSanitize(hex2);
if (hex1.length == 3) {
hex1 = hex1.repeat(hex1[0], 2) + hex1.repeat(hex1[1], 2) + hex1.repeat(hex1[2], 2);
};
if (hex2.length == 3) {
hex2 = hex2.repeat(hex2[0], 2) + hex2.repeat(hex2[1], 2) + hex2.repeat(hex2[2], 2);
};
let red_hex = this.decToHex((percent * this.hexToDec(hex1[0] + hex1[1]) + (100 - percent) * this.hexToDec(hex2[0] + hex2[1])) / 100).padStart(2, '0');
let green_hex = this.decToHex((percent * this.hexToDec(hex1[2] + hex1[3]) + (100 - percent) * this.hexToDec(hex2[2] + hex2[3])) / 100).padStart(2, '0');
let blue_hex =this.decToHex((percent * this.hexToDec(hex1[4] + hex1[5]) + (100 - percent) * this.hexToDec(hex2[4] + hex2[5])) / 100).padStart(2, '0');
return `#${red_hex+green_hex+blue_hex}`;
},
sortColors: function(colors, type) {
const input = colors.slice(0);
const output = [];
while (input.length > 0) {
const color = this[type](input)
let index = input.indexOf(color);
if (index > -1) {
input.splice(index, 1);
}
output.push(color)
}
return output
},
mixSortColors: function(colors, type, mixcolor, percentage) {
const input = colors.slice(0)
const output = []
while (input.length > 0) {
const color = this[type](input)
let index = input.indexOf(color);
if (index > -1) {
input.splice(index, 1);
}
output.push(this.colorMixer(color, mixcolor, percentage))
}
return output
}
}