Class: RubySpriter::CellCleanupProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_spriter/cell_cleanup_processor.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CellCleanupProcessor

Returns a new instance of CellCleanupProcessor.



12
13
14
15
16
# File 'lib/ruby_spriter/cell_cleanup_processor.rb', line 12

def initialize(options = {})
  @config = CellCleanupConfig.new(options)
  @gimp_processor = GimpProcessor.new(options[:gimp_path], options)
  @options = options
end

Instance Method Details

#cleanup_cells(spritesheet_path, options) ⇒ Hash

Main method to cleanup background colors in spritesheet cells

Parameters:

  • spritesheet_path (String)

    Path to the spritesheet PNG

  • options (Hash)

    Processing options

Returns:

  • (Hash)

    Statistics about cleanup process



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ruby_spriter/cell_cleanup_processor.rb', line 22

def cleanup_cells(spritesheet_path, options)
  cell_dims = calculate_cell_dimensions(spritesheet_path, options)
  rows = calculate_rows(options)
  columns = options[:columns]

  temp_dir = create_temp_dir
  cleaned_cells = []
  stats = { processed: 0, cleaned: 0, skipped: 0, colors_removed: 0 }

  puts "  Analyzing spritesheet: #{columns}×#{rows} grid (#{rows * columns} cells)"
  puts "  Dominance threshold: #{@config.threshold}%\n\n"

  (0...rows).each do |row|
    (0...columns).each do |col|
      stats[:processed] += 1

      # Extract cell region
      cell_path = extract_cell(spritesheet_path, row, col, cell_dims[:width], cell_dims[:height], temp_dir)

      # Analyze for dominant colors
      dominant_colors = analyze_cell_colors(cell_path)

      if dominant_colors && !dominant_colors.empty?
        # Remove dominant colors via GIMP
        cleaned_cell = remove_dominant_colors(cell_path, dominant_colors, options, temp_dir)
        cleaned_cells << cleaned_cell
        stats[:cleaned] += 1
        stats[:colors_removed] += dominant_colors.length

        puts "  Cell [#{row},#{col}]: Removed #{dominant_colors.length} dominant color(s)"
      else
        # No cleanup needed
        cleaned_cells << cell_path
        stats[:skipped] += 1
        puts "  Cell [#{row},#{col}]: No dominant colors detected (skipped)"
      end
    end
  end

  # Reassemble cleaned cells
  reassemble_spritesheet(cleaned_cells, columns, rows, spritesheet_path)

  puts "\n  ✓ Cleanup complete"
  puts "  - Processed: #{stats[:processed]} cells"
  puts "  - Cleaned: #{stats[:cleaned]} cells"
  puts "  - Skipped: #{stats[:skipped]} cells"
  puts "  - Dominant colors removed: #{stats[:colors_removed]} total\n"

  stats
ensure
  cleanup_temp_dir(temp_dir) if temp_dir
end