Class: Btape::Palette::MedianCut::Box

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

Overview

Colours in a box, as [red, green, blue, weight] entries. The stats a split needs are worked out once per box: they are read every round, and the box they describe never changes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entries) ⇒ Box

Returns a new instance of Box.



131
132
133
134
135
136
# File 'lib/btape/palette.rb', line 131

def initialize(entries)
  @entries = entries
  @bounds = compute_bounds
  @axis = (0..2).max_by { |channel| @bounds[channel].last - @bounds[channel].first }
  @weight = entries.sum(&:last)
end

Instance Attribute Details

#axisObject (readonly)

Returns the value of attribute axis.



138
139
140
# File 'lib/btape/palette.rb', line 138

def axis
  @axis
end

#entriesObject (readonly)

Returns the value of attribute entries.



138
139
140
# File 'lib/btape/palette.rb', line 138

def entries
  @entries
end

#weightObject (readonly)

Returns the value of attribute weight.



138
139
140
# File 'lib/btape/palette.rb', line 138

def weight
  @weight
end

Instance Method Details

#colourObject



155
156
157
# File 'lib/btape/palette.rb', line 155

def colour
  (0..2).map { |channel| @entries.sum { |entry| entry[channel] * entry.last } / @weight }
end

#priorityObject

Both how far the box spreads and how many pixels are in it: a wide box nothing is using does not deserve the palette entry.



146
147
148
# File 'lib/btape/palette.rb', line 146

def priority
  (@bounds[@axis].last - @bounds[@axis].first) * @weight
end

#splitObject



150
151
152
153
# File 'lib/btape/palette.rb', line 150

def split
  sorted = @entries.sort_by { |entry| entry[@axis] }
  [Box.new(sorted.shift(median_index(sorted))), Box.new(sorted)]
end

#splittable?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/btape/palette.rb', line 140

def splittable?
  @entries.length > 1
end