Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

new example (color sampler)

  • Loading branch information...
commit cfdd65508b9ae8a25689860958aa03c179e6ac2f 1 parent f053351
Matt Aimonetti authored
4 README.md
Source Rendered
@@ -112,5 +112,5 @@ _see the examples folder for the source code of each image's source code._
112 112 ![MacRuby Graphics Image resizing](http://img.skitch.com/20100715-k8k8f1gd8rb9e1wfj4ush9i5bf.png)
113 113 ![MacRuby Graphics randomize](http://img.skitch.com/20100715-tycucqsgsfiy7syef8i24sw9xj.png)
114 114 ![MacRuby Graphics Spirograph](http://img.skitch.com/20100715-jh4nsrm193a2ttdmjjnh4g1x96.png)
115   -![MacRuby Graphics Rubbons](http://img.skitch.com/20100715-18f5pwc96b2gdfcdag26sjujam.png
116   -)
  115 +![MacRuby Graphics color sampler](http://img.skitch.com/20100716-nth8dcm4ag12bcns1fgngt4ird.png)
  116 +![MacRuby Graphics Rubbons](http://img.skitch.com/20100715-18f5pwc96b2gdfcdag26sjujam.png)
72 examples/color_sampler_example.rb
... ... @@ -0,0 +1,72 @@
  1 +framework 'Cocoa'
  2 +HERE = File.expand_path(File.dirname(__FILE__))
  3 +require File.join(HERE, '..', 'graphics')
  4 +require File.join(HERE, 'app_wrapper')
  5 +
  6 +class CustomView < NSView
  7 + include MRGraphics
  8 +
  9 + def flip_order(ord)
  10 + ord == 'ltr' ? 'rtl' : 'ltr'
  11 + end
  12 +
  13 + def calculate_position(x, y, order, rect, offset=20)
  14 + @width ||= CGRectGetWidth(rect)
  15 + if (x + offset > @width) || ((x - offset) == -20 && y != 0 && order == 'rtl')
  16 + order = flip_order(order)
  17 + y += offset
  18 + new_row = true
  19 + end
  20 + unless x == 0 && new_row
  21 + x = order == 'ltr' ? x + offset : x - offset
  22 + end
  23 + [x, y, order]
  24 + end
  25 +
  26 + def drawRect(rect)
  27 + dimensions = [CGRectGetWidth(rect), CGRectGetHeight(rect)]
  28 + Canvas.for_current_context(:size => dimensions) do |c|
  29 + c.background(Color.gray.lighten(0.4))
  30 + # load image and grab colors
  31 + img = Image.new(File.join(HERE, 'images', '1984.jpg')).saturation(1.9)
  32 +
  33 + # drawing the photo
  34 + img_x = 100
  35 + # y = height - resized img height - margin
  36 + img_y = dimensions.last - 200 - 20
  37 + c.draw(img.resize(200, 200), img_x, img_y)
  38 +
  39 + # drawing the text
  40 + c.text("Color sampling from jpg", 100, img_y - 30)
  41 +
  42 + colors = img.colors(500)
  43 + sample = Path.new.rect
  44 + x, y = -20, 0
  45 + order = 'ltr'
  46 +
  47 + # sorting colors isn't quite easy
  48 + # we are using spherical coordinates and multipass sorting
  49 + theta_sorter = lambda{|cs, window| cs.each_slice(window).map{|s| s.sort_by {|c| c.spherical_coordinates.first} }.flatten}
  50 + psi_sorter = lambda{|cs, window| cs.each_slice(window).map{|s| s.sort_by {|c| c.spherical_coordinates.last} }.flatten}
  51 + # sort on psi
  52 + first_sort = colors.sort_by{|c| c.spherical_coordinates.last }
  53 + second_sort = theta_sorter.call(first_sort, 25)
  54 + # third_sort = theta_sorter.call(first_sort, 15) #psi_sorter.call(first_sort, 35)
  55 + second_sort.reverse.each do |color|
  56 + # skip of the color is too white or too black
  57 + next if color.white? || color.brightness < 0.2
  58 + x, y, order = calculate_position(x, y, order, rect)
  59 + c.push
  60 + sample.fill(color)
  61 + c.draw(sample, x, y)
  62 + c.pop
  63 + end
  64 +
  65 + end
  66 + end
  67 +
  68 +end
  69 +
  70 +app = AppWrapper.new(400,520)
  71 +app.window.contentView = CustomView.alloc.initWithFrame(app.frame)
  72 +app.start
BIN  examples/images/1984.jpg
10 lib/color.rb
@@ -800,6 +800,16 @@ def white?
800 800 false
801 801 end
802 802 end
  803 +
  804 + # The spherical coordinates (θ, φ) of the color can be obtained from Cartesian coordinates (x, y, z)
  805 + # using the YUV values, we are getting 2 spherical coordinates
  806 + # r isn't calculated
  807 + # By using theta and psi, one can sort colors
  808 + def spherical_coordinates
  809 + theta = Math.acos(y / (Math.sqrt(u*u + v*v + y*y)))
  810 + psi = Math.atan2(v, u)
  811 + [theta, psi]
  812 + end
803 813
804 814 private
805 815

0 comments on commit cfdd655

Please sign in to comment.
Something went wrong with that request. Please try again.