Class: Rubydraw::Color
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
-
#alpha ⇒ Object
readonly
Returns the value of attribute alpha.
-
#blue ⇒ Object
readonly
Returns the value of attribute blue.
-
#green ⇒ Object
readonly
Returns the value of attribute green.
-
#red ⇒ Object
readonly
Returns the value of attribute red.
Class Method Summary collapse
-
.[](*args) ⇒ Object
Shorthand new method.
-
.from_ary(ary) ⇒ Object
Create a new color from an array with the format [r, g, b, a].
-
.in_bounds?(*args) ⇒ Boolean
Returns true if all arguments are within the valid RGB color values (0-255).
Instance Method Summary collapse
-
#+(other, a = 255) ⇒ Object
Return a new color resulting from mixing this color and
other
additively. -
#/(other, a = 255) ⇒ Object
Return a new color that is the average of this color and
other
. -
#calc_num_vals ⇒ Object
Calculate and store the numerical values.
-
#initialize(red, green, blue, alpha = 255) ⇒ Color
constructor
Create a new color with the given red, green, blue, and alpha values.
-
#invert ⇒ Object
(also: #complimentary, #opposite, #reverse)
Returns the complimentary color.
-
#to_ary ⇒ Object
Returns an Array containing each
rgba
value. -
#to_i(format) ⇒ Object
Convert this color to a numerical value, which only makes sense when read as a hex number.
- #to_s ⇒ Object
-
#to_sdl ⇒ Object
Create an SDL::Color equivilent to this Rubydraw::Color.
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
#alpha ⇒ Object (readonly)
Returns the value of attribute alpha.
32 33 34 |
# File 'lib/rubydraw/color.rb', line 32 def alpha @alpha end |
#blue ⇒ Object (readonly)
Returns the value of attribute blue.
32 33 34 |
# File 'lib/rubydraw/color.rb', line 32 def blue @blue end |
#green ⇒ Object (readonly)
Returns the value of attribute green.
32 33 34 |
# File 'lib/rubydraw/color.rb', line 32 def green @green end |
#red ⇒ Object (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).
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_vals ⇒ Object
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 |
#invert ⇒ Object 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_ary ⇒ Object
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.
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_s ⇒ Object
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_sdl ⇒ Object
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 |