summaryrefslogtreecommitdiffhomepage
path: root/lib
diff options
context:
space:
mode:
authorTom Black <[email protected]>2015-11-13 15:49:35 -0500
committerTom Black <[email protected]>2015-11-13 15:49:35 -0500
commit4b4ceac1d95773f8c054cd04e287af9f65c29dec (patch)
treeddd1cbc7214db1b0aa755c8e23b68b1ce10f710d /lib
parent592dd9976211f8b00da0f4992ddbe27d7d2b300e (diff)
downloadruby2d-4b4ceac1d95773f8c054cd04e287af9f65c29dec.tar.gz
ruby2d-4b4ceac1d95773f8c054cd04e287af9f65c29dec.zip
Extract string colors to class hash
Also validate them
Diffstat (limited to 'lib')
-rw-r--r--lib/ruby2d/color.rb80
1 files changed, 33 insertions, 47 deletions
diff --git a/lib/ruby2d/color.rb b/lib/ruby2d/color.rb
index df28b52..c7bb72d 100644
--- a/lib/ruby2d/color.rb
+++ b/lib/ruby2d/color.rb
@@ -5,62 +5,48 @@ module Ruby2D
attr_reader :r, :g, :b, :a
+ @@colors = {
+ 'black' => [ 0, 0, 0, 255],
+ 'gray' => [170, 170, 170, 255],
+ 'silver' => [221, 221, 221, 255],
+ 'white' => [255, 255, 255, 255],
+ 'navy' => [ 0, 31, 63, 255],
+ 'blue' => [ 0, 116, 217, 255],
+ 'aqua' => [127, 219, 255, 255],
+ 'teal' => [ 57, 204, 204, 255],
+ 'olive' => [ 61, 153, 112, 255],
+ 'green' => [ 46, 204, 64, 255],
+ 'lime' => [ 1, 255, 112, 255],
+ 'yellow' => [255, 220, 0, 255],
+ 'orange' => [255, 133, 27, 255],
+ 'red' => [255, 65, 54, 255],
+ 'maroon' => [133, 20, 75, 255],
+ 'fuchsia' => [240, 18, 190, 255],
+ 'purple' => [177, 13, 201, 255],
+ 'brown' => [102, 51, 0, 255],
+ 'random' => []
+ }
+
def initialize(c)
if !self.class.is_valid? c
raise Error, "`#{c}` is not a valid color"
- end
-
- case c
- when 'black'
- @r, @g, @b, @a = to_f([0, 0, 0, 255])
- when 'gray'
- @r, @g, @b, @a = to_f([170, 170, 170, 255])
- when 'silver'
- @r, @g, @b, @a = to_f([221, 221, 221, 255])
- when 'white'
- @r, @g, @b, @a = to_f([255, 255, 255, 255])
- when 'navy'
- @r, @g, @b, @a = to_f([0, 31, 63, 255])
- when 'blue'
- @r, @g, @b, @a = to_f([0, 116, 217, 255])
- when 'aqua'
- @r, @g, @b, @a = to_f([127, 219, 255, 255])
- when 'teal'
- @r, @g, @b, @a = to_f([57, 204, 204, 255])
- when 'olive'
- @r, @g, @b, @a = to_f([61, 153, 112, 255])
- when 'green'
- @r, @g, @b, @a = to_f([46, 204, 64, 255])
- when 'lime'
- @r, @g, @b, @a = to_f([1, 255, 112, 255])
- when 'yellow'
- @r, @g, @b, @a = to_f([255, 220, 0, 255])
- when 'orange'
- @r, @g, @b, @a = to_f([255, 133, 27, 255])
- when 'red'
- @r, @g, @b, @a = to_f([255, 65, 54, 255])
- when 'maroon'
- @r, @g, @b, @a = to_f([133, 20, 75, 255])
- when 'fuchsia'
- @r, @g, @b, @a = to_f([240, 18, 190, 255])
- when 'purple'
- @r, @g, @b, @a = to_f([177, 13, 201, 255])
- when 'brown'
- @r, @g, @b, @a = to_f([102, 51, 0, 255])
- when 'random'
- @r, @g, @b, @a = rand(0..1.0), rand(0..1.0), rand(0..1.0), 1.0
- when Array
- @r, @g, @b, @a = to_f([c[0], c[1], c[2], c[3]])
else
- raise Error, "`#{c}` is not a valid color"
+ case c
+ when String
+ if c == 'random'
+ @r, @g, @b, @a = rand(0..1.0), rand(0..1.0), rand(0..1.0), 1.0
+ else
+ @r, @g, @b, @a = to_f(@@colors[c])
+ end
+ when Array
+ @r, @g, @b, @a = to_f([c[0], c[1], c[2], c[3]])
+ end
end
end
# Color must be String, like 'red', or Array, like [1.0, 0, 0, 1.0]
def self.is_valid?(c)
- # TODO: Check if valid color string
- # (c.class == String && c.has_key?(c)) ||
- (c.class == String) ||
+ (c.class == String && @@colors.key?(c)) ||
(c.class == Array && c.length == 4 &&
c.all? { |el| el.is_a? Numeric } &&
c.all? { |el| el.class == Fixnum && (0..255).include?(el) ||