-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
242 lines (161 loc) · 6.94 KB
/
script.js
File metadata and controls
242 lines (161 loc) · 6.94 KB
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
import {Color} from "./classes/color.js";
import {generatePalette} from "./classes/generator.js";
import { adaptColor,downloadFile} from "./classes/utils.js";
import { events } from "./classes/events.js";
window.downloadFile = downloadFile;
window.numColumns = 5;
window.generatedType = "monochromatic";
window.lastgeneratedType = "";
window.currentColor;
class mainapp{
constructor(){
this.palette = null;
this.exportMode = "pdf";
this.eventsmanager = new events(this);
this.genBtn = document.getElementById('gen-btn');
this.generateNewPalette = this.generateNewPalette.bind(this);
this.genBtn.addEventListener('click', this.generateNewPalette);
this.plusBtn = document.getElementById('plus-btn');
this.plusBtn.addEventListener('click', this.eventsmanager.plusButton);
this.minusBtn = document.getElementById('minus-btn');
this.minusBtn.addEventListener('click', this.eventsmanager.minusButton);
this.switchModeBtn = document.getElementById('switch-mode-btn');
this.switchModeBtn.onclick = this.eventsmanager.updateSwitchModeIcon;
this.colorPicker = document.getElementById('base-color-picker');
this.colorPicker.addEventListener('input', this.eventsmanager.picker );
this.pdfButton = document.getElementById('pdf-download-btn');
this.printButton = document.getElementById('print-btn');
this.pdfButton.addEventListener('click',this.eventsmanager.pdfButton);
this.printButton.addEventListener('click',this.eventsmanager.printButton);
this.jsonBtn = document.getElementById('json-download-btn');
this.jsonBtn.addEventListener('click',this.eventsmanager.jsonExport);
this.jsonCompatibleBtn = document.getElementById('json-compatible-download-btn');
this.jsonCompatibleBtn.addEventListener('click',this.eventsmanager.jsonImportFriendlyExport);
this.csvBtn = document.getElementById('csv-download-btn');
this.csvBtn.addEventListener('click',this.eventsmanager.csvExport);
this.files_pop_up = document.getElementById('file-export-select');
this.files_pop_up_btn = document.getElementById('download-btn');
this.files_pop_up_btn.addEventListener('click',this.eventsmanager.trigger_download_menu);
this.file_picker = document.getElementById('open-file-input');
this.file_picker_btn = document.getElementById('open-file-btn');
this.file_picker_btn.addEventListener('click',this.eventsmanager.jsonImportHandle);
this.file_picker.addEventListener('input',this.eventsmanager.jsonImport);
this.paletteOutput = document.querySelector('.palette-output');
this.generateNewPalette();
}
randomHsvColor() {
return [Math.floor(Math.random() * 360),Math.floor(Math.random() * 50 + 50),Math.floor(Math.random() * 50 + 50)]
}
renderColumns() {
this.paletteOutput.innerHTML = '';
let type = window.generatedType;
if( !this.paletteOutput.classList.contains("complementary") && type == "complementary" ) this.paletteOutput.className = "palette-output complementary";
if( !this.paletteOutput.classList.contains("monochromatic") && type == "monochromatic" ) this.paletteOutput.className = "palette-output monochromatic";
if (type === "complementary") {
// Create two rows, each with numColumns columns
for (let row = 0; row < 2; row++) {
const rowDiv = document.createElement('div');
rowDiv.className = 'palette-row';
for (let i = 0; i < numColumns; i++) {
const div = document.createElement('div');
div.className = 'palette-column';
rowDiv.appendChild(div);
}
this.paletteOutput.appendChild(rowDiv);
}
} else {
// Monochromatic: single row
for (let i = 0; i < numColumns; i++) {
const div = document.createElement('div');
div.className = 'palette-column';
this.paletteOutput.appendChild(div);
}
}
}
applyPalette(baseHex) {
if( window.lastgenerated != window.generatedType ){
this.renderColumns();
window.lastgenerated = window.generatedType;
}
this.palette = generatePalette(baseHex,window.generatedType,numColumns);
this.patchPalette();
}
generateNewPalette() {
let newcolor = this.randomHsvColor();
this.currentColor = new Color(newcolor,"hsv");
this.colorPicker.value = this.currentColor.hex;
this.applyPalette(this.currentColor.hex);
}
//Rewriting the palette applier so all different functions applying palettes will use only one instead of clones
patchPalette() {
let columns;
if (window.generatedType === "complementary") {
// Select all palette-column in both rows
columns = document.querySelectorAll('.palette-row .palette-column');
} else {
columns = document.querySelectorAll('.palette-column');
}
columns.forEach((col, i) => {
let currentColor = this.palette[i];
if (currentColor) {
col.style.backgroundColor = currentColor.hex;
col.innerHTML = `
<div class="color-info">
<div class="hex">${currentColor.hex}</div>
<div class="name">${currentColor.name || ''}</div>
<div class="rgb">${currentColor.rgb}</div>
</div>
`;
col.style.color = (adaptColor(currentColor.hsv));
}
});
}
handleImport() {
if(window.generatedType == "complementary"){
window.numColumns = this.palette.length / 2;
this.currentColor = this.palette[Math.floor(window.numColumns / 2)];
this.switchModeBtn.classList.add('active');
this.switchModeBtn.title = 'Mode: Complementary';
}
else{
this.currentColor = this.palette[Math.ceil(window.numColumns / 2)];
this.switchModeBtn.classList.remove('active');
this.switchModeBtn.title = 'Mode: Monochromatic';
}
for (let index = 0; index < this.palette.length; index++) {
this.palette[index] = new Color(this.palette[index].Hex,"hex");
}
this.colorPicker.value = this.currentColor.Hex;
this.renderColumns();
this.patchPalette();
// window.numColumns = this.palette.length;
// this.currentColor = this.palette[Math.ceil(window.numColumns / 2)];
// this.colorPicker.value = this.currentColor.hex;
// this.renderColumns();
// this.patchPalette();
// let columns = document.querySelectorAll('.palette-column');
// columns.forEach((col, i) => {
// let currentColor = this.palette[i];
// if (currentColor) {
// col.style.backgroundColor = currentColor.hex;
// col.innerHTML = `
// <div class="color-info">
// <div class="hex">${currentColor.hex}</div>
// <div class="name">${currentColor.name || ''}</div>
// <div class="rgb">${currentColor.rgb}</div>
// </div>
// `;
// col.style.color = (adaptColor(currentColor.hsv));
// }
// });
}
}
function main() {
try {
window.app = new mainapp();
window.Color = Color;
} catch (error) {
console.error('Erreur:', error);
}
}
main();