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