Class: Camalian::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/camalian/image.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ Image

Returns a new instance of Image.



7
8
9
# File 'lib/camalian/image.rb', line 7

def initialize(file_path)
  @src_file_path = file_path
end

Instance Attribute Details

#src_file_pathObject

Returns the value of attribute src_file_path.



5
6
7
# File 'lib/camalian/image.rb', line 5

def src_file_path
  @src_file_path
end

Instance Method Details

#prominent_colors(count = Camalian.options[:color_count]) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/camalian/image.rb', line 11

def prominent_colors(count=Camalian.options[:color_count])
  image = ::Magick::Image.read(@src_file_path)[0]
  image.resize!(100,100)
  colors = Palette.new
  initial_count = count
  q = nil
  loop do
    q = image.quantize(initial_count, Magick::RGBColorspace)
    break if q.color_histogram.size > count
    initial_count = initial_count + 10
  end
  palette = q.color_histogram.sort {|a, b| b[1] <=> a[1]}
  (0..[count, palette.count].min - 1).each do |i|
      c = palette[i].first.to_s.split(',').map {|x| x[/\d+/]}
      c.pop
      c[0], c[1], c[2] = [c[0], c[1], c[2]].map { |s|
          s = s.to_i
          s = s / 255 if s / 255 > 0 # not all ImageMagicks are created equal....
          s
      }
      colors << Color.new(c[0],c[1],c[2])
  end
  return colors.uniq(&:to_hex)
end