Permalink
Browse files

new example (color sampler)

  • Loading branch information...
1 parent f053351 commit cfdd65508b9ae8a25689860958aa03c179e6ac2f @mattetti committed Jul 16, 2010
Showing with 84 additions and 2 deletions.
  1. +2 −2 README.md
  2. +72 −0 examples/color_sampler_example.rb
  3. BIN examples/images/1984.jpg
  4. +10 −0 lib/color.rb
View
@@ -112,5 +112,5 @@ _see the examples folder for the source code of each image's source code._
![MacRuby Graphics Image resizing](http://img.skitch.com/20100715-k8k8f1gd8rb9e1wfj4ush9i5bf.png)
![MacRuby Graphics randomize](http://img.skitch.com/20100715-tycucqsgsfiy7syef8i24sw9xj.png)
![MacRuby Graphics Spirograph](http://img.skitch.com/20100715-jh4nsrm193a2ttdmjjnh4g1x96.png)
-![MacRuby Graphics Rubbons](http://img.skitch.com/20100715-18f5pwc96b2gdfcdag26sjujam.png
-)
+![MacRuby Graphics color sampler](http://img.skitch.com/20100716-nth8dcm4ag12bcns1fgngt4ird.png)
+![MacRuby Graphics Rubbons](http://img.skitch.com/20100715-18f5pwc96b2gdfcdag26sjujam.png)
@@ -0,0 +1,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
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -800,6 +800,16 @@ def white?
false
end
end
+
+ # The spherical coordinates (θ, φ) of the color can be obtained from Cartesian coordinates (x, y, z)
+ # using the YUV values, we are getting 2 spherical coordinates
+ # r isn't calculated
+ # By using theta and psi, one can sort colors
+ def spherical_coordinates
+ theta = Math.acos(y / (Math.sqrt(u*u + v*v + y*y)))
+ psi = Math.atan2(v, u)
+ [theta, psi]
+ end
private

0 comments on commit cfdd655

Please sign in to comment.