From 8c1f99bf3f8a1989b4a8071cf898a5e23afa61aa Mon Sep 17 00:00:00 2001 From: Wes Devauld Date: Tue, 13 Jan 2009 21:19:58 -0700 Subject: [PATCH] Add Gradients --- lib/colour.rb | 29 +++++++++++++++++++++++++++++ spec/colour_spec.rb | 4 ++++ 2 files changed, 33 insertions(+) diff --git a/lib/colour.rb b/lib/colour.rb index 9971411..c919b6d 100644 --- a/lib/colour.rb +++ b/lib/colour.rb @@ -82,6 +82,35 @@ def triadic(&block) end end + + def gradient_to(colour, steps=10) + c = self.class.name.downcase + origin = self.to_rgb + destination = colour.to_rgb + gradient = [] + + #TODO: *_range isn't really being used + red_range = destination.r - origin.r + red_increment = red_range / steps + green_range = destination.g - origin.g + green_increment = green_range / steps + blue_range = destination.b - origin.b + blue_increment = blue_range / steps + + steps.times do |i| + intermediate = RGB.new( + origin.r + red_increment * i, + origin.g + green_increment * i, + origin.b + blue_increment * i + ) + gradient << intermediate.send("to_" + c) + if block_given? then + yield intermediate.send("to_" + c) + end + end + gradient + end + #Bones specific stuff # :stopdoc: diff --git a/spec/colour_spec.rb b/spec/colour_spec.rb index 87b374a..821afde 100644 --- a/spec/colour_spec.rb +++ b/spec/colour_spec.rb @@ -23,6 +23,10 @@ @green.web_safe.should eql("#0F0") end + it "should gradient through the RGB space towards another colour" do + gradient = @green.gradient_to(RGB.new(1,0,1), steps=3) + end + it "should provide a complementary colour" do end