Class: Ruby2D::Color

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#aObject (readonly)

Returns the value of attribute a.



28
29
30
# File 'lib/ruby2d/color.rb', line 28

def a
  @a
end

#bObject (readonly)

Returns the value of attribute b.



28
29
30
# File 'lib/ruby2d/color.rb', line 28

def b
  @b
end

#gObject (readonly)

Returns the value of attribute g.



28
29
30
# File 'lib/ruby2d/color.rb', line 28

def g
  @g
end

#rObject (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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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)
  }
end

Instance Method Details

#opacityObject



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