In Files

Parent

Files

GD2::Color

Description

Color objects hold the red, green, blue, and alpha components for a pixel color. Color objects may also be linked to a particular palette index associated with an image.

Creating

Color objects are created by specifying the individual components:

red   = Color[1.0, 0.0, 0.0]
green = Color[0.0, 1.0, 0.0]
blue  = Color[0.0, 0.0, 1.0]

transparent_yellow = Color[1.0, 1.0, 0.0, 0.5]

The components may be specified as a percentage, as an explicit value between 0..RGB_MAX or 0..ALPHA_MAX, or as another color from which the associated component will be extracted.

Constants

BLACK
TRANSPARENT
WHITE

Attributes

index[R]

The palette index of this color, if associated with an image palette

palette[R]

The palette of this color, if associated with an image palette

Public Class Methods

new(r, g, b, a = ALPHA_OPAQUE) click to toggle source

Create a new Color object with the given component values.

# File lib/gd2/color.rb, line 79
def initialize(r, g, b, a = ALPHA_OPAQUE)
  r = self.class.normalize(r, RGB_MAX, :red)
  g = self.class.normalize(g, RGB_MAX, :green)
  b = self.class.normalize(b, RGB_MAX, :blue)
  a = self.class.normalize(a, ALPHA_MAX, :alpha)

  init_with_rgba(self.class.pack(r, g, b, a))
end

Public Instance Methods

<<(other) click to toggle source
Alias for: alpha_blend!
==(other) click to toggle source

Compare this color with another color. Returns true if the associated red, green, blue, and alpha components are identical.

# File lib/gd2/color.rb, line 129
def ==(other)
  other.kind_of?(Color) && rgba == other.rgba
end
===(other) click to toggle source

Return true if this color is visually identical to another color.

# File lib/gd2/color.rb, line 134
def ===(other)
  self == other || (self.transparent? && other.transparent?)
end
a() click to toggle source
Alias for: alpha
a=(value) click to toggle source
Alias for: alpha=
alpha() click to toggle source

Return the alpha component of this color (0..ALPHA_MAX).

# File lib/gd2/color.rb, line 198
def alpha
  (rgba & 0x7F000000) >> 24
end
Also aliased as: a
alpha=(value) click to toggle source

Modify the alpha component of this color. If this color is associated with a palette entry, this also modifies the palette.

# File lib/gd2/color.rb, line 205
def alpha=(value)
  self.rgba = (rgba & ~0xFF000000) |
    (self.class.normalize(value, ALPHA_MAX, :alpha) << 24)
end
Also aliased as: a=
alpha_blend(other) click to toggle source

Like Color#alpha_blend! except returns a new Color without modifying the receiver.

# File lib/gd2/color.rb, line 221
def alpha_blend(other)
  dup.alpha_blend!(other)
end
alpha_blend!(other) click to toggle source

Alpha blend this color with the given color. If this color is associated with a palette entry, this also modifies the palette.

# File lib/gd2/color.rb, line 213
def alpha_blend!(other)
  self.rgba = SYM[:gdAlphaBlend].call(rgba, other.rgba)[0]
  self
end
Also aliased as: <<
b() click to toggle source
Alias for: blue
b=(value) click to toggle source
Alias for: blue=
blue() click to toggle source

Return the blue component of this color (0..RGB_MAX).

# File lib/gd2/color.rb, line 184
def blue
  rgba & 0x0000FF
end
Also aliased as: b
blue=(value) click to toggle source

Modify the blue component of this color. If this color is associated with a palette entry, this also modifies the palette.

# File lib/gd2/color.rb, line 191
def blue=(value)
  self.rgba = (rgba & ~0x0000FF) |
    self.class.normalize(value, RGB_MAX, :blue)
end
Also aliased as: b=
eql?(other) click to toggle source

Compare this color with another color in a manner that takes into account palette identities.

# File lib/gd2/color.rb, line 140
def eql?(other)
  self == other &&
    (palette.nil? || other.palette.nil? ||
      (palette == other.palette && index == other.index))
end
from_palette?(palette = nil) click to toggle source

Return true if this color is associated with the specified palette, or with any palette if nil is given.

# File lib/gd2/color.rb, line 109
def from_palette?(palette = nil)
  @palette && @index && (palette.nil? || palette.equal?(@palette))
end
g() click to toggle source
Alias for: green
g=(value) click to toggle source
Alias for: green=
green() click to toggle source

Return the green component of this color (0..RGB_MAX).

# File lib/gd2/color.rb, line 170
def green
  (rgba & 0x00FF00) >> 8
end
Also aliased as: g
green=(value) click to toggle source

Modify the green component of this color. If this color is associated with a palette entry, this also modifies the palette.

# File lib/gd2/color.rb, line 177
def green=(value)
  self.rgba = (rgba & ~0x00FF00) |
    (self.class.normalize(value, RGB_MAX, :green) << 8)
end
Also aliased as: g=
opaque?() click to toggle source

Return true if the alpha channel of this color is completely opaque.

# File lib/gd2/color.rb, line 232
def opaque?
  alpha == ALPHA_OPAQUE
end
r() click to toggle source
Alias for: red
r=(value) click to toggle source
Alias for: red=
red() click to toggle source

Return the red component of this color (0..RGB_MAX).

# File lib/gd2/color.rb, line 156
def red
  (rgba & 0xFF0000) >> 16
end
Also aliased as: r
red=(value) click to toggle source

Modify the red component of this color. If this color is associated with a palette entry, this also modifies the palette.

# File lib/gd2/color.rb, line 163
def red=(value)
  self.rgba = (rgba & ~0xFF0000) |
    (self.class.normalize(value, RGB_MAX, :red) << 16)
end
Also aliased as: r=
to_s() click to toggle source

Return a string description of this color.

# File lib/gd2/color.rb, line 114
def to_s
  s  = 'RGB'
  s += "A" if alpha != ALPHA_OPAQUE
  s += "[#{@index}]" if @index
  s += '#' + [red, green, blue].map { |e| '%02X' % e }.join('')
  s += '%02X' % alpha if alpha != ALPHA_OPAQUE
  s
end
transparent?() click to toggle source

Return true if the alpha channel of this color is completely transparent.

# File lib/gd2/color.rb, line 227
def transparent?
  alpha == ALPHA_TRANSPARENT
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.