Class: PNG::Color

Inherits:
Object
  • Object
show all
Defined in:
lib/png.rb

Overview

RGBA colors

Constant Summary collapse

Background =

Transparent white

Color.from 0x00000000, "Transparent"
Black =
Color.from 0x000000FF, "Black"
Blue =
Color.from 0x0000FFFF, "Blue"
Brown =
Color.from 0x996633FF, "Brown"
Bubblegum =
Color.from 0xFF66FFFF, "Bubblegum"
Cyan =
Color.from 0x00FFFFFF, "Cyan"
Gray =
Color.from 0x7F7F7FFF, "Gray"
Green =
Color.from 0x00FF00FF, "Green"
Magenta =
Color.from 0xFF00FFFF, "Magenta"
Orange =
Color.from 0xFF7F00FF, "Orange"
Purple =
Color.from 0x7F007FFF, "Purple"
Red =
Color.from 0xFF0000FF, "Red"
White =
Color.from 0xFFFFFFFF, "White"
Yellow =
Color.from 0xFFFF00FF, "Yellow"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(red, green, blue, alpha, name = nil) ⇒ Color

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



283
284
285
286
# File 'lib/png.rb', line 283

def initialize(red, green, blue, alpha, name = nil)
  @values = "%c%c%c%c" % [red, green, blue, alpha]
  @name = name
end

Instance Attribute Details

#valuesObject (readonly)

Returns the value of attribute values.



271
272
273
# File 'lib/png.rb', line 271

def values
  @values
end

Class Method Details

.from(str, name = nil) ⇒ Object



273
274
275
276
277
278
# File 'lib/png.rb', line 273

def self.from str, name = nil
  str = "%08x" % str if Integer === str
  colors = str.scan(/[\da-f][\da-f]/i).map { |n| n.hex }
  colors << name
  self.new(*colors)
end

Instance Method Details

#==(other) ⇒ Object

:nodoc:



306
307
308
# File 'lib/png.rb', line 306

def ==(other) # :nodoc:
  self.class === other and other.values == values
end

#aObject

Alpha transparency component



328
# File 'lib/png.rb', line 328

def a; @values[3]; end

#bObject

Blue component



323
# File 'lib/png.rb', line 323

def b; @values[2]; end

#blend(color) ⇒ Object

Blends color into this color returning a new blended color.



333
334
335
336
337
338
# File 'lib/png.rb', line 333

def blend(color)
  return Color.new((r * (0xFF - color.a) + color.r * color.a) >> 8,
                   (g * (0xFF - color.a) + color.g * color.a) >> 8,
                   (b * (0xFF - color.a) + color.b * color.a) >> 8,
                   (a * (0xFF - color.a) + color.a * color.a) >> 8)
end

#gObject

Green component



318
# File 'lib/png.rb', line 318

def g; @values[1]; end

#inspectObject

:nodoc:



347
348
349
350
351
352
353
# File 'lib/png.rb', line 347

def inspect # :nodoc:
  if @name then
    "#<%s %s>" % [self.class, @name]
  else
    "#<%s %02x %02x %02x %02x>" % [self.class, r, g, b, a]
  end
end

#intensity(i) ⇒ Object

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



343
344
345
# File 'lib/png.rb', line 343

def intensity(i)
  return Color.new(r,g,b,(a*i) >> 8)
end

#rObject

Red component



313
# File 'lib/png.rb', line 313

def r; @values[0]; end

#to_asciiObject

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



359
360
361
362
363
364
365
366
# File 'lib/png.rb', line 359

def to_ascii
  return '  ' if a == 0x00
  brightness = (((r + g + b) / 3) * a) / 0xFF
  return '00' if brightness >= 0xc0
  return '++' if brightness >= 0x7F
  return ',,' if brightness >= 0x40
  return '..'
end

#to_sObject



368
369
370
371
372
373
374
# File 'lib/png.rb', line 368

def to_s
  if @name then
    @name
  else
    super
  end
end