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.



3
4
5
6
# File 'lib/colorscore/histogram.rb', line 3

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

Instance Method Details

#color_countsObject



14
15
16
# File 'lib/colorscore/histogram.rb', line 14

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

#colorsObject

Returns an array of colors in descending order of occurances.



9
10
11
12
# File 'lib/colorscore/histogram.rb', line 9

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

#scoresObject



18
19
20
21
22
# File 'lib/colorscore/histogram.rb', line 18

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