Class: Ruby2D::Color

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

Overview

Colors are represented by the Color class. Colors can be created from keywords (based on css), a hexadecimal value or an array containing a collection of red, green, blue, and alpha (transparency) values expressed as a Float from 0.0 to 1.0.

Defined Under Namespace

Classes: Set

Constant Summary collapse

NAMED_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' => ''
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(color) ⇒ Color

Returns a new instance of Color.

Raises:



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ruby2d/color.rb', line 76

def initialize(color)
  raise Error, "`#{color}` is not a valid color" unless self.class.valid? color

  case color
  when String
    init_from_string color
  when Array
    @r = color[0]
    @g = color[1]
    @b = color[2]
    @a = color[3]
  when Color
    @r = color.r
    @g = color.g
    @b = color.b
    @a = color.a
  end
end

Instance Attribute Details

#aObject

Returns the value of attribute a.



51
52
53
# File 'lib/ruby2d/color.rb', line 51

def a
  @a
end

#bObject

Returns the value of attribute b.



51
52
53
# File 'lib/ruby2d/color.rb', line 51

def b
  @b
end

#gObject

Returns the value of attribute g.



51
52
53
# File 'lib/ruby2d/color.rb', line 51

def g
  @g
end

#rObject

Returns the value of attribute r.



51
52
53
# File 'lib/ruby2d/color.rb', line 51

def r
  @r
end

Class Method Details

.hex?(color_string) ⇒ Boolean

Check if string is a proper hex value

Returns:

  • (Boolean)


108
109
110
111
112
113
114
# File 'lib/ruby2d/color.rb', line 108

def hex?(color_string)
  # MRuby doesn't support regex, otherwise we'd do:
  #   !(/^#[0-9A-F]{6}$/i.match(a).nil?)
  color_string.instance_of?(String) &&
    color_string[0] == '#' &&
    color_string.length == 7
end

.set(colors) ⇒ Object

Return a color set if an array of valid colors



97
98
99
100
101
102
103
104
105
# File 'lib/ruby2d/color.rb', line 97

def set(colors)
  # If a valid array of colors, return a `Color::Set` with those colors
  if colors.is_a?(Array) && colors.all? { |el| Color.valid? el }
    Color::Set.new(colors)
  # Otherwise, return single color
  else
    Color.new(colors)
  end
end

.valid?(color) ⇒ Boolean Also known as: is_valid?, is_hex?

Check if the color is valid

Returns:

  • (Boolean)


117
118
119
120
121
122
123
124
125
126
# File 'lib/ruby2d/color.rb', line 117

def valid?(color)
  color.is_a?(Color) ||             # color object
    NAMED_COLORS.key?(color) ||     # keyword
    hex?(color) ||                  # hexadecimal value
    (                               # Array of Floats from 0.0..1.0
      color.instance_of?(Array) &&
      color.length == 4 &&
      color.all? { |el| el.is_a?(Numeric) }
    )
end

Instance Method Details

#opacityObject

Convenience methods to alias ‘opacity` to `@a`



135
136
137
# File 'lib/ruby2d/color.rb', line 135

def opacity
  @a
end

#opacity=(opacity) ⇒ Object



139
140
141
# File 'lib/ruby2d/color.rb', line 139

def opacity=(opacity)
  @a = opacity
end

#to_aObject

Return colour components as an array [r, g, b, a]



144
145
146
# File 'lib/ruby2d/color.rb', line 144

def to_a
  [@r, @g, @b, @a]
end