Class: Rubydraw::Color

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

Overview

Instances of Color are created with four arguments: Red, green, blue, and alpha (see Rubydraw::Color#initialize) The last is 0 by default, and all should have values ranging from 0 to 255. Use this to specify colors instead of hex numbers. Each color instance can return its numerical value, but only Rubydraw itself should need it.

Constant Summary collapse

White =
new(255, 255, 255)
Black =
new(0, 0, 0)
Red =

The primary colors

new(255, 0, 0)
Green =
new(0, 255, 0)
Blue =
new(0, 0, 255)
Yellow =

Secondary colors

new(255, 255, 0)
Magenta =
new(255, 0, 255)
Cyan =
new(0, 255, 255)
Purple =

Other colors

new(128, 0, 255)
Orange =
new(255, 128, 0)
LimeGreen =
new(128, 255, 0)
SeaweedGreen =
new(0, 255, 128)
LightBlue =
new(0, 128, 255)
Pink =
new(255, 0, 128)
Grey =
new(128, 128, 128)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(red, green, blue, alpha = 255) ⇒ Color

Create a new color with the given red, green, blue, and alpha values. Alpha is 0 by default.

TODO: Add other color specs, like HSV or maybe CYMK



38
39
40
41
42
43
44
# File 'lib/rubydraw/color.rb', line 38

def initialize(red, green, blue, alpha=255)
  unless self.class.in_bounds?(red, green, blue, alpha)
    raise IndexError, "One or more color values are out of bounds (must be between 0 and 255)"
  end
  @red, @green, @blue, @alpha = red, green, blue, alpha
  calc_num_vals
end

Instance Attribute Details

#alphaObject (readonly)

Returns the value of attribute alpha.



32
33
34
# File 'lib/rubydraw/color.rb', line 32

def alpha
  @alpha
end

#blueObject (readonly)

Returns the value of attribute blue.



32
33
34
# File 'lib/rubydraw/color.rb', line 32

def blue
  @blue
end

#greenObject (readonly)

Returns the value of attribute green.



32
33
34
# File 'lib/rubydraw/color.rb', line 32

def green
  @green
end

#redObject (readonly)

Returns the value of attribute red.



32
33
34
# File 'lib/rubydraw/color.rb', line 32

def red
  @red
end

Class Method Details

.[](*args) ⇒ Object

Shorthand new method.



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

def self.[](*args)
  self.new(*args)
end

.from_ary(ary) ⇒ Object

Create a new color from an array with the format [r, g, b, a]. Ignores any extraneous values.



23
24
25
# File 'lib/rubydraw/color.rb', line 23

def self.from_ary(ary)
  return self.new(ary[0], ary[1], ary[2], ary[3])
end

.in_bounds?(*args) ⇒ Boolean

Returns true if all arguments are within the valid RGB color values (0-255).

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
# File 'lib/rubydraw/color.rb', line 11

def self.in_bounds?(*args)
  args.each {|element|
    if (0..255).include?(element)
      return true
    else
      return false
    end
  }
end

Instance Method Details

#+(other, a = 255) ⇒ Object

Return a new color resulting from mixing this color and other additively.



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

def +(other, a=255)
  r = [@red + other.red, 255].min
  g = [@green + other.green, 255].min
  b = [@blue + other.blue, 255].min
  Color.new(r, g, b, a)
end

#/(other, a = 255) ⇒ Object

Return a new color that is the average of this color and other.



117
118
119
120
121
122
# File 'lib/rubydraw/color.rb', line 117

def /(other, a=255)
  r = [(@red + other.red) / 2, 255].min
  g = [(@green + other.green) / 2, 255].min
  b = [(@blue + other.blue) / 2, 255].min
  Color.new(r, g, b, a)
end

#calc_num_valsObject

Calculate and store the numerical values. You shouldn’t need this (see Rubydraw::Color.new).



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rubydraw/color.rb', line 48

def calc_num_vals
  # Get the hex string for each value.
  hex_alpha = (@alpha.to_s(16)).color_string
  hex_red = (@red.to_s(16)).color_string
  hex_green = (@green.to_s(16)).color_string
  hex_blue = (@blue.to_s(16)).color_string
  # Construct a hex string (only compatible with surfaces other than
  # the window) using the previously determined hex colors. Not sure
  # how alpha comes in here yet... I'll figure it out.
  surf_color_string = hex_red + hex_green + hex_blue
  @surface_num_val = surf_color_string.to_i(16)
  # *Note:* SDL's color format for the display is different than other
  # ordinary surfaces. It is: +BBGGRRAA+.
  display_color_string = hex_blue + hex_green + hex_red + hex_alpha
  @display_num_val = display_color_string.to_i(16)
end

#invertObject Also known as: complimentary, opposite, reverse

Returns the complimentary color.



80
81
82
# File 'lib/rubydraw/color.rb', line 80

def invert
  Color.new(255 - @red, 255 - @green, 255 - @blue, @alpha)
end

#to_aryObject

Returns an Array containing each rgba value.

Example:

color = Rubydraw::Color.new(red = 200, green = 60, blue = 5, alpha = 255)
=> #<Rubydraw::Color: (@red: 200, @green: 60, @blue: 5, @alpha: 255)>
color.to_ary
=> [200, 60, 5, 255]


95
96
97
# File 'lib/rubydraw/color.rb', line 95

def to_ary
  [@red, @blue, @green, @alpha]
end

#to_i(format) ⇒ Object

Convert this color to a numerical value, which only makes sense when read as a hex number.

Also see the comments in: Rubydraw::Color#calc_num_vals.

Raises:

  • (ArgumentError)


69
70
71
72
73
74
75
76
77
# File 'lib/rubydraw/color.rb', line 69

def to_i(format)
  if format == :surface or format == :display_fullscreen
    return @surface_num_val
  end
  if format == :display
    return @display_num_val
  end
  raise ArgumentError, "Unrecognized color format \"@{format}\""
end

#to_sObject



99
100
101
# File 'lib/rubydraw/color.rb', line 99

def to_s
  "#<#{self.class}: (@red: #{@red}, @green: #{@green}, @blue: #{@blue}, @alpha: #{@alpha})>"
end

#to_sdlObject

Create an SDL::Color equivilent to this Rubydraw::Color.



104
105
106
# File 'lib/rubydraw/color.rb', line 104

def to_sdl
  SDL::Color.new(to_ary)
end