1
0
Fork 0
mirror of https://github.com/Orama-Interactive/Pixelorama.git synced 2025-01-18 09:09:47 +00:00

Merge pull request #168 from novhack/png-palette-import

Add palette import from a PNG file
This commit is contained in:
Manolis Papadeas 2020-03-03 14:08:07 +02:00 committed by GitHub
commit bbb4393319
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 1 deletions

View file

@ -1478,7 +1478,7 @@ visible = false
[node name="NewPaletteDialog" parent="." instance=ExtResource( 52 )]
[node name="PaletteImportFileDialog" parent="." instance=ExtResource( 53 )]
filters = PoolStringArray( "*.json ; JavaScript Object Notation", "*.gpl ; Gimp Palette Library" )
filters = PoolStringArray( "*.json ; JavaScript Object Notation", "*.gpl ; Gimp Palette Library", "*.png; Portable Network Graphics" )
current_dir = "C:/Users/Overloaded/Dropbox/Orama Founding Members/εταιρικα αρχεια/Godot Projects/Pixelorama"
current_path = "C:/Users/Overloaded/Dropbox/Orama Founding Members/εταιρικα αρχεια/Godot Projects/Pixelorama/"

View file

@ -87,3 +87,33 @@ func import_gpl(path : String) -> Palette:
file.close()
return result
func import_png_palette(path: String) -> Palette:
var result: Palette = null
var image := Image.new()
var err := image.load(path)
if err != OK: # An error occured
return null
var height: int = image.get_height()
var width: int = image.get_width()
result = Palette.new()
# Iterate all pixels and store unique colors to palete
image.lock()
for y in range(0, height):
for x in range(0, width):
var color: Color = image.get_pixel(x, y)
if not result.has_color(color):
result.add_color(color, "#" + color.to_html())
image.unlock()
var name_start = path.find_last('/') + 1
var name_end = path.find_last('.')
if name_end > name_start:
result.name = path.substr(name_start, name_end - name_start)
return result
pass

View file

@ -50,6 +50,12 @@ func get_color_data(index : int) -> String:
return result
func has_color(color: Color) -> bool:
for palette_color in colors:
if palette_color.color == color:
return true
return false
func set_color_data(index : int, new_color : String) -> void:
if index < colors.size():
colors[index].data = new_color

View file

@ -43,6 +43,8 @@ func on_palette_import_file_selected(path : String) -> void:
palette = Palette.new().load_from_file(path)
elif path.to_lower().ends_with("gpl"):
palette = Import.import_gpl(path)
elif path.to_lower().ends_with("png"):
palette = Import.import_png_palette(path)
if palette:
if not Global.palettes.has(palette.name):