Class: Btape::Palette::Adaptive
- Inherits:
-
Object
- Object
- Btape::Palette::Adaptive
- Defined in:
- lib/btape/palette.rb
Overview
Colours chosen from the frames themselves by median cut.
Constant Summary collapse
- MAX_SAMPLES_PER_IMAGE =
Enough of each frame to describe what colours it uses. Reading every pixel of every frame would cost far more and change the answer very little.
60_000
Instance Attribute Summary collapse
-
#colours ⇒ Object
readonly
Returns the value of attribute colours.
Class Method Summary collapse
Instance Method Summary collapse
- #index_for(pixel) ⇒ Object
-
#initialize(histogram) ⇒ Adaptive
constructor
A new instance of Adaptive.
- #to_gct ⇒ Object
Constructor Details
#initialize(histogram) ⇒ Adaptive
Returns a new instance of Adaptive.
86 87 88 89 90 91 92 |
# File 'lib/btape/palette.rb', line 86 def initialize(histogram) @colours = MedianCut.new(histogram).colours # One entry per five-bit colour cell, filled in as cells are met. A # screenshot touches a small fraction of them, so searching the # palette for the rest would be work thrown away. @lookup = Array.new(CELLS) end |
Instance Attribute Details
#colours ⇒ Object (readonly)
Returns the value of attribute colours.
94 95 96 |
# File 'lib/btape/palette.rb', line 94 def colours @colours end |
Class Method Details
.from(images) ⇒ Object
68 69 70 |
# File 'lib/btape/palette.rb', line 68 def self.from(images) new(histogram(images)) end |
.histogram(images) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/btape/palette.rb', line 72 def self.histogram(images) counts = Hash.new(0) images.each do |image| pixels = image.pixels stride = [pixels.length / MAX_SAMPLES_PER_IMAGE, 1].max index = 0 while index < pixels.length counts[Palette.cell(pixels[index])] += 1 index += stride end end counts end |
Instance Method Details
#index_for(pixel) ⇒ Object
96 97 98 99 |
# File 'lib/btape/palette.rb', line 96 def index_for(pixel) cell = Palette.cell(pixel) @lookup[cell] || (@lookup[cell] = nearest(cell)) end |
#to_gct ⇒ Object
101 102 103 |
# File 'lib/btape/palette.rb', line 101 def to_gct Palette.colour_table(@colours) end |