-
Notifications
You must be signed in to change notification settings - Fork 1
/
image_color.rb
39 lines (35 loc) · 1.04 KB
/
image_color.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
require "mini_magick"
module MiniMagick
class Image
def get_median_color
color = run_command("convert", path, "-format", "%c\n", "-colors", 1, "-depth", 8, "histogram:info:")
{
hex: color.split[-2][0..6],
rgb: color.split[-1][/\(.*\)/]
}
end
def get_dominant_color
txt = run_command("convert", path, "-format", "%c\n", "-colors", 256, "-depth", 8, "histogram:info:")
color = txt.lines.sort.reverse[0]
{
hex: color.split[-2][0..6],
rgb: color.split[1]
}
end
def get_dominant_colors n
colors = []
return colors if n < 1
txt = run_command("convert", path, "-format", "%c\n", "-colors", 256, "-depth", 8, "histogram:info:")
txt.lines.sort.reverse[0..n-1].each do |x|
colors << x.split[-2][0..6]
end
colors
end
def pixel_at(x, y)
case run_command("convert","#{path}[1x1+#{x}+#{y}]", "-depth", 8, "txt:").split("\n")[1]
when /^0,0:.*(#[\da-fA-F]{6}).*$/ then $1
else nil
end
end
end
end