-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcarmine_module_alpha.js
362 lines (295 loc) · 13.1 KB
/
carmine_module_alpha.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
//Carmine prototype
//https://github.com/theultraman20/carmine
//By theultraman20
(function(){
var colorFader = document.createElement("style");
colorFader.innerText = "*{ -moz-transition:background-color 0.5s ease-in; -o-transition:background-color 0.5s ease-in; -webkit-transition:background-color 0.5s ease-in; }";
//document.querySelector("head").appendChild(colorFader);
var carmine = {};
carmine.maxThemeCache = 5;//must be at least one
carmine.cacheList = [];
var cacheListString = [];
var themeDataCache = [];
var colorList;
var numColors;
var colorDataSet = {
colorList: [],
groupList: [],
};
//home - resonance ["#e57244", "#6a60db", "#4261de", "#89c2fd", "#e1f4fe"]
//home - sun ["#d61ea2", "#ff5160", "#26211e"]
var colorPropWeights = {
c: 1,
m: 1,
y: 1,
s: 0,
l: 0,
lowSatBgFactor: 2,
count: 5,
size: 1
};
var colorModProps = {
s: 0,
l: 0,
saturationDescaling: 0.1
};
var colorPropScales = {
s: 1,
l: 1
};
var bgNodeList = document.querySelectorAll(":not(html):not(script):not(link):not(title):not(meta)");
var textNodeList = getTextNodes();
var bgNodeLen = bgNodeList.length;
var textNodeLen = textNodeList.length;
var bgData, textData, largestColor;
function getTextNodes(){
var n, a=[], walk=document.createTreeWalker(document.querySelector("body"),NodeFilter.SHOW_TEXT,null,false);
while(n=walk.nextNode()) {
if (n.data !== " ") {
a.push(n.parentElement);
};
};
return a;
};
//big thanks to phrogz for this: https://stackoverflow.com/questions/10730309/find-all-text-nodes-in-html-page
function scoreColor(props, ogColor, listedColor){
var score = 0;
var propsLen = props.length;
var prop;
for (var i = 0; i < propsLen; ++i) {
prop = props[i];
score += Math.abs(ogColor[prop] - listedColor[prop])*colorPropWeights[prop];
};
return score;
};
function getMedian(array){
var arrayLen = array.length;
if (arrayLen%2==0) {//even
return (array[arrayLen/2-1] + array[arrayLen/2])/2;
} else {
return array[(arrayLen-1)/2];
};
};
function removeZeroes(array){
var tmp;
for (var i = array.length; !(tmp=array.pop()); --i);
array.push(tmp);
return array;
};
function getClosestColor(ogColorStr, colorUsageList) {//maybe add colorFormat option later? Also add a preanalyzation option for multiple colorLists
var bgWeights = bgData.bgWeights;
var medianBgWeight = bgData.medianBgWeight;
var ogColorSatScale = bgWeights[bgData.colorList.indexOf(ogColorStr)] / medianBgWeight;//it's "saturation scale" based off of the median saturation
var maxWeight = Math.max(... bgWeights);
var ogColor = tinycolor(ogColorStr);
var ogColorHsl = ogColor.toHsl();
var ogColorCmyk = ogColor.toCmyk();
var colorScores = [];
var color, colorHsl, colorCmyk, closestColor;
console.log(ogColorStr + " sat scale: " + ogColorSatScale + "\n");
for (var i = 0; i < numColors; ++i) {
colorHsl = tinycolor(colorList[i]).toHsl();
colorCmyk = tinycolor(colorList[i]).toCmyk();
colorScores[i] = scoreColor(['c', 'm', 'y'], ogColorCmyk, colorCmyk)/360;
colorScores[i] += scoreColor(['s', 'l'], ogColorHsl, colorHsl);
colorScores[i] += ogColorSatScale / maxWeight / (colorHsl.s/255) * colorPropWeights.lowSatBgFactor;
colorScores[i] += colorUsageList[i] * colorPropWeights.count;//make sure colors aren't overused!
};
closestColor = colorList[colorScores.indexOf(Math.min(...colorScores))];
return closestColor;
};
function getColorDiffScales(ogColor, desiredColor){
var ogHsl, desiredHsl;
ogHsl = tinycolor(ogColor).toHsl();
desiredHsl = tinycolor(desiredColor).toHsl();
return {
s: desiredHsl.s/ogHsl.s,
l: desiredHsl.l/ogHsl.l
}
};
/*color modification properties:
s: max change in saturation (0-1)
l: max change in light (0-1)
*/
function modColor(ogColor, desiredColor){
var ogColorSize = bgData.bgWeights[bgData.colorList.indexOf(ogColor)];
var maxColorSize = Math.max(... bgData.bgWeights);
var ogHsl, desiredHsl, newColor;
//determine a list of new colors to be used
ogHsl = tinycolor(ogColor).toHsl();//background hsl model
desiredHsl = tinycolor(desiredColor).toHsl();//hsl model of colorList color
newColor = desiredHsl;
newColor.s *= Math.min(colorPropScales.s + colorModProps.s - ogColorSize/maxColorSize*colorModProps.saturationDescaling, 1);
newColor.l *= Math.min(colorPropScales.l + colorModProps.l, 1)
return tinycolor(newColor).toHexString();
};
function modBorderColor(ogBgColor, newBgColor, oldBorderColor){
var ogHsl, newHsl, oldBorderHsl, newBorder;
ogBgHsl = tinycolor(ogBgColor).toHsl();
newBgHsl = tinycolor(newBgColor).toHsl();
oldBorderHsl = tinycolor(oldBorderColor).toHsl();
newBorder = oldBorderHsl;
newBorder.h = ogBgHsl.h-oldBorderHsl.h+newBgHsl.h;
newBorder.s = ogBgHsl.s-oldBorderHsl.s+newBgHsl.s;
newBorder.l = ogBgHsl.l-oldBorderHsl.l+newBgHsl.l;
return tinycolor(newBorder).toHexString();
};
function organizeNodeList(nodeList, colorDataHolder, property, skipValue, newValueFoundCallback, iterateCallback){
var organizedList = [];
var valuesList = [];
var listLen = nodeList.length;
//var propertyListLen = propertyList.length;
var node, style, propValue, index;
for (var i = 0; i < nodeList.length; ++i) {
node = nodeList[i];
style = getComputedStyle(node);
propValue = style.getPropertyValue(property);
if (propValue && propValue != skipValue) {
index = valuesList.indexOf(propValue);
if (index == -1) {
index = organizedList.length;
organizedList[index] = [];
valuesList.push(propValue);
newValueFoundCallback(propValue);
};
organizedList[index].push(node);
iterateCallback(node, index);
};
};
colorDataHolder.groupList = organizedList;
colorDataHolder.colorList = valuesList;
};
function getElemData() {//will calculate colordiffscales during elem data collection and slightly speed things up overall. should be used for first theme on page.
bgData = Object.create(colorDataSet);
bgData.bgWeights = [];
bgData.borderColorList = [];
bgData.medianBgWeight = 0;
textData = Object.create(colorDataSet);
textData.bgColorList = [];
textData.textNodes = [];
//add borders soon
organizeNodeList(bgNodeList, bgData, "background-color", "rgba(0, 0, 0, 0)",
function(node, index){
bgData.bgWeights.push(0)
},
function(node, index){
var weight = node.offsetWidth*node.offsetHeight;
bgData.borderColorList.push(getComputedStyle(node).getPropertyValue("border-top-color"));
if (weight==weight) bgData.bgWeights[index] += weight;
});
organizeNodeList(textNodeList, textData, "color", "", function(bg){},
function(node){
var textNodeBg;//maybe later have it just store the color
for (var bgNode = node; (textNodeBg=getComputedStyle(bgNode).getPropertyValue("background-color")) == "rgba(0, 0, 0, 0)"; bgNode = bgNode.parentElement) if (!bgNode.parentElement) break;
textData.bgColorList.push(bgNode);
textData.textNodes.push(node);
});
bgData.medianBgWeight = getMedian(removeZeroes(bgData.bgWeights.sort()));
};
function getThemeData(_colorList){
var themeData;
themeData = Object.create(colorDataSet);
themeData.colorList = _colorList;
themeData.bgColorList = [];
themeData.borderColorList = [];
themeData.textColorList = [];
colorList = _colorList;
numColors = colorList.length;
colorPropScales = {
s: 1,
l: 1
};
var colorUsageList = Array(colorList.length).fill(0);//for getClosestColor();
var textColors = ["black", "white"];//todo
var bgColorList = bgData.colorList;
var bgBorderColorList = bgData.borderColorList;
//var textColorList = textData.colorList;
var textBgColorList = textData.bgColorList;
var bgNumGroups = bgData.groupList.length;
var k = 0;
var closestColor, colorDiffScales, bgGroupLen, bgColor, newColor;
for (var i = 0; i < bgNumGroups; ++i) {
bgColor = bgColorList[i];
closestColor = getClosestColor(bgColor, colorUsageList)
newColor = modColor(bgColor, closestColor);
themeData.bgColorList.push(newColor);
colorUsageList[colorList.indexOf(closestColor)]++;
closestColor = getClosestColor(bgColor, colorUsageList);
colorDiffScales = getColorDiffScales(bgColor, closestColor);
if (colorDiffScales.s < colorPropScales.s) colorPropScales.s = colorDiffScales.s;
if (colorDiffScales.l < colorPropScales.l) colorPropScales.l = colorDiffScales.l;
for (var j = 0, bgGroupLen = bgData.groupList[i].length; j < bgGroupLen; ++j, ++k) {
themeData.borderColorList.push(modBorderColor(bgColor, newColor, bgBorderColorList[k]));
};
};
for (var i = 0; i < textNodeLen; ++i) {
themeData.textColorList.push(
tinycolor.mostReadable(themeData.bgColorList[bgColorList.indexOf(getComputedStyle(textBgColorList[i]).getPropertyValue("background-color"))], textColors).toHexString()
);
};
if (carmine.maxThemeCache == themeDataCache.length) {
themeDataCache.pop();
cacheListString.pop();
carmine.cacheList.pop();
};
themeDataCache.unshift(themeData);
cacheListString.unshift(JSON.stringify(colorList));
carmine.cacheList.unshift(colorList);
//return themeData;
};
function themePage(theme){
var themeData;
if (typeof(theme)=="object") {
themeData = themeDataCache[cacheListString.indexOf(JSON.stringify(theme))];
} else {
themeData = themeDataCache[theme];
};
colorList = themeData.colorList;
numColors = colorList.length;
var bgColorList = themeData.bgColorList;
var borderColorList = themeData.borderColorList;
/*var textGroupList = textData.groupList;
var textNumGroups = textGroupList.length;*/
var textNodeList = textData.textNodes;
var textNodeListLen = textData.textNodes.length;
var textColorList = themeData.textColorList;
var groupList = bgData.groupList;
var bgNumGroups = groupList.length;
var k = 0;
var currentGroup, currentGroupLen, currentElem;
for (var i = 0; i < bgNumGroups; ++i) {
currentGroup = groupList[i];
for (var j = 0, currentGroupLen = currentGroup.length; j < currentGroupLen; ++j, ++k) {
currentElem = currentGroup[j];
currentElem.style.backgroundColor = bgColorList[i];
currentElem.style.borderColor = borderColorList[k];
};
};
//using the organized groups causes a bug since the nodes aren't ordered...
/*for (var i = 0, k = 0; i < textNumGroups; ++i) {
currentGroup = textGroupList[i];
for (var j = 0, currentGroupLen = currentGroup.length; j < currentGroupLen; ++j, ++k) {
currentGroup[j].style.color = textColorList[k];
};
};*/
for (var i = 0; i < textNodeListLen; ++i) {
textNodeList[i].style.color = textColorList[i];
};
};
carmine.getElemData = getElemData;
carmine.getThemeData = getThemeData;
carmine.themePage = themePage;
window.carmine = carmine;
//-------------------------------------------------------------------------------
/*if (!colorData){
colorData = getColorData(colorList, colorPropWeights, colorModProps.colorChangeSync);
document.querySelector("head").appendChild(colorFader);
}*/
})();
carmine.getElemData();
carmine.getThemeData(["#e57244", "#6a60db", "#4261de", "#89c2fd", "#e1f4fe"]);
//carmine.getThemeData(["#d61ea2", "#ff5160", "#26211e"]);
//carmine.getThemeData(["rgb(97, 38, 34)", "rgb(191, 169, 134)", "rgb(20, 147, 70)"]);
//test theme
//carmine.getThemeData(["#45207d", "#c21f79", "#252438"]);
carmine.themePage(0);