Color Rules

June 29, 2008 | color

Various sets of colors can be generated from a single starting color by using the various classical color harmony schemes.  For more information, see Color Theory on Wikipedia.

#!/usr/local/bin/macruby

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

# set up the canvas and font
canvas = Canvas.new :type => :image, :filename => 'harmony.png', :size => [400,400]
canvas.background(Color.white)
canvas.font('Skia')
canvas.fontsize(12)

# create a new color with the specified red, green, blue, and alpha values
blue = Color.new(0.0,0.4,0.6,1.0)

# draw the original color
canvas.translate(135,350)
canvas.text("original color",-115,10)
canvas.fill(blue)
canvas.rect(0,0,255,30)
canvas.fill(Color.black)

# create a rectangle to use as a color swatch
swatch = Path.new.rect(0,0,15,30)
swatch.increment(:x, 15)

# draw harmony schemes derived from this color
for scheme in [
  :complementary,:split_complementary,:left_complement,
  :right_complement,:monochrome,:triad,:tetrad,:compound,
  :flipped_compound
  ] do
  canvas.translate(0,-38)
  canvas.text(scheme,-115,10)
  swatch.increment(:fill, blue.send(scheme).sort)
  canvas.draw(swatch,0,0,17)
end

# save the canvas
canvas.save