Module: Abachrome::PaletteMixins::Resample
- Defined in:
- lib/abachrome/palette_mixins/resample.rb
Instance Method Summary collapse
- #expand(new_size) ⇒ Object
- #expand!(new_size) ⇒ Object
- #reduce(new_size) ⇒ Object
- #reduce!(new_size) ⇒ Object
- #resample(new_size) ⇒ Object
- #resample!(new_size) ⇒ Object
Instance Method Details
#expand(new_size) ⇒ Object
34 35 36 37 38 |
# File 'lib/abachrome/palette_mixins/resample.rb', line 34 def (new_size) raise ArgumentError, "New size must be larger than current size" if new_size <= size resample(new_size) end |
#expand!(new_size) ⇒ Object
40 41 42 43 44 |
# File 'lib/abachrome/palette_mixins/resample.rb', line 40 def (new_size) raise ArgumentError, "New size must be larger than current size" if new_size <= size resample!(new_size) end |
#reduce(new_size) ⇒ Object
46 47 48 49 50 |
# File 'lib/abachrome/palette_mixins/resample.rb', line 46 def reduce(new_size) raise ArgumentError, "New size must be smaller than current size" if new_size >= size resample(new_size) end |
#reduce!(new_size) ⇒ Object
52 53 54 55 56 |
# File 'lib/abachrome/palette_mixins/resample.rb', line 52 def reduce!(new_size) raise ArgumentError, "New size must be smaller than current size" if new_size >= size resample!(new_size) end |
#resample(new_size) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/abachrome/palette_mixins/resample.rb', line 6 def resample(new_size) return self if new_size == size || empty? return self.class.new([@colors.first]) if new_size == 1 step = (size - 1).to_f / (new_size - 1) self.class.new( (0...new_size).map do |i| index = i * step lower_index = index.floor upper_index = [lower_index + 1, size - 1].min if lower_index == upper_index @colors[lower_index] else fraction = index - lower_index @colors[lower_index].blend(@colors[upper_index], fraction) end end ) end |
#resample!(new_size) ⇒ Object
28 29 30 31 32 |
# File 'lib/abachrome/palette_mixins/resample.rb', line 28 def resample!(new_size) resampled = resample(new_size) @colors = resampled.colors self end |