From 348d758ef4f3bcbeec24fcdd9f2d54719dd1c047 Mon Sep 17 00:00:00 2001 From: Matthew Paul Date: Wed, 26 Aug 2020 05:36:03 -0400 Subject: [PATCH] Add PAL palette format import (#315) * Add PAL palette format import * Move .pal selection into Dialog; Attempt HTML implementation * Fix issue with HTML5 pal palette import. --- src/Autoload/HTML5FileExchange.gd | 6 +++++- src/Autoload/Import.gd | 25 ++++++++++++++++++++++++ src/Palette/PaletteContainer.gd | 7 +++++++ src/Palette/PaletteImportFileDialog.tscn | 2 +- 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/Autoload/HTML5FileExchange.gd b/src/Autoload/HTML5FileExchange.gd index 5fb424991..1d5274e31 100644 --- a/src/Autoload/HTML5FileExchange.gd +++ b/src/Autoload/HTML5FileExchange.gd @@ -47,7 +47,7 @@ func _define_js() -> void: canceled = true; var input = document.createElement('INPUT'); input.setAttribute("type", "file"); - input.setAttribute("accept", "application/json, .gpl, image/png, image/jpeg, image/webp"); + input.setAttribute("accept", "application/json, .gpl, .pal, image/png, image/jpeg, image/webp"); input.click(); input.addEventListener('change', event => { if (event.target.files.length > 0){ @@ -173,6 +173,10 @@ func load_palette() -> void: var palette := Palette.new() palette = Import.import_gpl(file_name, palette_data) Global.palette_container.attempt_to_import_palette(palette) + elif file_name.ends_with(".pal"): + var palette := Palette.new() + palette = Import.import_pal_palette(file_name, palette_data) + Global.palette_container.attempt_to_import_palette(palette) else: match file_type: "image/png": diff --git a/src/Autoload/Import.gd b/src/Autoload/Import.gd index e6c2135fb..c12226ade 100644 --- a/src/Autoload/Import.gd +++ b/src/Autoload/Import.gd @@ -294,3 +294,28 @@ func import_png_palette(path: String, image : Image) -> Palette: result.name = path.get_basename().get_file() return result + + +func import_pal_palette(path : String, text : String) -> Palette: + var result: Palette = null + + var lines = text.split('\n') + + if not 'JASC-PAL' in lines[0] or not '0100' in lines[1]: + return result + else: + result = Palette.new() + result.name = path.get_basename().get_file() + + var num_colors = int(lines[2]) + + for i in range(3, num_colors + 3): + var color_data = lines[i].split(' ') + var red : float = color_data[0].to_float() / 255.0 + var green : float = color_data[1].to_float() / 255.0 + var blue : float = color_data[2].to_float() / 255.0 + + var color = Color(red, green, blue) + result.add_color(color) + + return result diff --git a/src/Palette/PaletteContainer.gd b/src/Palette/PaletteContainer.gd index 932536521..bb6962b5d 100644 --- a/src/Palette/PaletteContainer.gd +++ b/src/Palette/PaletteContainer.gd @@ -58,6 +58,13 @@ func on_palette_import_file_selected(path : String) -> void: var text = file.get_as_text() file.close() palette = Import.import_gpl(path, text) + elif path.to_lower().ends_with("pal"): + var file = File.new() + if file.file_exists(path): + file.open(path, File.READ) + var text = file.get_as_text() + file.close() + palette = Import.import_pal_palette(path, text) elif path.to_lower().ends_with("png") or path.to_lower().ends_with("bmp") or path.to_lower().ends_with("hdr") or path.to_lower().ends_with("jpg") or path.to_lower().ends_with("svg") or path.to_lower().ends_with("tga") or path.to_lower().ends_with("webp"): var image := Image.new() var err := image.load(path) diff --git a/src/Palette/PaletteImportFileDialog.tscn b/src/Palette/PaletteImportFileDialog.tscn index 303886672..dcd98f05c 100644 --- a/src/Palette/PaletteImportFileDialog.tscn +++ b/src/Palette/PaletteImportFileDialog.tscn @@ -10,6 +10,6 @@ window_title = "Open a File" resizable = true mode = 0 access = 2 -filters = PoolStringArray( "*.json ; JavaScript Object Notation", "*.gpl ; Gimp Palette Library", "*.png; Portable Network Graphics" ) +filters = PoolStringArray( "*.json ; JavaScript Object Notation", "*.gpl ; Gimp Palette Library", "*.png; Portable Network Graphics", "*.pal; JASC Palette" ) current_dir = "/Users" current_path = "/Users/"