Class: Branding::Pixel

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

Direct Known Subclasses

Pixel2x, PixelHiColor

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*opts) ⇒ Pixel

basic 2-space with background color per pixel



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/branding/pixel.rb', line 23

def initialize(*opts)
  opts = opts.first if opts.size == 1

  case opts
  when self.class
    @uint32 = opts.uint32
  when Fixnum
    @uint32 = opts
  when Array
    r,g,b,a = opts
    a ||= 0xff # alpha is optional. If it is not supplied, assume full-alpha.
    @uint32 = (r << 24) | (g << 16) | b << 8 | a
  when Hash
    #(r << 24) | (g << 16) | b << 8)
  else
    raise "Cannot initialize Pixel with #{opts.inspect}."
  end
end

Instance Attribute Details

#uint32Object

Returns the value of attribute uint32.



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

def uint32
  @uint32
end

Class Method Details

.load_strategy(pixels, height: 0, width: 0) ⇒ Object



9
10
11
12
13
# File 'lib/branding/pixel.rb', line 9

def self.load_strategy(pixels, height: 0, width: 0)
  pixels.each do |pixel_value|
    yield self.new(pixel_value)
  end
end

.rgb(r, g, b) ⇒ Object



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

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

Instance Method Details

#==(value) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/branding/pixel.rb', line 46

def ==(value)
  case value
  when self.class
    @uint32 == value.uint32
  when Fixnum
    @uint32 == value
  else
    @uint32 == value
  end
end

#aObject



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

def a
  @uint32 & 0x000000ff
end

#bObject



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

def b
  @uint32 & 0x0000ff00
end

#gObject



65
66
67
# File 'lib/branding/pixel.rb', line 65

def g
  @uint32 & 0x00ff0000
end

#inspectObject



42
43
44
# File 'lib/branding/pixel.rb', line 42

def inspect
  "0x%0.8x" % @uint32
end

#rObject



61
62
63
# File 'lib/branding/pixel.rb', line 61

def r
  @uint32 & 0xff000000
end

#to_iObject



57
58
59
# File 'lib/branding/pixel.rb', line 57

def to_i
  uint32
end

#to_rgbObject



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

def to_rgb
  {r: r, g: g, b: b}
end

#to_rgbaObject



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

def to_rgba
  {r: r, g: g, b: b, a: a}
end

#to_sObject



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

def to_s
  "#{ANSI.bg(*ANSI.uint32_to_rgb(@uint32))}  "
end

#widthObject



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

def width
  2
end