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
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
- .from(input) ⇒ Object
-
.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.
- #opacity ⇒ Object
- #opacity=(opacity) ⇒ Object
Constructor Details
#initialize(c) ⇒ Color
Returns a new instance of Color.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/ruby2d/color.rb', line 53 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.
28 29 30 |
# File 'lib/ruby2d/color.rb', line 28 def a @a end |
#b ⇒ Object (readonly)
Returns the value of attribute b.
28 29 30 |
# File 'lib/ruby2d/color.rb', line 28 def b @b end |
#g ⇒ Object (readonly)
Returns the value of attribute g.
28 29 30 |
# File 'lib/ruby2d/color.rb', line 28 def g @g end |
#r ⇒ Object (readonly)
Returns the value of attribute r.
28 29 30 |
# File 'lib/ruby2d/color.rb', line 28 def r @r end |
Class Method Details
.from(input) ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'lib/ruby2d/color.rb', line 91 def self.from(input) # If a valid array of colors, return a Color::Set with those colors # Else return single color if input.is_a? Array and input.all? { |el| Color.is_valid? el } Color::Set.new(input) else Color.new(input) end end |
.is_hex?(s) ⇒ Boolean
Check if string is a proper hex value
73 74 75 76 77 |
# File 'lib/ruby2d/color.rb', line 73 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
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/ruby2d/color.rb', line 80 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 |
Instance Method Details
#opacity ⇒ Object
101 |
# File 'lib/ruby2d/color.rb', line 101 def opacity; @a end |
#opacity=(opacity) ⇒ Object
103 104 105 |
# File 'lib/ruby2d/color.rb', line 103 def opacity=(opacity) @a = opacity end |