Skip to content

Commit

Permalink
Add PAL palette format import (#315)
Browse files Browse the repository at this point in the history
* Add PAL palette format import

* Move .pal selection into Dialog; Attempt HTML implementation

* Fix issue with HTML5 pal palette import.
  • Loading branch information
matthewpaul-us authored Aug 26, 2020
1 parent f121c39 commit 348d758
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/Autoload/HTML5FileExchange.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down Expand Up @@ -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":
Expand Down
25 changes: 25 additions & 0 deletions src/Autoload/Import.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions src/Palette/PaletteContainer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/Palette/PaletteImportFileDialog.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -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/"

0 comments on commit 348d758

Please sign in to comment.