Class: RubySpriter::BackgroundSampler
- Inherits:
-
Object
- Object
- RubySpriter::BackgroundSampler
- Defined in:
- lib/ruby_spriter/background_sampler.rb
Overview
BackgroundSampler collects unique background colors from interior image regions
Sampling Strategy:
-
Starts at (sample_offset, sample_offset) to avoid edge compression artifacts
-
Samples horizontally across the image with calculated intervals
-
Moves to next row if not enough unique colors found
-
Uses pixel cache for fast lookups (loads all pixels once)
Instance Attribute Summary collapse
-
#image_path ⇒ Object
readonly
Returns the value of attribute image_path.
-
#max_rows ⇒ Object
readonly
Returns the value of attribute max_rows.
-
#sample_count ⇒ Object
readonly
Returns the value of attribute sample_count.
-
#sample_offset ⇒ Object
readonly
Returns the value of attribute sample_offset.
Instance Method Summary collapse
-
#collect_unique_colors ⇒ Object
Collect unique background colors by sampling interior regions.
-
#initialize(image_path, sample_offset = 5, sample_count = 10, max_rows = 20) ⇒ BackgroundSampler
constructor
A new instance of BackgroundSampler.
Constructor Details
#initialize(image_path, sample_offset = 5, sample_count = 10, max_rows = 20) ⇒ BackgroundSampler
Returns a new instance of BackgroundSampler.
16 17 18 19 20 21 22 23 24 |
# File 'lib/ruby_spriter/background_sampler.rb', line 16 def initialize(image_path, sample_offset = 5, sample_count = 10, max_rows = 20) @image_path = image_path @sample_offset = sample_offset @sample_count = sample_count @max_rows = max_rows @image_width = nil @image_height = nil @pixel_cache = nil end |
Instance Attribute Details
#image_path ⇒ Object (readonly)
Returns the value of attribute image_path.
14 15 16 |
# File 'lib/ruby_spriter/background_sampler.rb', line 14 def image_path @image_path end |
#max_rows ⇒ Object (readonly)
Returns the value of attribute max_rows.
14 15 16 |
# File 'lib/ruby_spriter/background_sampler.rb', line 14 def max_rows @max_rows end |
#sample_count ⇒ Object (readonly)
Returns the value of attribute sample_count.
14 15 16 |
# File 'lib/ruby_spriter/background_sampler.rb', line 14 def sample_count @sample_count end |
#sample_offset ⇒ Object (readonly)
Returns the value of attribute sample_offset.
14 15 16 |
# File 'lib/ruby_spriter/background_sampler.rb', line 14 def sample_offset @sample_offset end |
Instance Method Details
#collect_unique_colors ⇒ Object
Collect unique background colors by sampling interior regions
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 |
# File 'lib/ruby_spriter/background_sampler.rb', line 27 def collect_unique_colors load_image_dimensions load_pixel_cache # Input validation if @sample_count < 2 raise ValidationError, "sample_count must be at least 2" end usable_width = @image_width - (2 * @sample_offset) if usable_width <= 0 raise ValidationError, "sample_offset (#{@sample_offset}) too large for image width (#{@image_width})" end unique_colors = [] y = @sample_offset rows_sampled = 0 while unique_colors.length < @sample_count && rows_sampled < @max_rows # Calculate interval across usable width (excluding offset margins on both sides) interval = (@image_width - 2 * @sample_offset).to_f / (@sample_count - 1) # Sample across the width at current y position @sample_count.times do |i| x = @sample_offset + (i * interval).round # Ensure x is within bounds x = x.clamp(@sample_offset, @image_width - @sample_offset - 1) color = sample_pixel(x, y) if color && !color_exists?(unique_colors, color) unique_colors << color break if unique_colors.length >= @sample_count end end # Move to next row y += 1 rows_sampled += 1 end unique_colors end |