-
Notifications
You must be signed in to change notification settings - Fork 8
/
demo.html
490 lines (459 loc) Β· 14.8 KB
/
demo.html
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="anypalette-logo.png" type="image/png">
<link rel="icon" href="anypalette-logo.svg" type="image/svg+xml">
<title>AnyPalette.js Demo</title>
<style>
body {
font-family: Helvetica, Arial, sans-serif;
padding-bottom: 150px;
}
.color {
display: inline-block;
width: 20px;
height: 20px;
margin: 2px;
outline: 1px solid black;
border: 1px solid white;
}
@media (max-width: 700px) {
.palette-row {
line-height: 0;
}
.color {
width: 15px;
height: 15px;
margin: 0;
outline: 0;
border: 0;
}
}
h2 > *:not(.header-text) {
font-size: 0.5em;
padding: 10px;
}
a {
color: blue;
cursor: pointer;
}
a.header-text {
text-decoration: none;
color: black;
}
a:hover {
text-decoration: underline;
}
.lesser-importance {
color: gray !important;
}
.failed {
background: #f0b0a0;
opacity: 0.9;
box-shadow: 0px 0px 20px rgba(255, 255, 255, 1) inset, 0px 0px 250px rgba(255, 0, 0, 0.5) inset, 0px 0px 50px rgba(255, 0, 0, 0.5) inset;
}
.failed h3,
.errors {
color: #a00;
font-family: monospace;
margin: 15px;
}
.errors {
padding-bottom: 25px;
}
.status-error {
color: red;
}
body > h2 {
font-size: 1.7em;
padding-top: 1em;
border-top: 0.1em solid black;
margin-top: 1em;
box-shadow: inset 0px 5px 5px rgb(231, 255, 55);
border-radius: 50px;
text-align: center;
}
h1 {
text-align: center;
}
/* Drag and drop this page onto this page to reveal this secret hidden palette! */
/* [
["#000000","#000000","#000000","#aaab84"],
["#a80e08","#000000","#000000","#000000"],
["#000000","#000000","#000000","#f0ec90"],
["#000000","#184553","#000000","#000000"]
] */
</style>
</head>
<body>
<h1><a href="https://github.com/1j01/anypalette.js"><img src="anypalette-logo.png" style="height:1.3em; vertical-align: middle;" alt="">AnyPalette.js</a>: Let's load some palettes</h1>
<script src="lib/jquery-3.5.1.js"></script>
<script src="build/anypalette-dev.js"></script>
<script>
if (!window.AnyPalette) {
document.write(`<script src="build/anypalette-0.7.0.js"><\/script>`);
}
</script>
<script>
function PaletteView(palette_name, url){
var $section = $("<div>");
var $header = $("<h2>").appendTo($section);
var $output = $("<div>").appendTo($section);
var $loaded_as = $("<span>").appendTo($header);
var $toggle_dupes = $("<button>").appendTo($header).hide();
var $save = $("<button>").appendTo($header);
var $header_inner;
if(url){
$header_inner = $("<a>")
.attr("href", url)
.appendTo($header);
}else{
$header_inner = $("<span>");
}
$header_inner
.prependTo($header)
.addClass("header-text")
.text(palette_name.replace(/.*\//, ""));
this.appendTo = function(e){
$section.appendTo(e);
};
this.status = function(text){
$output.text(text);
};
this.error = function(text){
$output.text(text).addClass("status-error");
};
this.update = function(error, palette, formatUsed, matchedFileExtension){
if(error){
$section.addClass("failed");
$output.append("<h3>Failed to load:</h3>");
if (error.errors) {
var $ul = $("<ul class='errors'>").appendTo($output);
for(var i=0; i<error.errors.length; i++){
$ul.append($("<li class='error'>").text(error.errors[i].message));
}
} else {
$output.append($("<p class='error'>").text(error));
}
return;
}
$loaded_as.html(
"<span class='lesser-importance'>loaded as </span>"
+"<span>"+formatUsed.name+"</span>"
+"<span class='lesser-importance'> ("+(formatUsed.fileExtensionsPretty)+")"+(matchedFileExtension?"":" - file extension not matched")+"</span>"
);
var show_colors = function(palette){
$output.empty();
var $row = $("<div class='palette-row'>").appendTo($output);
var col = 0;
$.each(palette, function(i, color){
$("<div class='color'>")
.appendTo($row)
.css("background-color", color)
.attr("title", color.name ? (color.name + "\n" + color) : color)
col += 1;
if(col >= palette.numberOfColumns){
col = 0;
$row = $("<div class='palette-row'>").appendTo($output);
}
});
};
var withoutDuplicates = AnyPalette.uniqueColors(palette);
var n_dupes = palette.length - withoutDuplicates.length;
var s = (n_dupes > 1) ? "s" : "";
var showing_dupes = true;
const update_display = ()=> {
show_colors(showing_dupes ? palette : withoutDuplicates);
$toggle_dupes.text(showing_dupes ? "Hide Duplicate"+s : "Show Duplicate"+s);
};
update_display();
$toggle_dupes.on("click", function(e){
showing_dupes = !showing_dupes;
update_display();
});
$toggle_dupes.on("mousedown", function(e){
e.preventDefault();
});
if(n_dupes > 0){
$toggle_dupes.show();
if(palette.geometrySpecifiedByFile){
$toggle_dupes.click();
}
}
$save.text("Save As...");
$save.on("click", () => {
if ($save.parent().find(".fan").length) {
$save.parent().find(".fan").remove();
return;
}
const formats = Object.values(AnyPalette.formats).filter((format)=> format.write);
const $fan = $("<div class='fan'>");
$save.after($fan);
formats.forEach((format)=> {
const $option = $("<button class='fan-option'>");
$option.text(`${format.name} (.${format.fileExtensions[0]})`);
$option.on("click", () => {
var file_content = AnyPalette.writePalette(palette, format);
var file = new File([file_content], `${palette_name.replace(/.*\//, "")}.${format.fileExtensions[0]}`);
var url = URL.createObjectURL(file);
var a = document.createElement("a");
a.href = url;
a.download = file.name;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
});
$fan.append($option);
});
});
};
}
$.each([{
name: "",
description: "",
dir: "Other/",
fnames: [
"CortexCommand.pal",
"test.act",
"mini.spl",
"Pumpkin.cs",
"s2green.pal",
"spacewar.pal",
//"test32.gif",
"drf.color.styl.txt",
"material-design.sketchpalette",
"sketch-default.sketchpalette",
"ios.sketchpalette",
"Default_Paint.NET_Palette.txt",
"NES.aco",
// "Resene-TCS-Diwali.acb",
"emoji-average-colors.gpl",
"../sk1_palette_collection_1.0/SOC/openSUSE_colors.soc"
]
},{
name: "Windows Themes",
description: "Windows theme and themepack files are supported.",
dir: "Windows_themes/",
fnames: [
"Peggys Pastels.themepack",
"Solarized Dark.themepack",
"Windows Official/Inside your Computer (high color).theme",
"Windows Official/The 60's USA (256 color).theme",
"Windows Official/Windows Default.theme",
]
},{
name: "Nord",
description: `<a href="https://www.nordtheme.com/">Nord</a> is described as βan arctic, north-bluish color palette.β <br/> (The Wavefront Material Template Library (MTL) file format is not supported yet, but it loads anyways just with some extra colors. Similarly, the CSS-like files load just with duplicates.)`,
dir: "Nord/",
fnames: [
"nord.css",
"nord.scss",
"nord.less",
"nord.styl",
"native/nord.aco",
"native/nord.ase",
// "native/nord.gpa",
// "native/nord.clr",
"native/nord.mtl",
]
},{
name: "DawnBringer's palettes",
description: "<a href='http://www.pixeljoint.com/p/23821.htm'>DawnBringer</a>'s 16 and 32 color palettes in various formats. <a href='http://www.pixeljoint.com/forum/forum_posts.asp?TID=12795'>DB16</a> and <a href='http://www.pixeljoint.com/forum/forum_posts.asp?TID=16247'>DB32</a>",
dir: "DawnBringer/",
fnames: [
"DawnBringers-32-color-palette-pdn.txt",
"db32.pal",
"db32.act",
"db32.gpl",
"db16.gpl",
//"db32_v1_pal64x32.png",
]
},{
name: "Visibone",
description: "<a href='http://www.visibone.com/swatches/'>Visibone</a> color swatches in various formats",
dir: "Other/",
fnames: [
"VisiBone2.aco",
"VisiBone2.gimp",
"VisiBone2_ap.hpl",
"VisiBone2_km.psppalette"
]
},{
disabled: true,
name: "Adobe Color Book files",
description: "",
dir: "AdobeColorBook/",
fnames: [
"PANTONE metallic coated.acb",
"PANTONE pastel coated.acb",
"PANTONE pastel uncoated.acb",
"PANTONE process coated.acb",
"PANTONE process uncoated.acb",
"PANTONE solid coated.acb",
"PANTONE solid matte.acb",
"PANTONE solid to process EURO.acb",
"PANTONE solid to process.acb",
"PANTONE solid uncoated.acb",
"TOYO Color Finder.acb",
"TOYO Process Color Finder.acb",
"TRUMATCH.acb",
"ANPA Color.acb",
"DIC Color Guide.acb",
"FOCOLTONE.acb",
"HKS E Process.acb",
"HKS E.acb",
"HKS K Process.acb",
"HKS K.acb",
"HKS N Process.acb",
"HKS N.acb",
"HKS Z Process.acb",
"HKS Z.acb",
]
},{
name: "StarCraft palettes",
description: "",//"Load even StarCraft's palette formats!",
dir: "StarCraft/",
fnames: [
"bfire.pal",
"gfire.pal",
"ofire.pal",
"Icons.pal",
"Terrain.pal",
"Units.pal", // (exactly same as Wireframe.pal)
]
},{
// TODO: make this not part of this silly palette sections definition object
name: "Try your own palettes",
description: "<button id='select-files'>Select Files</button> or drag and drop. If you don't have any palettes handy, you could try searching online for some palette files to load. Also try HTML, CSS, JS, SVG and other text files that have CSS-style colors in them.",
// TODO: use a native file input? Maybe say "Drag and drop or <input type="file" multiple>" on one line and the rest of the stuff in a separate <p>
// TODO: maybe add a Clear button
fnames: []
}], function(i, group){
if(group.disabled) return;
//var $section = $("<section>").appendTo("body");
var $h2 = $("<h2>").appendTo("body").text(group.name);
var $p = $("<p>").appendTo("body").html(group.description);
$.each(group.fnames, function(i, fname){
var url = "palettes/" + group.dir + fname;
var palette_view = new PaletteView(fname, url);
palette_view.appendTo("body");
palette_view.status("Waiting...");
setTimeout(function(){
palette_view.status("Loading file...");
fetch(url)
.then((response)=> {
if (!response.ok) {
palette_view.error(`Failed to load file! HTTP ${response.status} ${response.statusText}`);
return;
}
response.arrayBuffer()
.then((arrayBuffer)=> {
palette_view.status("Loading palette from file...");
requestAnimationFrame(()=> { // just give time for the loading message to change
AnyPalette.loadPalette({
data: arrayBuffer,
fileName: fname
}, function(err, palette, formatUsed, matchedFileExtension){
palette_view.status("");
palette_view.update(err, palette, formatUsed, matchedFileExtension);
});
});
});
}, (error)=> {
palette_view.error(`Failed to load file! ${error}`);
});
}, i * 100); //stagger loading
});
});
function addPaletteViewsFromFiles(files){
files.forEach((file, i)=> {
var palette_view = new PaletteView("β’ "+file.name);
palette_view.appendTo("body");
palette_view.status("Loading palette from file...");
setTimeout(function(){
AnyPalette.loadPalette(file, function(err, palette, formatUsed, matchedFileExtension){
palette_view.update(err, palette, formatUsed, matchedFileExtension);
if (palette) {
window.console && console.log(`New palette: ${palette.map(()=> `%cβ`).join("")}`, ...palette.map((color)=> `color: ${color};`));
window.console && console.log(palette);
}
});
}, i * 100); //stagger loading
});
}
var file_input = document.createElement("input");
file_input.type = "file";
file_input.multiple = "multiple";
$(file_input).on("change", function(e){
addPaletteViewsFromFiles([...file_input.files]);
});
document.getElementById("select-files").addEventListener("click", function(){
file_input.click();
});
// TODO: drop indicator
$(window).on("dragover dragenter", function(e){
e.preventDefault();
e.stopPropagation();
}).on("drop", async function(e){
e.preventDefault();
e.stopPropagation();
const dt = e.originalEvent.dataTransfer;
if (!dt) return;
if (dt.items) {
const items = await getAllFileEntries(dt.items);
const files = await Promise.all(items.map((item)=> new Promise((resolve, reject)=> {
item.file(resolve, reject);
})));
addPaletteViewsFromFiles(files.length ? files : [...dt.files]);
} else if(dt.files){
addPaletteViewsFromFiles([...dt.files]);
}
});
// Drop handler function to get all files
async function getAllFileEntries(dataTransferItemList) {
let fileEntries = [];
// Use BFS to traverse entire directory/file structure
let queue = [];
// Unfortunately dataTransferItemList is not iterable i.e. no forEach
for (let i = 0; i < dataTransferItemList.length; i++) {
const entry = dataTransferItemList[i].webkitGetAsEntry();
if (entry) {
queue.push(entry);
}
}
while (queue.length > 0) {
let entry = queue.shift();
if (entry.isFile) {
fileEntries.push(entry);
} else if (entry.isDirectory) {
let reader = entry.createReader();
queue.push(...await readAllDirectoryEntries(reader));
}
}
return fileEntries;
}
// Get all the entries (files or sub-directories) in a directory by calling readEntries until it returns empty array
async function readAllDirectoryEntries(directoryReader) {
let entries = [];
let readEntries = await readEntriesPromise(directoryReader);
while (readEntries.length > 0) {
entries.push(...readEntries);
readEntries = await readEntriesPromise(directoryReader);
}
return entries;
}
// Wrap readEntries in a promise to make working with readEntries easier
async function readEntriesPromise(directoryReader) {
try {
return await new Promise((resolve, reject) => {
directoryReader.readEntries(resolve, reject);
});
} catch (err) {
console.log(err);
}
}
</script>
</body>
</html>