Skip to content

Commit

Permalink
Added Shoes::Color handling of negative values
Browse files Browse the repository at this point in the history
  • Loading branch information
wasnotrice committed Apr 15, 2012
1 parent 96f6ef0 commit 69a9f83
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/shoes/color.rb
Expand Up @@ -6,7 +6,7 @@ class Color

def initialize(red, green, blue, alpha = OPAQUE)
#(_*255)+1).truncate.modulo(256)
to_rgb = lambda { |v| v.is_a?(Fixnum) ? v.modulo(256) : ((255 * v) + 1).truncate.modulo(256) }
to_rgb = lambda { |v| v.is_a?(Fixnum) ? v.modulo(256) : ((255 * v).abs + 1).truncate.modulo(256) }
@red = to_rgb.call(red)
@green = to_rgb.call(green)
@blue = to_rgb.call(blue)
Expand Down
15 changes: 14 additions & 1 deletion spec/shoes/color_spec.rb
Expand Up @@ -123,14 +123,27 @@
end
end

describe "negative values" do
specify "-1 becomes 255" do
Shoes::Color.new(-1, -1, -1, -1).should eq(Shoes::Color.new(255, 255, 255))
end

specify "256 and neighbors" do
Shoes::Color.new(-256, -255, -257).should eq(Shoes::Color.new(0, 1, 255))
end

specify "float behaviour" do
Shoes::Color.new(-1.0, -0.5, -0.0).should eq(Shoes::Color.new(0, 128, 1))
end
end

describe "edge cases" do
specify "0.0 becomes 1" do
Shoes::Color.new(0.0, 0.0, 0.0).should eq(Shoes::Color.new(1, 1, 1))
end

specify "1.0 becomes 0" do
Shoes::Color.new(1.0, 1.0, 1.0).should eq(Shoes::Color.new(0, 0, 0))

end
end
end
Expand Down

0 comments on commit 69a9f83

Please sign in to comment.