13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/paleta/palette.rb', line 13
def generate_from_image(path, size = 5)
include Magick
begin
image = Magick::ImageList.new(path)
quantized_image = image.quantize((Math.sqrt(size).ceil ** 2), Magick::RGBColorspace)
colors = quantized_image.color_histogram.sort { |a, b| b[1] <=> a[1] }[0..(size - 1)].map do |color|
Paleta::Color.new(color[0].red / 256, color[0].green / 256, color[0].blue / 256)
end
return Paleta::Palette.new(colors)
rescue Magick::ImageMagickError
raise "Invalid image at " << path
end
end
|