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)

Methods

==   a   a=   alpha   alpha=   b   b=   blue   blue=   g   g=   green   green=   hex   new   r   r=   red   red=   to_hash  

Constants

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)

Public Class methods

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

[Source]

# 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

Public Instance methods

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

[Source]

# 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

Returns the color’s alpha value

[Source]

# File ruby/site_ruby/joyau/color.rb, line 67
    def a
      @hash["a"]
    end

Changes the color’s alpha value

[Source]

# File ruby/site_ruby/joyau/color.rb, line 95
    def a=(val)
      @hash["a"] = val
    end
alpha()

Alias for a

alpha=(val)

Alias for a=

Returns the blue level in the color

[Source]

# File ruby/site_ruby/joyau/color.rb, line 60
    def b
      @hash["b"]
    end

Changes the color’s blue value

[Source]

# File ruby/site_ruby/joyau/color.rb, line 88
    def b=(val)
      @hash["b"] = val
    end
blue()

Alias for b

blue=(val)

Alias for b=

Returns the green level in the color

[Source]

# File ruby/site_ruby/joyau/color.rb, line 53
    def g
      @hash["g"]
    end

Changes the color’s green value

[Source]

# File ruby/site_ruby/joyau/color.rb, line 81
    def g=(val)
      @hash["g"] = val
    end
green()

Alias for g

green=(val)

Alias for g=

Returns a color’s hexadecimal value :

  Color.new(255, 255, 0).hex
  => 0xffffff00

[Source]

# 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

[Source]

# File ruby/site_ruby/joyau/color.rb, line 46
    def r
      @hash["r"]
    end

Changes the color’s red value.

[Source]

# File ruby/site_ruby/joyau/color.rb, line 74
    def r=(val)
      @hash["r"] = val
    end
red()

Alias for r

red=(val)

Alias for r=

Returns the hash. This methods allows the compatibility with Joyau, and is called internally. This way, you can create similar classes.

[Source]

# File ruby/site_ruby/joyau/color.rb, line 39
    def to_hash
      @hash
    end

[Validate]