Class: Colorscore::Histogram

Inherits:
Object
  • Object
show all
Defined in:
lib/colorscore/histogram.rb

Instance Method Summary collapse

Constructor Details

#initialize(image_path, colors = 16, depth = 8) ⇒ Histogram

Returns a new instance of Histogram.



5
6
7
8
# File 'lib/colorscore/histogram.rb', line 5

def initialize(image_path, colors=16, depth=8)
  output = `convert #{image_path.shellescape} -resize 400x400 -format %c -dither None -quantize YIQ -colors #{colors.to_i} -depth #{depth.to_i} histogram:info:-`
  @lines = output.lines.sort.reverse.map(&:strip).reject(&:empty?)
end

Instance Method Details

#color_countsObject



16
17
18
# File 'lib/colorscore/histogram.rb', line 16

def color_counts
  @lines.map { |line| line.split(':')[0].to_i }
end

#colorsObject

Returns an array of colors in descending order of occurances.



11
12
13
14
# File 'lib/colorscore/histogram.rb', line 11

def colors
  hex_values = @lines.map { |line| line[/#([0-9A-F]{6}) /, 1] }.compact
  hex_values.map { |hex| Color::RGB.from_html(hex) }
end

#scoresObject



20
21
22
23
24
# File 'lib/colorscore/histogram.rb', line 20

def scores
  total = color_counts.inject(:+).to_f
  scores = color_counts.map { |count| count / total }
  scores.zip(colors)
end