Class: ZPNG::Color

Inherits:
Struct
  • Object
show all
Defined in:
lib/zpng/color.rb

Constant Summary collapse

BLACK =
Color.new(0,0,0,0xff)
WHITE =
Color.new(0xff,0xff,0xff,0xff)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Color

Returns a new instance of Color.



4
5
6
7
# File 'lib/zpng/color.rb', line 4

def initialize *args
  super
  self.a ||= 0xff
end

Instance Attribute Details

#aObject Also known as: alpha

Returns the value of attribute a

Returns:

  • (Object)

    the current value of a



2
3
4
# File 'lib/zpng/color.rb', line 2

def a
  @a
end

#bObject

Returns the value of attribute b

Returns:

  • (Object)

    the current value of b



2
3
4
# File 'lib/zpng/color.rb', line 2

def b
  @b
end

#gObject

Returns the value of attribute g

Returns:

  • (Object)

    the current value of g



2
3
4
# File 'lib/zpng/color.rb', line 2

def g
  @g
end

#rObject

Returns the value of attribute r

Returns:

  • (Object)

    the current value of r



2
3
4
# File 'lib/zpng/color.rb', line 2

def r
  @r
end

Class Method Details

.from_grayscale(value, alpha = nil) ⇒ Object



31
32
33
# File 'lib/zpng/color.rb', line 31

def self.from_grayscale value, alpha = nil
  Color.new value,value,value, alpha
end

Instance Method Details

#alpha=(v) ⇒ Object



10
# File 'lib/zpng/color.rb', line 10

def alpha= v; self.a = v; end

#black?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/zpng/color.rb', line 19

def black?
  r == 0 && g == 0 && b == 0
end

#inspectObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/zpng/color.rb', line 43

def inspect
  if r && g && b && a
    "#<ZPNG::Color #%02x%02x%02x a=%d>" % [r,g,b,a]
  else
    rs = r ? "%02x" % r : "??"
    gs = g ? "%02x" % g : "??"
    bs = b ? "%02x" % b : "??"
    as = a ? "%d"   % a : "?"
    "#<ZPNG::Color #%s%s%s%s a=%s>" % [rs,gs,bs,as]
  end
end

#to_grayscaleObject



27
28
29
# File 'lib/zpng/color.rb', line 27

def to_grayscale
  (r+g+b)/3
end

#to_iObject



39
40
41
# File 'lib/zpng/color.rb', line 39

def to_i
  ((a||0) << 24) + ((r||0) << 16) + ((g||0) << 8) + (b||0)
end

#to_sObject



35
36
37
# File 'lib/zpng/color.rb', line 35

def to_s
  "%02X%02X%02X" % [r,g,b]
end

#transparent?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/zpng/color.rb', line 23

def transparent?
  a == 0
end

#white?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/zpng/color.rb', line 15

def white?
  r == 0xff && g == 0xff && b == 0xff
end