Class | Joyau::Color |
In: |
ruby/site_ruby/joyau/color.rb
|
Parent: | Object |
A class which represents a color. Used in order to have a better interface than a simple hash.
It can be given to any method asking a color, excepted those which require an hexdecimal value, just like a hash :
line.color = Color.new
However, Joyau’s function which returns a color returns a hash. Luckily, You can easily create a Color from them :
a_color = Color.new(line.color)
Another syntax is provided, for convenience :
{ "r" => 255, "g" => 120, "b" => 40, "a" => 70 }.to_col => Color(255, 120, 40, 70)
BLACK | = | Color.new( 0, 0, 0, 255) |
WHITE | = | Color.new(255, 255, 255, 255) |
NONE | = | Color.new( 0, 0, 0, 0) |
RED | = | Color.new(255, 0, 0, 255) |
BLUE | = | Color.new( 0, 0, 255, 255) |
GREEN | = | Color.new( 0, 255, 0, 255) |
Creates a new Color. If one argument is given, it’s a Hash : Example :
Color.new(DrawableText.new.backgroud)
If three arguments are given, a new color is created, using the RGB format :
Color.new(0, 0, 0) # Black Color.new(255, 0, 0) # Red Color.new(0, 255, 0) # Green Color.new(255, 255, 255) # White
If a fourth argument is given, it’s the alpha value, which is 255 by default :
Color.new(255, 255, 255, 127)
Finally, if no arguments are given, a white color is created :
Color.new == Color.new(255, 255, 255, 255) => true
# File ruby/site_ruby/joyau/color.rb, line 142 def initialize(*args) @hash = Hash.new if args.size == 1 @hash = args[0] elsif args.size >= 3 @hash["r"] = args[0] @hash["g"] = args[1] @hash["b"] = args[2] if args.size > 3 @hash["a"] = args[3] else @hash["a"] = 255 end else @hash["r"] = 255 @hash["g"] = 255 @hash["b"] = 255 @hash["a"] = 255 end end
Returns whether self and other are equal. Example :
require 'joyau/color' Color.new(255, 0, 0, 255) == Color.new(255, 0, 0) => true Color.new(255, 0, 0, 255) == Color.new(255, 0, 0, 127) => false
# File ruby/site_ruby/joyau/color.rb, line 110 def ==(other) @hash["r"] == other.r and @hash["g"] == other.g and @hash["b"] == other.b and @hash["a"] == other.a end
Changes the color’s alpha value
# File ruby/site_ruby/joyau/color.rb, line 95 def a=(val) @hash["a"] = val end
Returns the blue level in the color
# File ruby/site_ruby/joyau/color.rb, line 60 def b @hash["b"] end
Changes the color’s blue value
# File ruby/site_ruby/joyau/color.rb, line 88 def b=(val) @hash["b"] = val end
Returns the green level in the color
# File ruby/site_ruby/joyau/color.rb, line 53 def g @hash["g"] end
Changes the color’s green value
# File ruby/site_ruby/joyau/color.rb, line 81 def g=(val) @hash["g"] = val end
Returns a color’s hexadecimal value :
Color.new(255, 255, 0).hex => 0xffffff00
# File ruby/site_ruby/joyau/color.rb, line 170 def hex ((@hash["r"]) | (@hash["g"] << 8) | (@hash["b"] << 16) | (@hash["a"] << 24)) end
Returns the red level in the color
# File ruby/site_ruby/joyau/color.rb, line 46 def r @hash["r"] end