-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonet.js
346 lines (278 loc) · 15.2 KB
/
monet.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
var palette;
// Function to generate a Material Design color palette from an image URL
function generateMaterialDesignPalette(imageURL, callback) {
// Create an image element to load the image
const img = new Image();
img.crossOrigin = "Anonymous"; // Enable cross-origin access to the image
// Set up an event listener for when the image is loaded
img.onload = function () {
// Create a Vibrant.js object to extract colors from the image
const vibrant = new Vibrant(img);
const swatches = vibrant.swatches();
// Check if swatches were successfully generated
if (swatches) {
// Extract Material Design color palette
palette = {
accent: swatches.Vibrant.getHex(),
primaryDark: swatches.DarkVibrant.getHex(),
primaryLight: swatches.LightVibrant.getHex(),
primary: swatches.Muted.getHex()
};
// Execute the callback function with the generated palette
callback(null, palette);
}
else {
// Error handling if swatches couldn't be generated
callback("Failed to generate swatches", null);
}
};
// Set the image source to the provided URL
img.src = imageURL;
}
// Function to generate an RGBA value with a specified alpha
function generateRGBA(hex, alpha) {
// Remove the "#" symbol if present
hex = hex.replace(/^#/, '');
// Parse the hex color to RGB components
const bigint = parseInt(hex, 16);
const red = (bigint >> 16) & 255;
const green = (bigint >> 8) & 255;
const blue = bigint & 255;
// Create the RGBA string
return `rgba(${red}, ${green}, ${blue}, ${alpha})`;
}
function uploadFile() {
let input = document.createElement('input');
input.type = 'file';
input.accept = 'image/*';
input.onchange = () => {
let file = input.files[0]; // Get the first selected file
if (file) {
let reader = new FileReader();
reader.onload = function (e) {
let fileURL = e.target.result;
document.getElementById("img").src = fileURL;
generateMaterialDesignPalette(fileURL, (error, palette) => {
if (error) {
console.error(error);
}
else {
console.log("Material Design Palette:", palette);
document.getElementById("accent").style.backgroundColor = palette.accent;
document.getElementById("primary").style.backgroundColor = palette.primary;
document.getElementById("primaryDark").style.backgroundColor = palette.primaryDark;
document.getElementById("primaryLight").style.backgroundColor = palette.primaryLight;
document.getElementById("accent").innerText = palette.accent;
document.getElementById("primary").innerText = palette.primary;
document.getElementById("primaryDark").innerText = palette.primaryDark;
document.getElementById("primaryLight").innerText = palette.primaryLight;
if (calculateContrastRatio(palette.accent) < 4.5) {
document.getElementById("accent").style.color = "black";
}
if (calculateContrastRatio(palette.primary) < 4.5) {
document.getElementById("primary").style.color = "black";
}
if (calculateContrastRatio(palette.primaryDark) < 4.5) {
document.getElementById("primaryDark").style.color = "black";
}
if (calculateContrastRatio(palette.primaryLight) < 4.5) {
document.getElementById("primaryLight").style.color = "black";
}
document.getElementById("accent-trans").style.backgroundColor = generateRGBA(palette.accent, 0.25);
document.getElementById("primary-trans").style.backgroundColor = generateRGBA(palette.primary, 0.25);
document.getElementById("primaryDark-trans").style.backgroundColor = generateRGBA(palette.primaryDark, 0.25);
document.getElementById("primaryLight-trans").style.backgroundColor = generateRGBA(palette.primaryLight, 0.25);
document.getElementById("accent-trans").innerText = rgbaToHex(generateRGBA(palette.accent, 0.25));
document.getElementById("primary-trans").innerText = rgbaToHex(generateRGBA(palette.primary, 0.25));
document.getElementById("primaryDark-trans").innerText = rgbaToHex(generateRGBA(palette.primaryDark, 0.25));
document.getElementById("primaryLight-trans").innerText = rgbaToHex(generateRGBA(palette.primaryLight, 0.25));
document.getElementById("accent-trans-inter").style.backgroundColor = generateRGBA(palette.accent, 0.5);
document.getElementById("primary-trans-inter").style.backgroundColor = generateRGBA(palette.primary, 0.5);
document.getElementById("primaryDark-trans-inter").style.backgroundColor = generateRGBA(palette.primaryDark, 0.5);
document.getElementById("primaryLight-trans-inter").style.backgroundColor = generateRGBA(palette.primaryLight, 0.5);
document.getElementById("accent-trans-inter").innerText = rgbaToHex(generateRGBA(palette.accent, 0.5));
document.getElementById("primary-trans-inter").innerText = rgbaToHex(generateRGBA(palette.primary, 0.5));
document.getElementById("primaryDark-trans-inter").innerText = rgbaToHex(generateRGBA(palette.primaryDark, 0.5));
document.getElementById("primaryLight-trans-inter").innerText = rgbaToHex(generateRGBA(palette.primaryLight, 0.5));
document.getElementById("navbar").style.backgroundColor = generateRGBA(palette.accent, 0.25);
document.getElementById("uploadLinkButton").style.backgroundColor = generateRGBA(palette.accent, 0.25);
document.getElementById("uploadFileButton").style.backgroundColor = generateRGBA(palette.accent, 0.25);
document.getElementById("export").style.backgroundColor = generateRGBA(palette.accent, 0.25);
document.getElementById("uploadLinkButton").addEventListener("mouseover", () => {
document.getElementById("uploadLinkButton").style.backgroundColor = generateRGBA(palette.accent, 0.5);
});
document.getElementById("uploadLinkButton").addEventListener("mouseout", () => {
document.getElementById("uploadLinkButton").style.backgroundColor = generateRGBA(palette.accent, 0.25);
});
document.getElementById("uploadFileButton").addEventListener("mouseover", () => {
document.getElementById("uploadFileButton").style.backgroundColor = generateRGBA(palette.accent, 0.5);
});
document.getElementById("uploadFileButton").addEventListener("mouseout", () => {
document.getElementById("uploadFileButton").style.backgroundColor = generateRGBA(palette.accent, 0.25);
});
document.getElementById("export").addEventListener("mouseover", () => {
document.getElementById("export").style.backgroundColor = generateRGBA(palette.accent, 0.5);
});
document.getElementById("export").addEventListener("mouseout", () => {
document.getElementById("export").style.backgroundColor = generateRGBA(palette.accent, 0.25);
});
}
});
};
reader.readAsDataURL(file); // Read the file as a data URL
}
};
input.click();
}
function openLink() {
document.getElementById("export").style.display = "flex";
var imageURL = prompt("URL:");
generateMaterialDesignPalette(imageURL, (error, palette) => {
if (error) {
console.error(error);
}
else {
console.log("Material Design Palette:", palette);
document.getElementById("accent").style.backgroundColor = palette.accent;
document.getElementById("primary").style.backgroundColor = palette.primary;
document.getElementById("primaryDark").style.backgroundColor = palette.primaryDark;
document.getElementById("primaryLight").style.backgroundColor = palette.primaryLight;
document.getElementById("accent").innerText = palette.accent;
document.getElementById("primary").innerText = palette.primary;
document.getElementById("primaryDark").innerText = palette.primaryDark;
document.getElementById("primaryLight").innerText = palette.primaryLight;
if (calculateContrastRatio(palette.accent) < 4.5) {
document.getElementById("accent").style.color = "black";
}
if (calculateContrastRatio(palette.primary) < 4.5) {
document.getElementById("primary").style.color = "black";
}
if (calculateContrastRatio(palette.primaryDark) < 4.5) {
document.getElementById("primaryDark").style.color = "black";
}
if (calculateContrastRatio(palette.primaryLight) < 4.5) {
document.getElementById("primaryLight").style.color = "black";
}
document.getElementById("accent-trans").style.backgroundColor = generateRGBA(palette.accent, 0.25);
document.getElementById("primary-trans").style.backgroundColor = generateRGBA(palette.primary, 0.25);
document.getElementById("primaryDark-trans").style.backgroundColor = generateRGBA(palette.primaryDark, 0.25);
document.getElementById("primaryLight-trans").style.backgroundColor = generateRGBA(palette.primaryLight, 0.25);
document.getElementById("accent-trans").innerText = rgbaToHex(generateRGBA(palette.accent, 0.25));
document.getElementById("primary-trans").innerText = rgbaToHex(generateRGBA(palette.primary, 0.25));
document.getElementById("primaryDark-trans").innerText = rgbaToHex(generateRGBA(palette.primaryDark, 0.25));
document.getElementById("primaryLight-trans").innerText = rgbaToHex(generateRGBA(palette.primaryLight, 0.25));
document.getElementById("accent-trans-inter").style.backgroundColor = generateRGBA(palette.accent, 0.5);
document.getElementById("primary-trans-inter").style.backgroundColor = generateRGBA(palette.primary, 0.5);
document.getElementById("primaryDark-trans-inter").style.backgroundColor = generateRGBA(palette.primaryDark, 0.5);
document.getElementById("primaryLight-trans-inter").style.backgroundColor = generateRGBA(palette.primaryLight, 0.5);
document.getElementById("accent-trans-inter").innerText = rgbaToHex(generateRGBA(palette.accent, 0.5));
document.getElementById("primary-trans-inter").innerText = rgbaToHex(generateRGBA(palette.primary, 0.5));
document.getElementById("primaryDark-trans-inter").innerText = rgbaToHex(generateRGBA(palette.primaryDark, 0.5));
document.getElementById("primaryLight-trans-inter").innerText = rgbaToHex(generateRGBA(palette.primaryLight, 0.5));
document.getElementById("navbar").style.backgroundColor = generateRGBA(palette.accent, 0.25);
document.getElementById("uploadLinkButton").style.backgroundColor = generateRGBA(palette.accent, 0.25);
document.getElementById("uploadFileButton").style.backgroundColor = generateRGBA(palette.accent, 0.25);
document.getElementById("export").style.backgroundColor = generateRGBA(palette.accent, 0.25);
document.getElementById("uploadLinkButton").addEventListener("mouseover", () => {
document.getElementById("uploadLinkButton").style.backgroundColor = generateRGBA(palette.accent, 0.5);
});
document.getElementById("uploadLinkButton").addEventListener("mouseout", () => {
document.getElementById("uploadLinkButton").style.backgroundColor = generateRGBA(palette.accent, 0.25);
});
document.getElementById("uploadFileButton").addEventListener("mouseover", () => {
document.getElementById("uploadFileButton").style.backgroundColor = generateRGBA(palette.accent, 0.5);
});
document.getElementById("uploadFileButton").addEventListener("mouseout", () => {
document.getElementById("uploadFileButton").style.backgroundColor = generateRGBA(palette.accent, 0.25);
});
document.getElementById("export").addEventListener("mouseover", () => {
document.getElementById("export").style.backgroundColor = generateRGBA(palette.accent, 0.5);
});
document.getElementById("export").addEventListener("mouseout", () => {
document.getElementById("export").style.backgroundColor = generateRGBA(palette.accent, 0.25);
});
}
});
document.getElementById("img").src = imageURL;
}
function hexToRgb(hex) {
// Convert a hex color to RGB values
const r = parseInt(hex.slice(1, 3), 16);
const g = parseInt(hex.slice(3, 5), 16);
const b = parseInt(hex.slice(5, 7), 16);
return [r, g, b];
}
function getRelativeLuminance(rgb) {
// Calculate relative luminance
const [r, g, b] = rgb.map((c) => {
const sRGB = c / 255;
return sRGB <= 0.03928 ? sRGB / 12.92 : Math.pow((sRGB + 0.055) / 1.055, 2.4);
});
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
}
function calculateContrastRatio(background) {
var foreground = "#ffffff";
if (foreground && background) {
// Calculate contrast ratio
const fgRgb = hexToRgb(foreground);
const bgRgb = hexToRgb(background);
const fgLuminance = getRelativeLuminance(fgRgb);
const bgLuminance = getRelativeLuminance(bgRgb);
var contrastRatio = (Math.max(fgLuminance, bgLuminance) + 0.05) / (Math.min(fgLuminance, bgLuminance) + 0.05);
return contrastRatio;
}
}
function rgbaToHex(rgba) {
// Check if the input is a valid RGBA string
const rgbaRegex = /^rgba\((\d+),\s*(\d+),\s*(\d+),\s*([\d.]+)\)$/;
if (!rgbaRegex.test(rgba)) {
return null; // Invalid input
}
// Extract RGBA values
const [, r, g, b, a] = rgba.match(rgbaRegex);
// Convert the values to hexadecimal and ensure they have two digits
const toHex = (value) => {
const hex = parseInt(value, 10).toString(16);
return hex.length === 1 ? '0' + hex : hex;
};
const hexR = toHex(r);
const hexG = toHex(g);
const hexB = toHex(b);
// Convert the alpha value to a hexadecimal value (between 00 and FF)
const alpha = Math.round(parseFloat(a) * 255);
const hexA = toHex(alpha);
// Construct the hexadecimal color string
const hexColor = `#${hexR}${hexG}${hexB}${hexA}`;
return hexColor;
}
function exportCSS(accentColour) {
fetch("https://linfindel.github.io/nadircss/nadir.css")
.then(response => response.text())
.then(data => {
var nadirCSS = data;
var rgbaDefault = generateRGBA(palette.accent, 0.25);
var rgbaHover = generateRGBA(palette.accent, 0.5);
while (nadirCSS.includes("rgba(0, 89, 255, 0.25)")) {
nadirCSS = nadirCSS.replace("rgba(0, 89, 255, 0.25)", rgbaDefault);
}
while (nadirCSS.includes("rgba(0, 89, 255, 0.5)")) {
nadirCSS = nadirCSS.replace("rgba(0, 89, 255, 0.5)", rgbaHover);
}
// Copy the URI to the clipboard
navigator.clipboard.writeText(nadirCSS)
.then(() => {
document.getElementById("export-icon").innerText = "done";
document.getElementById("export-text").innerText = "CSS copied to clipboard";
setTimeout(() => {
document.getElementById("export-icon").innerText = "export_notes";
document.getElementById("export-text").innerText = "Export as custom NadirCSS";
}, 3000);
})
.catch(error => {
console.error('Error copying to clipboard:', error);
});
})
.catch(error => {
console.error('Error fetching CSS:', error);
});
}