diff options
| author | Tom Black <[email protected]> | 2016-11-27 23:21:50 -0500 |
|---|---|---|
| committer | Tom Black <[email protected]> | 2016-11-27 23:21:50 -0500 |
| commit | 92fcfa846909bec9a939a3edd729d15fdce4d0ff (patch) | |
| tree | 5903604b2f887f8cdedbfe4aa0c6b07500112623 /lib | |
| parent | 3e019cb28b2a1ea1e369b490c54adeba0011db6a (diff) | |
| download | ruby2d-92fcfa846909bec9a939a3edd729d15fdce4d0ff.tar.gz ruby2d-92fcfa846909bec9a939a3edd729d15fdce4d0ff.zip | |
Opal fixes and other cleanup to `Color`
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/ruby2d/color.rb | 43 |
1 files changed, 20 insertions, 23 deletions
diff --git a/lib/ruby2d/color.rb b/lib/ruby2d/color.rb index c4f1925..bd3f436 100644 --- a/lib/ruby2d/color.rb +++ b/lib/ruby2d/color.rb @@ -34,29 +34,29 @@ module Ruby2D 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 + @r, @g, @b, @a = rand, rand, rand, 1.0 elsif self.class.is_hex(c) @r, @g, @b, @a = hex_to_f(c) 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]]) + @r, @g, @b, @a = [c[0], c[1], c[2], c[3]] end end end - - # test whether input string is Hex color + + # Check if string is a proper hex value def self.is_hex(a) - # result = !(/^#[0-9A-F]{6}$/i.match(a).nil?) - # return result + # MRuby doesn't support regex, otherwise we'd do: + # !(/^#[0-9A-F]{6}$/i.match(a).nil?) if (a.include? "#") && (a.length == 7) true else false end end - + # Color must be String, like 'red', or Array, like [1.0, 0, 0, 1.0] def self.is_valid?(c) (c.class == String && @@colors.key?(c)) || @@ -69,31 +69,28 @@ module Ruby2D private + # TODO: Only `Number` supported in JS # Convert from Fixnum (0..255) to Float (0.0..1.0) def to_f(a) b = [] a.each do |n| - if n.class == Fixnum - b.push(n / 255.0) - else - b.push(n) - end + b.push(n / 255.0) end return b end - - # convert from "#FFF000" to Float (0.0..1.0) + + # Convert from "#FFF000" to Float (0.0..1.0) def hex_to_f(a) - c=[] - b=a.delete("#") - n=(b.length) - #n1=n/3 - j=0 - for i in (0..n-1).step(n/3) - c[j]=Integer("0x".concat(b[i,n/3])) - j=j+1 + c = [] + b = a.delete('#') + n = (b.length) + + j = 0 + for i in (0..n-1).step(n / 3) + c[j] = Integer("0x".concat(b[i, n / 3])) + j = j + 1 end - c[3] = 255 #set @a to 255 + c[3] = 255 # set `a` to 255 f = to_f(c) return f end |
