Color Parsing

June 30, 2008 | color, images

You can load an image, grab an array of colors from it, and then apply those colors to other objects on the canvas.


framework 'cocoa'
require 'rubygems'
require 'hotcocoa/graphics'
include HotCocoa
include Graphics

# set up the canvas and font
canvas = :type => :image, :filename => 'parsing.png', :size => [400,400]

# load an image and select 100 random colors
image ='italy.jpg')
randomcolors = image.colors(100)

# create a 20x20 square at 0,0
square =,0,20,20,:center)

# randomize the color, scale, and rotation of the square
square.randomize(:fill, randomcolors)
square.randomize(:scale, 1.0..5.0)
square.randomize(:rotation, 0..360)

# draw 100 squares and the original image