summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTom Black <[email protected]>2018-09-28 13:22:23 -0700
committerTom Black <[email protected]>2018-09-28 13:22:23 -0700
commit9b51a3b4718b082301ff70b706ac53bec8668ac9 (patch)
tree9a27a9933548f7c9446b756f42fc1928a8cf2a63
parentd0a2823398fb0b26b75a168ec13816c8873cceb1 (diff)
downloadruby2d-9b51a3b4718b082301ff70b706ac53bec8668ac9.tar.gz
ruby2d-9b51a3b4718b082301ff70b706ac53bec8668ac9.zip
Color enhancements
New shortcuts for setting color values, like `.color.r/g/b/a` and simply `.r/g/b/a`; allow the British English spelling "colour"
-rw-r--r--lib/ruby2d/color.rb5
-rw-r--r--lib/ruby2d/renderable.rb25
-rw-r--r--test/color_spec.rb8
-rw-r--r--test/renderable_spec.rb66
4 files changed, 93 insertions, 11 deletions
diff --git a/lib/ruby2d/color.rb b/lib/ruby2d/color.rb
index 26d176f..296f228 100644
--- a/lib/ruby2d/color.rb
+++ b/lib/ruby2d/color.rb
@@ -26,7 +26,7 @@ module Ruby2D
end
end
- attr_reader :r, :g, :b, :a
+ attr_accessor :r, :g, :b, :a
# Based on clrs.cc
@@colors = {
@@ -128,4 +128,7 @@ module Ruby2D
end
end
+
+ # Allow British English spelling of color
+ Colour = Color
end
diff --git a/lib/ruby2d/renderable.rb b/lib/ruby2d/renderable.rb
index 6a6761d..18c7a78 100644
--- a/lib/ruby2d/renderable.rb
+++ b/lib/ruby2d/renderable.rb
@@ -23,14 +23,23 @@ module Ruby2D
end
end
- def opacity
- self.color.opacity
- end
-
- def opacity=(val)
- self.color.opacity = val
- end
-
+ # Allow shortcuts for setting color values
+ def r; self.color.r end
+ def g; self.color.g end
+ def b; self.color.b end
+ def a; self.color.a end
+ def r=(c); self.color.r = c end
+ def g=(c); self.color.g = c end
+ def b=(c); self.color.b = c end
+ def a=(c); self.color.a = c end
+ def opacity; self.color.opacity end
+ def opacity=(val); self.color.opacity = val end
+
+ # Allow British English spelling of color
+ def colour; self.color end
+ def colour=(c); self.color = c end
+
+ # Add a contains method stub
def contains?(x, y)
raise Error, "\`#contains?\` not implemented for this class yet"
end
diff --git a/test/color_spec.rb b/test/color_spec.rb
index 4ed21aa..4f0ec35 100644
--- a/test/color_spec.rb
+++ b/test/color_spec.rb
@@ -32,9 +32,13 @@ RSpec.describe Ruby2D::Color do
s1.opacity = 0.5
s2 = Square.new(color: ['red', 'green', 'blue', 'yellow'])
s2.opacity = 0.7
- expect(s1.opacity).to eq 0.5
- expect(s2.opacity).to eq 0.7
+ expect(s1.opacity).to eq(0.5)
+ expect(s2.opacity).to eq(0.7)
end
end
+ it "allows British English spelling of color" do
+ expect(Ruby2D::Colour).to eq(Ruby2D::Color)
+ end
+
end
diff --git a/test/renderable_spec.rb b/test/renderable_spec.rb
new file mode 100644
index 0000000..56ee521
--- /dev/null
+++ b/test/renderable_spec.rb
@@ -0,0 +1,66 @@
+require 'ruby2d'
+
+RSpec.describe Ruby2D::Renderable do
+
+ it "allows colors to be set on objects" do
+ quad = Quad.new
+
+ quad.color = 'red'
+ expect(quad.color.r).to eq(1)
+
+ quad.color = [0.9, 0.8, 0.7, 0.6]
+ expect(quad.color.r).to eq(0.9)
+ expect(quad.color.g).to eq(0.8)
+ expect(quad.color.b).to eq(0.7)
+ expect(quad.color.a).to eq(0.6)
+
+ quad.color.r = 0.1
+ quad.color.g = 0.2
+ quad.color.b = 0.3
+ quad.color.a = 0.4
+ expect(quad.color.r).to eq(0.1)
+ expect(quad.color.g).to eq(0.2)
+ expect(quad.color.b).to eq(0.3)
+ expect(quad.color.a).to eq(0.4)
+
+ quad.r = 0.5
+ quad.g = 0.6
+ quad.b = 0.7
+ quad.a = 0.8
+ expect(quad.r).to eq(0.5)
+ expect(quad.g).to eq(0.6)
+ expect(quad.b).to eq(0.7)
+ expect(quad.a).to eq(0.8)
+ end
+
+ it "allows British English spelling of color (colour)" do
+ quad = Quad.new
+
+ quad.colour = 'blue'
+ expect(quad.color.r).to eq(0)
+
+ quad.colour = [0.1, 0.2, 0.3, 0.4]
+ expect(quad.color.r).to eq(0.1)
+ expect(quad.color.g).to eq(0.2)
+ expect(quad.color.b).to eq(0.3)
+ expect(quad.color.a).to eq(0.4)
+
+ quad.colour.r = 0.9
+ quad.colour.g = 0.8
+ quad.colour.b = 0.7
+ quad.colour.a = 0.6
+ expect(quad.colour.r).to eq(0.9)
+ expect(quad.colour.g).to eq(0.8)
+ expect(quad.colour.b).to eq(0.7)
+ expect(quad.colour.a).to eq(0.6)
+ expect(quad.color.r).to eq(0.9)
+ expect(quad.color.g).to eq(0.8)
+ expect(quad.color.b).to eq(0.7)
+ expect(quad.color.a).to eq(0.6)
+ expect(quad.r).to eq(0.9)
+ expect(quad.g).to eq(0.8)
+ expect(quad.b).to eq(0.7)
+ expect(quad.a).to eq(0.6)
+ end
+
+end