Class: Ruby2D::Color
- Inherits:
-
Object
- Object
- Ruby2D::Color
- Defined in:
- lib/ruby2d/color.rb
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
readonly
Returns the value of attribute a.
-
#b ⇒ Object
readonly
Returns the value of attribute b.
-
#g ⇒ Object
readonly
Returns the value of attribute g.
-
#r ⇒ Object
readonly
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.
Instance Method Summary collapse
-
#initialize(c) ⇒ Color
constructor
A new instance of Color.
Constructor Details
#initialize(c) ⇒ Color
Returns a new instance of Color.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/ruby2d/color.rb', line 31 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]] end end end |
Instance Attribute Details
#a ⇒ Object (readonly)
Returns the value of attribute a.
6 7 8 |
# File 'lib/ruby2d/color.rb', line 6 def a @a end |
#b ⇒ Object (readonly)
Returns the value of attribute b.
6 7 8 |
# File 'lib/ruby2d/color.rb', line 6 def b @b end |
#g ⇒ Object (readonly)
Returns the value of attribute g.
6 7 8 |
# File 'lib/ruby2d/color.rb', line 6 def g @g end |
#r ⇒ Object (readonly)
Returns the value of attribute r.
6 7 8 |
# File 'lib/ruby2d/color.rb', line 6 def r @r end |
Class Method Details
.is_hex?(s) ⇒ Boolean
Check if string is a proper hex value
51 52 53 54 55 |
# File 'lib/ruby2d/color.rb', line 51 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
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/ruby2d/color.rb', line 58 def self.is_valid?(c) @@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) && (0.0..1.0).include?(el) } end |