forked from drtoast/macruby_graphics
/
color_sampler_example.rb
72 lines (61 loc) · 2.3 KB
/
color_sampler_example.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
framework 'Cocoa'
HERE = File.expand_path(File.dirname(__FILE__))
require File.join(HERE, '..', 'graphics')
require File.join(HERE, 'app_wrapper')
class CustomView < NSView
include MRGraphics
def flip_order(ord)
ord == 'ltr' ? 'rtl' : 'ltr'
end
def calculate_position(x, y, order, rect, offset=20)
@width ||= CGRectGetWidth(rect)
if (x + offset > @width) || ((x - offset) == -20 && y != 0 && order == 'rtl')
order = flip_order(order)
y += offset
new_row = true
end
unless x == 0 && new_row
x = order == 'ltr' ? x + offset : x - offset
end
[x, y, order]
end
def drawRect(rect)
dimensions = [CGRectGetWidth(rect), CGRectGetHeight(rect)]
Canvas.for_current_context(:size => dimensions) do |c|
c.background(Color.gray.lighten(0.4))
# load image and grab colors
img = Image.new(File.join(HERE, 'images', '1984.jpg')).saturation(1.9)
# drawing the photo
img_x = 100
# y = height - resized img height - margin
img_y = dimensions.last - 200 - 20
c.draw(img.resize(200, 200), img_x, img_y)
# drawing the text
c.text("Color sampling from jpg", 100, img_y - 30)
colors = img.colors(500)
sample = Path.new.rect
x, y = -20, 0
order = 'ltr'
# sorting colors isn't quite easy
# we are using spherical coordinates and multipass sorting
theta_sorter = lambda{|cs, window| cs.each_slice(window).map{|s| s.sort_by {|c| c.spherical_coordinates.first} }.flatten}
psi_sorter = lambda{|cs, window| cs.each_slice(window).map{|s| s.sort_by {|c| c.spherical_coordinates.last} }.flatten}
# sort on psi
first_sort = colors.sort_by{|c| c.spherical_coordinates.last }
second_sort = theta_sorter.call(first_sort, 25)
# third_sort = theta_sorter.call(first_sort, 15) #psi_sorter.call(first_sort, 35)
second_sort.reverse.each do |color|
# skip of the color is too white or too black
next if color.white? || color.brightness < 0.2
x, y, order = calculate_position(x, y, order, rect)
c.push
sample.fill(color)
c.draw(sample, x, y)
c.pop
end
end
end
end
app = AppWrapper.new(400,520)
app.window.contentView = CustomView.alloc.initWithFrame(app.frame)
app.start