Class: Btape::Palette::MedianCut::Box
- Inherits:
-
Object
- Object
- Btape::Palette::MedianCut::Box
- 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
-
#axis ⇒ Object
readonly
Returns the value of attribute axis.
-
#entries ⇒ Object
readonly
Returns the value of attribute entries.
-
#weight ⇒ Object
readonly
Returns the value of attribute weight.
Instance Method Summary collapse
- #colour ⇒ Object
-
#initialize(entries) ⇒ Box
constructor
A new instance of Box.
-
#priority ⇒ Object
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.
- #split ⇒ Object
- #splittable? ⇒ Boolean
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
#axis ⇒ Object (readonly)
Returns the value of attribute axis.
138 139 140 |
# File 'lib/btape/palette.rb', line 138 def axis @axis end |
#entries ⇒ Object (readonly)
Returns the value of attribute entries.
138 139 140 |
# File 'lib/btape/palette.rb', line 138 def entries @entries end |
#weight ⇒ Object (readonly)
Returns the value of attribute weight.
138 139 140 |
# File 'lib/btape/palette.rb', line 138 def weight @weight end |
Instance Method Details
#colour ⇒ Object
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 |
#priority ⇒ Object
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 |
#split ⇒ Object
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
140 141 142 |
# File 'lib/btape/palette.rb', line 140 def splittable? @entries.length > 1 end |