Class: Swatchman::Palette

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(histogram, size) ⇒ Palette

Returns a new instance of Palette.



7
8
9
10
# File 'lib/swatchman/palette.rb', line 7

def initialize(histogram, size)
  @histogram = histogram
  @size = size
end

Instance Attribute Details

#sizeObject (readonly)

Returns the value of attribute size.



5
6
7
# File 'lib/swatchman/palette.rb', line 5

def size
  @size
end

Instance Method Details

#swatchesObject



12
13
14
15
16
# File 'lib/swatchman/palette.rb', line 12

def swatches
  histogram_swatches.sort_by do |swatch|
    swatch.frequency / total
  end.reverse.slice(0, size)
end

#totalObject



18
19
20
# File 'lib/swatchman/palette.rb', line 18

def total
  @total ||= histogram_swatches.map(&:frequency).reduce(:+).to_f
end