Gradients

June 30, 2008 | color

Radial or linear gradients may be drawn by first specifying their main constituent colors, then applying to the canvas with a start and endpoint. (warning: crashes in MacRuby 0.4)

#!/usr/local/bin/macruby

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

# FIXME: SEGFAULT!!

# set up the canvas and font
canvas = Canvas.new :type => :image, :filename => 'gradients.png', :size => [400,400]
canvas.background(Color.black)

# create a new gradient
gradient = Gradient.new

# set the component colors of the gradient
gradient.set(Color.black,Color.blue,Color.red.darken,Color.orange)

# draw a linear gradient starting at 50,50 and ending at 200,200
canvas.gradient(gradient, 50,50,200,200)

# do not extend gradient beyond its start/endpoints
gradient.pre(false)
gradient.post(false)

# draw a radial gradient starting at 200,200 with radius 100
canvas.radial(gradient, 200,200,100)

# save the canvas
canvas.save