Class: Ruby2D::Color
- Inherits:
-
Object
- Object
- Ruby2D::Color
- Defined in:
- lib/ruby2d/color.rb
Defined Under Namespace
Classes: Set
Constant Summary collapse
- @@colors =
Based on clrs.cc
{ 'navy' => '#001F3F', 'blue' => '#0074D9', 'aqua' => '#7FDBFF', 'teal' => '#39CCCC', 'olive' => '#3D9970', 'green' => '#2ECC40', 'lime' => '#01FF70', 'yellow' => '#FFDC00', 'orange' => '#FF851B', 'red' => '#FF4136', 'brown' => '#663300', 'fuchsia' => '#F012BE', 'purple' => '#B10DC9', 'maroon' => '#85144B', 'white' => '#FFFFFF', 'silver' => '#DDDDDD', 'gray' => '#AAAAAA', 'black' => '#111111', 'random' => '' }
Instance Attribute Summary collapse
-
#a ⇒ Object
Returns the value of attribute a.
-
#b ⇒ Object
Returns the value of attribute b.
-
#g ⇒ Object
Returns the value of attribute g.
-
#r ⇒ Object
Returns the value of attribute r.
Class Method Summary collapse
-
.is_hex?(s) ⇒ Boolean
Check if string is a proper hex value.
-
.is_valid?(c) ⇒ Boolean
Check if the color is valid.
-
.set(colors) ⇒ Object
Return a color set if an array of valid colors.
Instance Method Summary collapse
-
#initialize(c) ⇒ Color
constructor
A new instance of Color.
-
#opacity ⇒ Object
Convenience methods to alias ‘opacity` to `@a`.
- #opacity=(opacity) ⇒ Object
Constructor Details
#initialize(c) ⇒ Color
Returns a new instance of Color.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/ruby2d/color.rb', line 54 def initialize(c) if !self.class.is_valid? c raise Error, "`#{c}` is not a valid color" else case c when String if c == 'random' @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 = hex_to_f(@@colors[c]) end when Array @r, @g, @b, @a = [c[0], c[1], c[2], c[3]] when Color @r, @g, @b, @a = [c.r, c.g, c.b, c.a] end end end |
Instance Attribute Details
#a ⇒ Object
Returns the value of attribute a.
29 30 31 |
# File 'lib/ruby2d/color.rb', line 29 def a @a end |
#b ⇒ Object
Returns the value of attribute b.
29 30 31 |
# File 'lib/ruby2d/color.rb', line 29 def b @b end |
#g ⇒ Object
Returns the value of attribute g.
29 30 31 |
# File 'lib/ruby2d/color.rb', line 29 def g @g end |
#r ⇒ Object
Returns the value of attribute r.
29 30 31 |
# File 'lib/ruby2d/color.rb', line 29 def r @r end |
Class Method Details
.is_hex?(s) ⇒ Boolean
Check if string is a proper hex value
87 88 89 90 91 |
# File 'lib/ruby2d/color.rb', line 87 def self.is_hex?(s) # MRuby doesn't support regex, otherwise we'd do: # !(/^#[0-9A-F]{6}$/i.match(a).nil?) s.class == String && s[0] == '#' && s.length == 7 end |
.is_valid?(c) ⇒ Boolean
Check if the color is valid
94 95 96 97 98 99 100 101 102 |
# File 'lib/ruby2d/color.rb', line 94 def self.is_valid?(c) c.is_a?(Color) || # color object @@colors.key?(c) || # keyword self.is_hex?(c) || # hexadecimal value # Array of Floats from 0.0..1.0 c.class == Array && c.length == 4 && c.all? { |el| el.is_a?(Numeric) } end |
.set(colors) ⇒ Object
Return a color set if an array of valid colors
76 77 78 79 80 81 82 83 84 |
# File 'lib/ruby2d/color.rb', line 76 def self.set(colors) # If a valid array of colors, return a `Color::Set` with those colors if colors.is_a?(Array) && colors.all? { |el| Color.is_valid? el } Color::Set.new(colors) # Otherwise, return single color else Color.new(colors) end end |
Instance Method Details
#opacity ⇒ Object
Convenience methods to alias ‘opacity` to `@a`
105 |
# File 'lib/ruby2d/color.rb', line 105 def opacity; @a end |
#opacity=(opacity) ⇒ Object
106 |
# File 'lib/ruby2d/color.rb', line 106 def opacity=(opacity); @a = opacity end |