Skip to content

Commit

Permalink
Add import support for old json palette format
Browse files Browse the repository at this point in the history
  • Loading branch information
novhack committed Mar 22, 2021
1 parent 930e505 commit 6ce2f4f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Autoload/OpenSave.gd
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func handle_loading_files(files : PoolStringArray) -> void:
var file_ext : String = file.get_extension().to_lower()
if file_ext == "pxo": # Pixelorama project file
open_pxo_file(file)
elif file_ext == "tres" or file_ext == "gpl" or file_ext == "pal": # Palettes
elif file_ext == "tres" or file_ext == "gpl" or file_ext == "pal" or file_ext == "json": # Palettes
Palettes.import_palette(file)
else: # Image files
var image := Image.new()
Expand Down
38 changes: 35 additions & 3 deletions src/Autoload/Palettes.gd
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,13 @@ func import_palette(path: String) -> void:
var err := image.load(path)
if !err:
palette = import_image_palette(path, image)
"json":
var file = File.new()
if file.file_exists(path):
file.open(path, File.READ)
var text = file.get_as_text()
file.close()
palette = import_json_palette(text)

if palette:
var palette_path := save_palette(palette)
Expand All @@ -416,7 +423,7 @@ func import_palette(path: String) -> void:
Global.palette_panel.select_palette(palette_path)


func import_gpl(path : String, text : String) -> Palette:
func import_gpl(path: String, text: String) -> Palette:
# Refer to app/core/gimppalette-load.c of the GIMP for the "living spec"
var result : Palette = null
var lines = text.split('\n')
Expand Down Expand Up @@ -466,7 +473,7 @@ func import_gpl(path : String, text : String) -> Palette:
return result


func import_pal_palette(path : String, text : String) -> Palette:
func import_pal_palette(path: String, text: String) -> Palette:
var result: Palette = null

var lines = text.split('\n')
Expand All @@ -490,7 +497,7 @@ func import_pal_palette(path : String, text : String) -> Palette:
return result


func import_image_palette(path: String, image : Image) -> Palette:
func import_image_palette(path: String, image: Image) -> Palette:
var result: Palette = Palette.new(path.get_basename().get_file())

var height: int = image.get_height()
Expand All @@ -506,3 +513,28 @@ func import_image_palette(path: String, image : Image) -> Palette:
image.unlock()

return result


# Import of deprecated older json palette format
func import_json_palette(text: String):
var result: Palette = Palette.new()
var result_json = JSON.parse(text)

if result_json.error != OK: # If parse has errors
printerr("JSON palette import error")
printerr("Error: ", result_json.error)
printerr("Error Line: ", result_json.error_line)
printerr("Error String: ", result_json.error_string)
result = null
else: # If parse OK
var data = result_json.result
if data.has("name"): # If data is 'valid' palette file
result.name = data.name
if data.has("comments"):
result.comment = data.comments
if data.has("colors"):
for color_data in data.colors:
var color: Color = Color(color_data.data)
result.add_color(color)

return result

0 comments on commit 6ce2f4f

Please sign in to comment.