Class: Btape::Palette::MedianCut

Inherits:
Object
  • Object
show all
Defined in:
lib/btape/palette.rb

Overview

Repeatedly splits the colours in use along their widest axis, so that each of the 256 boxes it ends up with holds a similar share of the pixels. Colours a frame leans on get more of the palette than colours it barely touches.

Defined Under Namespace

Classes: Box

Instance Method Summary collapse

Constructor Details

#initialize(histogram, size: SIZE) ⇒ MedianCut

Returns a new instance of MedianCut.



178
179
180
181
# File 'lib/btape/palette.rb', line 178

def initialize(histogram, size: SIZE)
  @entries = histogram.map { |cell, weight| Palette.rgb(cell) << weight }
  @size = size
end

Instance Method Details

#coloursObject



183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/btape/palette.rb', line 183

def colours
  return [[0, 0, 0]] if @entries.empty?

  boxes = [Box.new(@entries)]
  while boxes.length < @size
    index = widest(boxes)
    break unless index

    boxes[index, 1] = boxes[index].split
  end
  boxes.map(&:colour)
end