Skip to content

Commit

Permalink
Add a crop_image boolean parameter to Palette.convert_to_image()
Browse files Browse the repository at this point in the history
Fixes some issues with the Palettize effect where the output would be different if the palette size changed and empty swatches were added, even if the colors themselves stayed the same.
  • Loading branch information
OverloadedOrama committed Nov 22, 2024
1 parent 996a234 commit bd7d3b1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/Classes/ImageExtended.gd
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func update_palette() -> void:
func convert_indexed_to_rgb() -> void:
if not is_indexed:
return
var palette_image := Palettes.current_palette.convert_to_image()
var palette_image := Palettes.current_palette.convert_to_image(false)
var palette_texture := ImageTexture.create_from_image(palette_image)
var shader_image_effect := ShaderImageEffect.new()
var indices_texture := ImageTexture.create_from_image(indices_image)
Expand All @@ -98,7 +98,7 @@ func convert_indexed_to_rgb() -> void:
func convert_rgb_to_indexed() -> void:
if not is_indexed:
return
var palette_image := Palettes.current_palette.convert_to_image()
var palette_image := Palettes.current_palette.convert_to_image(false)
var palette_texture := ImageTexture.create_from_image(palette_image)
var params := {
"palette_texture": palette_texture, "rgb_texture": ImageTexture.create_from_image(self)
Expand Down
6 changes: 4 additions & 2 deletions src/Palette/Palette.gd
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var colors_max := 0


class PaletteColor:
var color := Color.TRANSPARENT
var color := Color(0, 0, 0, 0)
var index := -1

func _init(init_color := Color.BLACK, init_index := -1) -> void:
Expand Down Expand Up @@ -358,9 +358,11 @@ static func strip_unvalid_characters(string_to_strip: String) -> String:
return regex.sub(string_to_strip, "", true)


func convert_to_image() -> Image:
func convert_to_image(crop_image := true) -> Image:
var image := Image.create(colors_max, 1, false, Image.FORMAT_RGBA8)
for i in colors_max:
if colors.has(i):
image.set_pixel(i, 0, Color(colors[i].color.to_html()))
if crop_image:
image.copy_from(image.get_region(image.get_used_rect()))
return image

0 comments on commit bd7d3b1

Please sign in to comment.