Class: ChunkyPNG::Pixel

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

Constant Summary collapse

BLACK =
rgb(  0,   0,   0)
WHITE =
rgb(255, 255, 255)
TRANSPARENT =
rgba(0, 0, 0, 0)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Pixel

Returns a new instance of Pixel.



7
8
9
# File 'lib/chunky_png/pixel.rb', line 7

def initialize(value)
  @value = value
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



5
6
7
# File 'lib/chunky_png/pixel.rb', line 5

def value
  @value
end

Class Method Details

.bytesize(color_mode) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# File 'lib/chunky_png/pixel.rb', line 102

def self.bytesize(color_mode)
  case color_mode
    when ChunkyPNG::COLOR_INDEXED         then 1
    when ChunkyPNG::COLOR_TRUECOLOR       then 3
    when ChunkyPNG::COLOR_TRUECOLOR_ALPHA then 4
    when ChunkyPNG::COLOR_GRAYSCALE       then 1
    when ChunkyPNG::COLOR_GRAYSCALE_ALPHA then 2
    else raise "Don't know the bytesize of pixels in this colormode: #{color_mode}!"
  end
end

.from_rgb_stream(stream) ⇒ Object



23
24
25
# File 'lib/chunky_png/pixel.rb', line 23

def self.from_rgb_stream(stream)
  self.rgb(*stream.unpack('C3'))
end

.from_rgba_stream(stream) ⇒ Object



27
28
29
# File 'lib/chunky_png/pixel.rb', line 27

def self.from_rgba_stream(stream)
  self.rgba(*stream.unpack('C4'))
end

.grayscale(teint, a = 255) ⇒ Object



19
20
21
# File 'lib/chunky_png/pixel.rb', line 19

def self.grayscale(teint, a = 255)
  rgba(teint, teint, teint, a)
end

.rgb(r, g, b) ⇒ Object



11
12
13
# File 'lib/chunky_png/pixel.rb', line 11

def self.rgb(r, g, b)
  rgba(r, g, b, 255)
end

.rgba(r, g, b, a) ⇒ Object



15
16
17
# File 'lib/chunky_png/pixel.rb', line 15

def self.rgba(r, g, b, a)
  new(r << 24 | g << 16 | b << 8 | a)
end

Instance Method Details

#<=>(other) ⇒ Object



69
70
71
# File 'lib/chunky_png/pixel.rb', line 69

def <=>(other)
  other.value <=> self.value
end

#aObject



43
44
45
# File 'lib/chunky_png/pixel.rb', line 43

def a
  @value & 0x000000ff
end

#bObject



39
40
41
# File 'lib/chunky_png/pixel.rb', line 39

def b
  (@value & 0x0000ff00) >> 8
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


63
64
65
# File 'lib/chunky_png/pixel.rb', line 63

def eql?(other)
  other.kind_of?(self.class) && other.value == self.value
end

#gObject



35
36
37
# File 'lib/chunky_png/pixel.rb', line 35

def g
  (@value & 0x00ff0000) >> 16
end

#grayscale?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/chunky_png/pixel.rb', line 51

def grayscale?
  r == g && r == b
end

#hashObject



59
60
61
# File 'lib/chunky_png/pixel.rb', line 59

def hash
  @value.hash
end

#index(palette) ⇒ Object



81
82
83
# File 'lib/chunky_png/pixel.rb', line 81

def index(palette)
  palette.index(self)
end

#inspectObject



55
56
57
# File 'lib/chunky_png/pixel.rb', line 55

def inspect
  '#%08x' % @value
end

#opaque?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/chunky_png/pixel.rb', line 47

def opaque?
  a == 0x000000ff
end

#rObject



31
32
33
# File 'lib/chunky_png/pixel.rb', line 31

def r
  (@value & 0xff000000) >> 24
end

#to_grayscale_alpha_bytesObject



93
94
95
# File 'lib/chunky_png/pixel.rb', line 93

def to_grayscale_alpha_bytes
  [r, a] # Assumption: r == g == b
end

#to_grayscale_bytesObject



89
90
91
# File 'lib/chunky_png/pixel.rb', line 89

def to_grayscale_bytes
  [r] # Assumption: r == g == b
end

#to_indexed_bytes(palette) ⇒ Object



85
86
87
# File 'lib/chunky_png/pixel.rb', line 85

def to_indexed_bytes(palette)
  [index(palette)]
end

#to_truecolor_alpha_bytesObject



73
74
75
# File 'lib/chunky_png/pixel.rb', line 73

def to_truecolor_alpha_bytes
  [r,g,b,a]
end

#to_truecolor_bytesObject



77
78
79
# File 'lib/chunky_png/pixel.rb', line 77

def to_truecolor_bytes
  [r,g,b]
end