summaryrefslogtreecommitdiffhomepage
path: root/lib
diff options
context:
space:
mode:
authorTom Black <[email protected]>2017-02-17 00:04:15 -0500
committerTom Black <[email protected]>2017-02-17 00:04:15 -0500
commit46f566d161b07c0e319cae628a564b034b3ecf0a (patch)
treea03294f7cc6f771e2e7afa938a16a60f549aa8f2 /lib
parenta583129cd8aab9ee275bc54254a0bd5fc7fe09a7 (diff)
downloadruby2d-46f566d161b07c0e319cae628a564b034b3ecf0a.tar.gz
ruby2d-46f566d161b07c0e319cae628a564b034b3ecf0a.zip
Improve color validation on shapes
Diffstat (limited to 'lib')
-rw-r--r--lib/ruby2d/quad.rb6
-rw-r--r--lib/ruby2d/triangle.rb21
2 files changed, 22 insertions, 5 deletions
diff --git a/lib/ruby2d/quad.rb b/lib/ruby2d/quad.rb
index eca123d..f17aa5c 100644
--- a/lib/ruby2d/quad.rb
+++ b/lib/ruby2d/quad.rb
@@ -47,16 +47,22 @@ module Ruby2D
@c3 = Color.new(c)
@c4 = Color.new(c)
+ elsif c.class == Array && c.length < 4
+ raise Error, "Quads require 4 colors, one for each vertex. Only " <<
+ "#{c.length} were given."
+
# If a valid array of colors, assign them to each vertex, respectively
elsif c.all? { |el| Color.is_valid? el }
@c1 = Color.new(c[0])
@c2 = Color.new(c[1])
@c3 = Color.new(c[2])
@c4 = Color.new(c[3])
+
else
raise Error, "Not a valid color for #{self.class}"
end
end
+
end
end
diff --git a/lib/ruby2d/triangle.rb b/lib/ruby2d/triangle.rb
index ad13688..ac7d67a 100644
--- a/lib/ruby2d/triangle.rb
+++ b/lib/ruby2d/triangle.rb
@@ -35,16 +35,27 @@ module Ruby2D
private
def update_color(c)
- # If a 2D array
- if c.class == Array && c.all? { |el| el.class == Array }
+
+ # If a valid color, use it for each vertex
+ if Color.is_valid? c
+ @c1 = Color.new(c)
+ @c2 = Color.new(c)
+ @c3 = Color.new(c)
+
+ elsif c.class == Array && c.length < 3
+ raise Error, "Triangles require 3 colors, one for each vertex. Only " <<
+ "#{c.length} were given."
+
+ # If a valid array of colors, assign them to each vertex, respectively
+ elsif c.all? { |el| Color.is_valid? el }
@c1 = Color.new(c[0])
@c2 = Color.new(c[1])
@c3 = Color.new(c[2])
+
else
- @c1 = Color.new(c)
- @c2 = Color.new(c)
- @c3 = Color.new(c)
+ raise Error, "Not a valid color for #{self.class}"
end
+
end
end