Enhancement : palette color creator

- Added import of GPL files
This commit is contained in:
jdescottes 2014-09-07 18:25:17 +02:00
parent e457209c8f
commit 125e332b7c
6 changed files with 82 additions and 8 deletions

View file

@ -0,0 +1,50 @@
(function () {
var ns = $.namespace('pskl.service.palette');
var RE_COLOR_LINE = /^(\d{1,3}\s+)(\d{1,3}\s+)(\d{1,3}\s+)/;
var RE_EXTRACT_NAME = /^name\s*\:\s*(.*)$/i;
ns.PaletteGplReader = function (file, onSuccess, onError) {
this.file = file;
this.onSuccess = onSuccess;
this.onError = onError;
};
ns.PaletteGplReader.prototype.read = function () {
pskl.utils.FileUtils.readFile(this.file, this.onFileLoaded_.bind(this));
};
ns.PaletteGplReader.prototype.onFileLoaded_ = function (content) {
var text = pskl.utils.Base64.toText(content);
var lines = text.match(/[^\r\n]+/g);
var name = lines.map(function (l) {
var matches = l.match(RE_EXTRACT_NAME);
return matches ? matches[1] : '';
}).join('');
var colorLines = lines.filter(function (l) {
return RE_COLOR_LINE.test(l);
});
var colors = colorLines.map(function (l) {
var matches = l.match(RE_COLOR_LINE);
var color = window.tinycolor({
r : parseInt(matches[1], 10),
g : parseInt(matches[2], 10),
b : parseInt(matches[3], 10)
});
return color.toRgbString();
});
if (name && colors.length) {
var uuid = pskl.utils.Uuid.generate();
var palette = new pskl.model.Palette(uuid, name, colors);
this.onSuccess(palette);
} else {
this.onError();
}
};
})();

View file

@ -1,26 +1,43 @@
(function () {
var ns = $.namespace('pskl.service.palette');
var supportedFileFormats = ['gpl'];
ns.PaletteImportService = function () {};
ns.PaletteImportService.prototype.read = function (file, onSuccess, onError) {
var reader;
if (this.isImage_(file)){
var reader = new ns.PaletteImageReader(file, onSuccess, onError);
reader.read();
reader = new ns.PaletteImageReader(file, onSuccess, onError);
} else if (this.isSupportedFormat_(file)) {
this.importFile(file);
reader = this.getFileReader_(file, onSuccess, onError);
}
};
ns.PaletteImportService.prototype.importFile = function (file) {
if (reader) {
reader.read();
} else {
throw 'Could not find reader for file : ' + file.name;
}
};
ns.PaletteImportService.prototype.isImage_ = function (file) {
return file.type.indexOf('image') === 0;
};
ns.PaletteImportService.prototype.getFileReader_ = function (file, onSuccess, onError) {
var extension = this.getExtension_(file);
if (extension === 'gpl') {
return new ns.PaletteGplReader(file, onSuccess, onError);
}
};
ns.PaletteImportService.prototype.isSupportedFormat_ = function (file) {
return (/\.piskel$/).test(file.name);
var extension = this.getExtension_(file);
return supportedFileFormats.indexOf(extension) != -1;
};
ns.PaletteImportService.prototype.getExtension_ = function (file) {
var parts = file.name.split('.');
return parts[parts.length-1];
};
})();

View file

@ -13,6 +13,10 @@
}
ns.Base64 = {
toText : function (base64) {
return window.atob(base64.replace(/data\:.*?\;base64\,/,''));
},
decode : function(base64) {
var outptr = 0;
var last = [0, 0];

View file

@ -1,6 +1,8 @@
(function () {
var ns = $.namespace('pskl.utils');
var BASE64_REGEX = /\s*;\s*base64\s*(?:;|$)/i;
ns.BlobUtils = {

View file

@ -12,7 +12,7 @@
*/
loadFromFile : function (file, onSuccess, onError) {
pskl.utils.FileUtils.readFile(file, function (content) {
var rawPiskel = window.atob(content.replace(/data\:.*?\;base64\,/,''));
var rawPiskel = pskl.utils.Base64.toText(content);
var serializedPiskel = JSON.parse(rawPiskel);
var fps = serializedPiskel.piskel.fps;
var descriptor = new pskl.model.piskel.Descriptor(serializedPiskel.piskel.name, serializedPiskel.piskel.description, true);

View file

@ -121,6 +121,7 @@
"js/service/HistoryService.js",
"js/service/palette/PaletteService.js",
"js/service/palette/PaletteImportService.js",
"js/service/palette/PaletteGplReader.js",
"js/service/palette/PaletteImageReader.js",
"js/service/SavedStatusService.js",
"js/service/keyboard/ShortcutService.js",