-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_palette.rb
98 lines (77 loc) · 1.59 KB
/
gen_palette.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
require 'RMagick'
include Magick
def is_img?(file)
# Very simple 'image' verification
fname = file.downcase
fname.include?(".gif") or fname.include?(".png") or fname.include?(".jpg")
end
imgs = []
ARGV.each do |file|
if File.directory?(file)
Dir.entries(file).each do |f|
imgs << "#{file}/#{f}" if is_img?(f)
end
else
if (is_img?(file))
imgs << file
else
puts "\"#{file}\" doesn't seem to be an image."
exit
end
end
end
puts "Looking at files:"
imgs.each { |i| puts " #{i}" }
colors = []
# Collect all the colors
imgs.each do |img|
source = ImageList.new(img)
puts "Opening #{img}"
p = source.unique_colors
puts " #{p.columns} colors"
for i in 0..p.columns
c = p.pixel_color(i, 0)
colors << c
end
colors.uniq! { |v|
"#{v}"
}
puts " #{colors.count} unique colors so far"
end
# Sort them. This could be better.
colors.sort! { |c1, c2|
hue1 = c1.to_hsla[0]
hue2 = c2.to_hsla[0]
sat1 = c1.to_hsla[1]
sat2 = c2.to_hsla[1]
lig1 = c1.to_hsla[2]
lig2 = c2.to_hsla[2]
if (hue1 == hue2)
if (sat1 == sat2)
lig1 <=> lig2
else
sat1 <=> sat2
end
else
hue1 <=> hue2
end
}
# Create the palette image
gc = Magick::Draw.new
gc.stroke_width(1)
gc.affine(1, 0, 0, 1, 0, 0)
max_colors_per_row = 40
start = 0
y = 0
colors.each { |pixel|
if (start + 1)%max_colors_per_row == 0
start = 0
y = y + 1
end
gc.stroke(pixel.to_color)
gc.line(start, y, (start+1), y)
start += 1
}
canvas = Magick::Image.new(max_colors_per_row, (y+1))
gc.draw(canvas)
canvas.write("palette.png")