From 125e332b7cd97434123797c8f7d9688329c816e3 Mon Sep 17 00:00:00 2001 From: jdescottes Date: Sun, 7 Sep 2014 18:25:17 +0200 Subject: [PATCH] Enhancement : palette color creator - Added import of GPL files --- src/js/service/palette/PaletteGplReader.js | 50 +++++++++++++++++++ .../service/palette/PaletteImportService.js | 31 +++++++++--- src/js/utils/Base64.js | 4 ++ src/js/utils/BlobUtils.js | 2 + src/js/utils/PiskelFileUtils.js | 2 +- src/piskel-script-list.js | 1 + 6 files changed, 82 insertions(+), 8 deletions(-) create mode 100644 src/js/service/palette/PaletteGplReader.js diff --git a/src/js/service/palette/PaletteGplReader.js b/src/js/service/palette/PaletteGplReader.js new file mode 100644 index 0000000..11d10f3 --- /dev/null +++ b/src/js/service/palette/PaletteGplReader.js @@ -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(); + } + + }; +})(); \ No newline at end of file diff --git a/src/js/service/palette/PaletteImportService.js b/src/js/service/palette/PaletteImportService.js index 3900118..bb10f1f 100644 --- a/src/js/service/palette/PaletteImportService.js +++ b/src/js/service/palette/PaletteImportService.js @@ -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]; }; })(); \ No newline at end of file diff --git a/src/js/utils/Base64.js b/src/js/utils/Base64.js index 8e8273f..6652f63 100644 --- a/src/js/utils/Base64.js +++ b/src/js/utils/Base64.js @@ -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]; diff --git a/src/js/utils/BlobUtils.js b/src/js/utils/BlobUtils.js index 8ef595b..169c2f9 100644 --- a/src/js/utils/BlobUtils.js +++ b/src/js/utils/BlobUtils.js @@ -1,6 +1,8 @@ (function () { var ns = $.namespace('pskl.utils'); + + var BASE64_REGEX = /\s*;\s*base64\s*(?:;|$)/i; ns.BlobUtils = { diff --git a/src/js/utils/PiskelFileUtils.js b/src/js/utils/PiskelFileUtils.js index 901aa4b..59d64ae 100644 --- a/src/js/utils/PiskelFileUtils.js +++ b/src/js/utils/PiskelFileUtils.js @@ -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); diff --git a/src/piskel-script-list.js b/src/piskel-script-list.js index e9adaeb..e8dd968 100644 --- a/src/piskel-script-list.js +++ b/src/piskel-script-list.js @@ -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",