PageRenderTime 46ms CodeModel.GetById 10ms app.highlight 32ms RepoModel.GetById 1ms app.codeStats 0ms

/test/color_spec.rb

http://jgosu.googlecode.com/
Ruby | 75 lines | 65 code | 10 blank | 0 comment | 12 complexity | f9f411c15803e544c26fe1930d5ff2d8 MD5 | raw file
 1$: << File.expand_path(File.join(File.dirname(__FILE__), '../lib'))
 2require 'rubygems'
 3require 'gosu'
 4
 5include Gosu
 6
 7describe Color do
 8  it "should accept 1, 3, or 4 parameters in the constructor" do
 9    lambda {Color.new 0}.should_not raise_error
10    lambda {Color.new 0, 0}.should raise_error(ArgumentError)
11    lambda {Color.new 0, 0, 0}.should_not raise_error
12    lambda {Color.new 0, 0, 0, 0}.should_not raise_error
13  end
14
15  it "should parse 0xaarrggbb in the 1-argument constructor" do
16    color = Color.new(0xff223344)
17    color.alpha.should == 0xff
18    color.red.should == 0x22
19    color.green.should == 0x33
20    color.blue.should == 0x44
21  end
22
23  it "should have the correct HSV values when constructed from RGB values" do
24    color = Color.new(95, 127, 63)
25    color.hue.should be_close(90.0, 0.005)
26    color.saturation.should be_close(0.5, 0.005)
27    color.value.should be_close(0.5, 0.005)
28  end
29
30  it "should have working getters and setters" do
31    color = Color.new(0)
32    color.red = 11
33    color.red.should == 11
34    color.green = 22
35    color.green.should == 22
36    color.blue = 33
37    color.blue.should == 33
38    color.alpha = 44
39    color.alpha.should == 44
40
41    color.hue = 180
42    color.hue.should be_close(180.0, 0.005)
43    color.red.should be_close(10, 2)
44    color.green.should == 33
45    color.blue.should == 33
46
47    color.saturation = 0.5
48    color.saturation.should be_close(0.5, 0.02)
49    color.red.should be_close(16, 2)
50
51    color.value = 0.5
52    color.value.should be_close(0.5, 0.005)
53    color.red.should be_close(61, 6)
54    color.green.should be_close(127, 2)
55    color.blue.should be_close(127, 2)
56  end
57
58  it "should have static from_hsv and from_ahsv methods that return a color from HSV values" do
59    lambda {Color.from_hsv 0, 0, 0, 0}.should raise_error(ArgumentError)
60    lambda {Color.from_hsv 0, 0}.should raise_error(ArgumentError)
61    lambda {Color.from_ahsv 0, 0, 0, 0, 0}.should raise_error(ArgumentError)
62    lambda {Color.from_ahsv 0, 0, 0}.should raise_error(ArgumentError)
63    c = Color.from_hsv 0.5, 0.5, 0.5
64    c.alpha.should == 255
65    c.red.should be_close(127, 2)
66    c.green.should be_close(64, 2)
67    c.blue.should be_close(63, 2)
68
69    c = Color.from_ahsv 100, 0.5, 0.5, 0.5
70    c.alpha.should == 100
71    c.red.should be_close(127, 2)
72    c.green.should be_close(64, 2)
73    c.blue.should be_close(63, 2)
74  end
75end