Class: Ruby2D::Color

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby2d/color.rb

Constant Summary collapse

@@colors =
{
  'black'   => [  0,   0,   0, 255],
  'gray'    => [170, 170, 170, 255],
  'silver'  => [221, 221, 221, 255],
  'white'   => [255, 255, 255, 255],
  'navy'    => [  0,  31,  63, 255],
  'blue'    => [  0, 116, 217, 255],
  'aqua'    => [127, 219, 255, 255],
  'teal'    => [ 57, 204, 204, 255],
  'olive'   => [ 61, 153, 112, 255],
  'green'   => [ 46, 204,  64, 255],
  'lime'    => [  1, 255, 112, 255],
  'yellow'  => [255, 220,   0, 255],
  'orange'  => [255, 133,  27, 255],
  'red'     => [255,  65,  54, 255],
  'maroon'  => [133,  20,  75, 255],
  'fuchsia' => [240,  18, 190, 255],
  'purple'  => [177,  13, 201, 255],
  'brown'   => [102,  51,   0, 255],
  'random'  => []
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(c) ⇒ Color

Returns a new instance of Color.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ruby2d/color.rb', line 30

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(0..1.0), rand(0..1.0), rand(0..1.0), 1.0
      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]])
    end
  end
end

Instance Attribute Details

#aObject (readonly)

Returns the value of attribute a.



6
7
8
# File 'lib/ruby2d/color.rb', line 6

def a
  @a
end

#bObject (readonly)

Returns the value of attribute b.



6
7
8
# File 'lib/ruby2d/color.rb', line 6

def b
  @b
end

#gObject (readonly)

Returns the value of attribute g.



6
7
8
# File 'lib/ruby2d/color.rb', line 6

def g
  @g
end

#rObject (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_valid?(c) ⇒ Boolean

Color must be String, like ‘red’, or Array, like [1.0, 0, 0, 1.0]

Returns:

  • (Boolean)


48
49
50
51
52
53
54
# File 'lib/ruby2d/color.rb', line 48

def self.is_valid?(c)
  (c.class == String && @@colors.key?(c)) ||
  (c.class == Array && c.length == 4 &&
   c.all? { |el| el.is_a? Numeric } &&
   c.all? { |el| el.class == Fixnum && (0..255).include?(el) ||
                 el.class == Float  && (0.0..1.0).include?(el) })
end