/test/color_spec.rb
http://jgosu.googlecode.com/ · Ruby · 75 lines · 65 code · 10 blank · 0 comment · 12 complexity · f9f411c15803e544c26fe1930d5ff2d8 MD5 · raw file
- $: << File.expand_path(File.join(File.dirname(__FILE__), '../lib'))
- require 'rubygems'
- require 'gosu'
- include Gosu
- describe Color do
- it "should accept 1, 3, or 4 parameters in the constructor" do
- lambda {Color.new 0}.should_not raise_error
- lambda {Color.new 0, 0}.should raise_error(ArgumentError)
- lambda {Color.new 0, 0, 0}.should_not raise_error
- lambda {Color.new 0, 0, 0, 0}.should_not raise_error
- end
- it "should parse 0xaarrggbb in the 1-argument constructor" do
- color = Color.new(0xff223344)
- color.alpha.should == 0xff
- color.red.should == 0x22
- color.green.should == 0x33
- color.blue.should == 0x44
- end
- it "should have the correct HSV values when constructed from RGB values" do
- color = Color.new(95, 127, 63)
- color.hue.should be_close(90.0, 0.005)
- color.saturation.should be_close(0.5, 0.005)
- color.value.should be_close(0.5, 0.005)
- end
- it "should have working getters and setters" do
- color = Color.new(0)
- color.red = 11
- color.red.should == 11
- color.green = 22
- color.green.should == 22
- color.blue = 33
- color.blue.should == 33
- color.alpha = 44
- color.alpha.should == 44
- color.hue = 180
- color.hue.should be_close(180.0, 0.005)
- color.red.should be_close(10, 2)
- color.green.should == 33
- color.blue.should == 33
- color.saturation = 0.5
- color.saturation.should be_close(0.5, 0.02)
- color.red.should be_close(16, 2)
- color.value = 0.5
- color.value.should be_close(0.5, 0.005)
- color.red.should be_close(61, 6)
- color.green.should be_close(127, 2)
- color.blue.should be_close(127, 2)
- end
- it "should have static from_hsv and from_ahsv methods that return a color from HSV values" do
- lambda {Color.from_hsv 0, 0, 0, 0}.should raise_error(ArgumentError)
- lambda {Color.from_hsv 0, 0}.should raise_error(ArgumentError)
- lambda {Color.from_ahsv 0, 0, 0, 0, 0}.should raise_error(ArgumentError)
- lambda {Color.from_ahsv 0, 0, 0}.should raise_error(ArgumentError)
- c = Color.from_hsv 0.5, 0.5, 0.5
- c.alpha.should == 255
- c.red.should be_close(127, 2)
- c.green.should be_close(64, 2)
- c.blue.should be_close(63, 2)
- c = Color.from_ahsv 100, 0.5, 0.5, 0.5
- c.alpha.should == 100
- c.red.should be_close(127, 2)
- c.green.should be_close(64, 2)
- c.blue.should be_close(63, 2)
- end
- end