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

Implement Columns field of imported .gpl palettes. (#1025)

* Implement Columns field of imported .gpl palettes.

- Fixes Columns field being ignored when imported.
- _fill_imported_palette_with_colors is modified to accept an optional
   width parameter, which defaults to Palette.DEFAULT_WIDTH instead of
   a hard-coded 8.
- A maximum of width of 16384 is enforced to mitigate maliciously large
  values.

* Run gdformat.

* Run gdlint.

* Consume all whitespace between field and value.
This commit is contained in:
Anaminus 2024-06-09 11:19:06 +00:00 committed by GitHub
parent 8ec5653d67
commit f69e2d06eb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -10,6 +10,8 @@ enum NewPalettePresetType {EMPTY, FROM_CURRENT_PALETTE, FROM_CURRENT_SPRITE, FRO
## Color options when user creates a new palette from current sprite or selection
enum GetColorsFrom { CURRENT_FRAME, CURRENT_CEL, ALL_FRAMES }
const DEFAULT_PALETTE_NAME := "Default"
## Maximum allowed width of imported palettes.
const MAX_IMPORT_PAL_WIDTH = 1 << 14
var palettes_write_path := Global.home_data_directory.path_join("Palettes")
## All available palettes
var palettes := {}
@ -447,6 +449,7 @@ func _import_gpl(path: String, text: String) -> Palette:
var line_number := 0
var palette_name := path.get_basename().get_file()
var comments := ""
var columns := 0
var colors := PackedColorArray()
for line in lines:
@ -464,8 +467,11 @@ func _import_gpl(path: String, text: String) -> Palette:
elif line.begins_with("Name: "):
palette_name = line.replace("Name: ", "")
elif line.begins_with("Columns: "):
# Number of colors in this palette. Unnecessary and often wrong
continue
# The width of the palette.
line = line.trim_prefix("Columns: ").strip_edges()
if !line.is_valid_int():
continue
columns = line.to_int()
elif line_number > 0 && line.length() >= 9:
line = line.replace("\t", " ")
var color_data: PackedStringArray = line.split(" ", false, 4)
@ -481,7 +487,7 @@ func _import_gpl(path: String, text: String) -> Palette:
line_number += 1
if line_number > 0:
return _fill_imported_palette_with_colors(palette_name, colors, comments)
return _fill_imported_palette_with_colors(palette_name, colors, comments, columns)
return result
@ -522,13 +528,20 @@ func _import_image_palette(path: String, image: Image) -> Palette:
return _fill_imported_palette_with_colors(path.get_basename().get_file(), colors)
## Fills a new [Palette] with colors. Used when importing files.
## TODO: Somehow let the users choose the fixed height or width, instead of hardcoding 8.
## Fills a new [Palette] with colors. Used when importing files. Dimensions are
## determined by taking colors as a one-dimensional array that is wrapped by
## width.
func _fill_imported_palette_with_colors(
palette_name: String, colors: PackedColorArray, comment := ""
palette_name: String,
colors: PackedColorArray,
comment := "",
width := Palette.DEFAULT_WIDTH,
) -> Palette:
var height := ceili(colors.size() / 8.0)
var result := Palette.new(palette_name, 8, height, comment)
if width <= 0:
width = Palette.DEFAULT_WIDTH
width = clampi(width, 1, MAX_IMPORT_PAL_WIDTH)
var height := ceili(colors.size() / float(width))
var result := Palette.new(palette_name, width, height, comment)
for color in colors:
result.add_color(color)