Class: Colour

Inherits:
Object
  • Object
show all
Defined in:
src/ruby/models/colour.rb,
mrb_doc/models/colour.rb

Overview

The Colour class is used for setting the colour of basic primitives (Circle, Rectangle, etc) but also for setting transparency on Texture2D objects.

Colours collapse

LIGHTGRAY =

A nice lightgray colour.

GRAY =

A nice gray colour.

DARKGRAY =

A nice darkgray colour.

YELLOW =

A nice yellow colour.

GOLD =

A nice gold colour.

ORANGE =

A nice orange colour.

PINK =

A nice pink colour.

RED =

A nice red colour.

MAROON =

A nice maroon colour.

GREEN =

A nice green colour.

LIME =

A nice lime colour.

DARKGREEN =

A nice darkgreen colour.

SKYBLUE =

A nice skyblue colour.

BLUE =

A nice blue colour.

DARKBLUE =

A nice darkblue colour.

PURPLE =

A nice purple colour.

VIOLET =

A nice violet colour.

DARKPURPLE =

A nice darkpurple colour.

BEIGE =

A nice beige colour.

BROWN =

A nice brown colour.

DARKBROWN =

A nice darkbrown colour.

WHITE =

A nice white colour.

BLACK =

A nice black colour.

BLANK =

A nice blank colour.

MAGENTA =

A nice magenta colour.

RAYWHITE =

The official Raylib white.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(red: 0, green: 0, blue: 0, alpha: 255) ⇒ Colour

Creates a new instance of Colour.

Examples:

Basic usage

purple = Colour.new(red: 128, green: 0, blue: 255)

Parameters:

  • red (Integer) (defaults to: 0)

    A value between 0 and 255.

  • blue (Integer) (defaults to: 0)

    A value between 0 and 255.

  • green (Integer) (defaults to: 0)

    A value between 0 and 255.

  • alpha (Integer) (defaults to: 255)

    A value between 0 and 255.



15
16
17
18
# File 'mrb_doc/models/colour.rb', line 15

def initialize(red: 0, green: 0, blue: 0, alpha: 255)
  # mrb_Colour_initialize
  # src/mruby_integration/models/colour.cpp
end

Instance Attribute Details

#alphaInteger

Returns:

  • (Integer)


3
4
5
# File 'mrb_doc/models/colour.rb', line 3

def alpha
  @alpha
end

#blueInteger

Returns:

  • (Integer)


3
4
5
# File 'mrb_doc/models/colour.rb', line 3

def blue
  @blue
end

#greenInteger

Returns:

  • (Integer)


3
4
5
# File 'mrb_doc/models/colour.rb', line 3

def green
  @green
end

#redInteger

Returns:

  • (Integer)


3
4
5
# File 'mrb_doc/models/colour.rb', line 3

def red
  @red
end

Class Method Details

.[](red = 0, green = 0, blue = 0, alpha = 255) ⇒ Colour

A short form way to create new Colour objects.

Examples:

Basic usage

red = Colour[255, 0, 0]
green = Colour[0, 255, 0]
opaque = Colour[128, 128, 128, 196]

Parameters:

  • red (Integer) (defaults to: 0)

    A value between 0 and 255.

  • blue (Integer) (defaults to: 0)

    A value between 0 and 255.

  • green (Integer) (defaults to: 0)

    A value between 0 and 255.

  • alpha (Integer) (defaults to: 255)

    A value between 0 and 255.

Returns:



16
17
18
# File 'src/ruby/models/colour.rb', line 16

def self.[](red = 0, green = 0, blue = 0, alpha = 255)
  new(red: red, green: green, blue: blue, alpha: alpha)
end

.mock_return(red: 0, green: 0, blue: 0, alpha: 255) ⇒ String

A method used to generate the mock data for Raylib.

Examples:

Basic usage

Taylor::Raylib.mock_call(
  "Fade",
  Colour.mock_return(red: 1, blue: 2, green: 3, alpha: 4)
)

Parameters:

  • red (Integer) (defaults to: 0)

    A value between 0 and 255.

  • blue (Integer) (defaults to: 0)

    A value between 0 and 255.

  • green (Integer) (defaults to: 0)

    A value between 0 and 255.

  • alpha (Integer) (defaults to: 255)

    A value between 0 and 255.

Returns:

  • (String)


186
187
188
# File 'src/ruby/models/colour.rb', line 186

def self.mock_return(red: 0, green: 0, blue: 0, alpha: 255)
  [red, green, blue, alpha].map(&:to_s).join(" ")
end

Instance Method Details

#==(other) ⇒ Boolean

Compares the values to another Colour.

Examples:

Basic usage

puts Colour[200, 200, 200] == Colour::LIGHTGRAY
# => true

puts Colour[1, 2, 3, 4] == Colour::LIGHTGRAY
# => false

Parameters:

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
# File 'src/ruby/models/colour.rb', line 31

def ==(other)
  return super unless other.is_a?(Colour)

  red == other.red &&
    green == other.green &&
    blue == other.blue &&
    alpha == other.alpha
end

#brightness(percent) ⇒ Colour

Returns a Colour brightened by the passed in percent.

Examples:

Basic usage

red = Colour::RED
# => #<Colour:0x55b398928100 red:230 blue:55 green:41 alpha:255>

p red.brightness(0.5)
# => #<Colour:0x5557726209e0 red:242 blue:155 green:148 alpha:255>

p red.brightness(-0.5)
# => #<Colour:0x555772620770 red:115 blue:27 green:20 alpha:255>

Parameters:

  • percent (Float)

    A value between -1.0 and 1.0.

Returns:

Raises:

  • (ArgumentError)

    If the percent is out of bounds.



74
75
76
77
78
# File 'mrb_doc/models/colour.rb', line 74

def brightness(percent)
  # mrb_Colour_brightness
  # src/mruby_integration/models/colour.cpp
  Colour[1, 2, 3, 4]
end

#brightness!(percent) ⇒ Colour

Brightens the Colour by the passed in percent.

Examples:

Basic usage

red = Colour::RED
p red
# => #<Colour:0x55b398928100 red:230 blue:55 green:41 alpha:255>

red.brightness!(0.5)
p red
# => #<Colour:0x5612f03b0100 red:242 blue:155 green:148 alpha:255>

red.brightness!(-0.5)
p red
# => #<Colour:0x555a65a95100 red:121 blue:77 green:74 alpha:255>

Parameters:

  • percent (Float)

    A value between -1.0 and 1.0.

Returns:

Raises:

  • (ArgumentError)

    If the percent is out of bounds.



125
126
127
128
129
130
131
132
# File 'src/ruby/models/colour.rb', line 125

def brightness!(percent)
  brightness(percent).tap { |colour|
    self.red = colour.red
    self.green = colour.green
    self.blue = colour.blue
    self.alpha = colour.alpha
  }
end

#contrast(percent) ⇒ Colour

Returns a Colour contrasted by the passed in percent.

Examples:

Basic usage

red = Colour::RED
p red
# => #<Colour:0x1cf6c580 red:230 blue:55 green:41 alpha:255>

p red.contrast(0.5)
# => #<Colour:0x1cfc3b70 red:255 blue:0 green:0 alpha:255>

p red.contrast(-0.5)
# => #<Colour:0x1cfc3900 red:153 blue:109 green:105 alpha:255>

Parameters:

  • percent (Float)

    A value between -1.0 and 1.0.

Returns:

Raises:

  • (ArgumentError)

    If the percent is out of bounds.



96
97
98
99
100
# File 'mrb_doc/models/colour.rb', line 96

def contrast(percent)
  # mrb_Colour_contrast
  # src/mruby_integration/models/colour.cpp
  Colour[1, 2, 3, 4]
end

#contrast!(percent) ⇒ Colour

Contrasts the Colour by the passed in percent.

Examples:

Basic usage

red = Colour::RED
p red
# => #<Colour:0x1603f580 red:230 blue:55 green:41 alpha:255>

red.contrast!(0.5)
p red
# => #<Colour:0x1603f580 red:255 blue:0 green:0 alpha:255>

red.contrast!(-0.5)
p red
# => #<Colour:0x1603f580 red:159 blue:95 green:95 alpha:255>

Parameters:

  • percent (Float)

    A value between -1.0 and 1.0.

Returns:

Raises:

  • (ArgumentError)

    If the percent is out of bounds.



152
153
154
155
156
157
158
159
# File 'src/ruby/models/colour.rb', line 152

def contrast!(percent)
  contrast(percent).tap { |colour|
    self.red = colour.red
    self.green = colour.green
    self.blue = colour.blue
    self.alpha = colour.alpha
  }
end

#fade(alpha) ⇒ Colour

Returns a new Colour which is a faded version of the original.

Examples:

Basic usage

faded_blue = Colour::BLUE.fade(0.25)
p faded_blue
# => #<Vector2:0x555a13701450 red:0 blue:241 green:121 alpha:63>

p Colour::BLUE
# => #<Vector2:0x555a13679de0 red:0 blue:241 green:121 alpha:255>

Parameters:

  • alpha (Float)

    A value between 0.0 and 1.0.

Returns:

Raises:

  • (ArgumentError)

    If the alpha is out of bounds.



33
34
35
36
37
# File 'mrb_doc/models/colour.rb', line 33

def fade(alpha)
  # mrb_Colour_alpha
  # src/mruby_integration/models/colour.cpp
  Colour[1, 2, 3, 4]
end

#fade!(alpha) ⇒ Colour

Fades the Colour to the passed in percent.

Examples:

Basic usage

faded_blue = Colour[0, 255, 0, 196]
p faded_blue
# => #<Colour:0x5649cc302660 red:0 blue:255 green:0 alpha:196>

faded_blue.fade!(0.75)
p faded_blue
# => #<Colour:0x5649cc302660 red:0 blue:255 green:0 alpha:191>

Parameters:

  • alpha (Float)

    A value between 0.0 and 1.0.

Returns:

Raises:

  • (ArgumentError)

    If the alpha is out of bounds.



75
76
77
78
79
80
81
82
# File 'src/ruby/models/colour.rb', line 75

def fade!(alpha)
  fade(alpha).tap { |colour|
    self.red = colour.red
    self.green = colour.green
    self.blue = colour.blue
    self.alpha = colour.alpha
  }
end

#inspectString

Returns a string representation of the Colour that's useful for debugging.

Examples:

Basic usage

puts Colour::PURPLE.inspect # => #<Colour:0x102bd20 x:6.0 y:8.0>

p Colour::PURPLE # => #<Colour:0x102bd20 x:6.0 y:8.0>

Returns:

  • (String)


169
170
171
# File 'src/ruby/models/colour.rb', line 169

def inspect
  "#<Colour:0x#{object_id.to_s(16)} red:#{red} blue:#{blue} green:#{green} alpha:#{alpha}>"
end

#tint(colour) ⇒ Colour

Returns a tinted version of Colour with the passed in Colour.

Examples:

Basic usage

grey = Colour[128, 128, 128, 255]
p grey
# => #<Colour:0xb78270 red:128 blue:128 green:128 alpha:255>

blue_grey = grey.tint(Colour::BLUE)
p blue_grey
# => #<Colour:0x20638fd0 red:0 blue:120 green:60 alpha:255>

Parameters:

Returns:

Raises:

  • (ArgumentError)

    If the alpha is out of bounds.



53
54
55
56
57
# File 'mrb_doc/models/colour.rb', line 53

def tint(colour)
  # mrb_Colour_tint
  # src/mruby_integration/models/colour.cpp
  Colour[1, 2, 3, 4]
end

#tint!(colour) ⇒ Colour

Tints the Colour with the passed in Colour.

Examples:

Basic usage

grey = Colour[128, 128, 128, 255]
p grey
# => #<Colour:0xb78270 red:128 blue:128 green:128 alpha:255>

grey.tint!(Colour::GREEN)
p grey
# => #<Colour:0xb78270 red:0 blue:24 green:114 alpha:255>

Parameters:

Returns:



98
99
100
101
102
103
104
105
# File 'src/ruby/models/colour.rb', line 98

def tint!(colour)
  tint(colour).tap { |result|
    self.red = result.red
    self.green = result.green
    self.blue = result.blue
    self.alpha = result.alpha
  }
end

#to_hHash

Return the Colour as a Hash.

Examples:

Basic usage

p Colour[64, 128, 196, 255].to_h
# => {
#      red: 64,
#      blue: 128,
#      green: 196,
#      alpha: 255,
#    }

Returns:

  • (Hash)


52
53
54
55
56
57
58
59
# File 'src/ruby/models/colour.rb', line 52

def to_h
  {
    red: red,
    green: green,
    blue: blue,
    alpha: alpha
  }
end