class PNG::Color

A 32 bit RGBA color. Can be created from RGB or RGBA via new, numeric value or hex string via from, or HSV via from_hsv.

Constants

Background

Transparent white

Black
Blue
Brown
Bubblegum
Cyan
Gray
Green
MAX
Magenta
Orange
Purple
Red
White
Yellow

Attributes

values[R]

Public Class Methods

from(str, name = nil) click to toggle source

Create a new color from a string or integer value. Can take an optional name as well.

# File lib/png.rb, line 214
def self.from str, name = nil
  str = "%08x" % str if Integer === str
  colors = str.scan(/[\da-f][\da-f]/i).map(&:hex)
  colors << name
  self.new(*colors)
end
from_hsv(h, s, v) click to toggle source

Creates a new RGB color from HSV equivalent values.

# File lib/png.rb, line 338
def self.from_hsv h, s, v
  r = g = b = v # gray
  unless s == 0.0 then
    h += 255 if h < 0
    h  = h / 255.0 * 6.0
    s  /= 255.0
    v  /= 255.0
    i  = h.floor
    f  = h - i
    p = v * (1 - (s))
    q = v * (1 - (s * (f)))
    w = v * (1 - (s * (1-f)))
    r, g, b = case i
              when 0, 6 then
                [v, w, p]
              when 1 then
                [q, v, p]
              when 2 then
                [p, v, w]
              when 3 then
                [p, q, v]
              when 4 then
                [w, p, v]
              when 5 then
                [v, p, q]
              else
                raise [h, s, v, i, f, p, q, w].inspect
              end
  end
  self.new((r * 255).round, (g * 255).round, (b * 255).round)
end
new(red, green, blue, alpha = MAX, name = nil) click to toggle source

Creates a new color with values red, green, blue, and alpha.

# File lib/png.rb, line 224
def initialize red, green, blue, alpha = MAX, name = nil
  @values = "%c%c%c%c" % [red, green, blue, alpha]
  @name = name
end

Public Instance Methods

a() click to toggle source

Alpha transparency component

# File lib/png.rb, line 290
def a; @values.getbyte 3; end
b() click to toggle source

Blue component

# File lib/png.rb, line 285
def b; @values.getbyte 2; end
blend(color) click to toggle source

Blends color into this color returning a new blended color.

# File lib/png.rb, line 295
def blend color
  Color.new(((r + color.r) / 2), ((g + color.g) / 2),
            ((b + color.b) / 2), ((a + color.a) / 2))
end
g() click to toggle source

Green component

# File lib/png.rb, line 280
def g; @values.getbyte 1; end
intensity(i) click to toggle source

Returns a new color with an alpha value adjusted by i.

# File lib/png.rb, line 303
def intensity i
  Color.new(r, g, b, (a*i) >> 8)
end
r() click to toggle source

Red component

# File lib/png.rb, line 275
def r; @values.getbyte 0; end
rgb() click to toggle source

Return an array of RGB

# File lib/png.rb, line 268
def rgb # TODO: rgba?
  return r, g, b
end
to_ascii() click to toggle source

An ASCII representation of this color, almost suitable for making ASCII art!

# File lib/png.rb, line 319
def to_ascii
  return "  " if a == 0x00

  brightness = (((r + g + b) / 3) * a) / 0xFF

  %w[.. ,, ++ 00][brightness / 64]
end
to_hsv() click to toggle source

Returns HSV equivalent of the current color.

# File lib/png.rb, line 373
def to_hsv # errors = 54230 out of 255^3 are off by about 1 on r, g, or b
  rgb = self.rgb
  r, g, b = rgb
  h, s, v = 0, 0, rgb.max

  return h, s, v if v == 0

  range = v - rgb.min
  s = 255 * range / v

  return h, s, v if s == 0

  h = case v
      when r then
        0x00 + 43 * (g - b) / range # 43 = 1/4 of 360 scaled to 255
      when g then
        0x55 + 43 * (b - r) / range
      else
        0xAA + 43 * (r - g) / range
      end

  return h.round, s.round, v.round
end
|(other) click to toggle source

“Bitwise or” as applied to colors. Background color is considered false.

# File lib/png.rb, line 257
def | other
  self == Background ? other : self
end