Object
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.
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.
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
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
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
Return the alpha component of this color (0..ALPHA_MAX).
# File lib/gd2/color.rb, line 198 def alpha (rgba & 0x7F000000) >> 24 end
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
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 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
Return the blue component of this color (0..RGB_MAX).
# File lib/gd2/color.rb, line 184 def blue rgba & 0x0000FF end
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
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
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
Return the green component of this color (0..RGB_MAX).
# File lib/gd2/color.rb, line 170 def green (rgba & 0x00FF00) >> 8 end
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
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
Return the red component of this color (0..RGB_MAX).
# File lib/gd2/color.rb, line 156 def red (rgba & 0xFF0000) >> 16 end
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
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
Generated with the Darkfish Rdoc Generator 2.