Class: Branding::Pixel
- Inherits:
-
Object
- Object
- Branding::Pixel
- Defined in:
- lib/branding/pixel.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#uint32 ⇒ Object
Returns the value of attribute uint32.
Class Method Summary collapse
Instance Method Summary collapse
- #==(value) ⇒ Object
- #a ⇒ Object
- #b ⇒ Object
- #g ⇒ Object
-
#initialize(*opts) ⇒ Pixel
constructor
basic 2-space with background color per pixel.
- #inspect ⇒ Object
- #r ⇒ Object
- #to_i ⇒ Object
- #to_rgb ⇒ Object
- #to_rgba ⇒ Object
- #to_s ⇒ Object
- #width ⇒ Object
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
#uint32 ⇒ Object
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 |
#a ⇒ Object
73 74 75 |
# File 'lib/branding/pixel.rb', line 73 def a @uint32 & 0x000000ff end |
#b ⇒ Object
69 70 71 |
# File 'lib/branding/pixel.rb', line 69 def b @uint32 & 0x0000ff00 end |
#g ⇒ Object
65 66 67 |
# File 'lib/branding/pixel.rb', line 65 def g @uint32 & 0x00ff0000 end |
#inspect ⇒ Object
42 43 44 |
# File 'lib/branding/pixel.rb', line 42 def inspect "0x%0.8x" % @uint32 end |
#r ⇒ Object
61 62 63 |
# File 'lib/branding/pixel.rb', line 61 def r @uint32 & 0xff000000 end |
#to_i ⇒ Object
57 58 59 |
# File 'lib/branding/pixel.rb', line 57 def to_i uint32 end |
#to_rgb ⇒ Object
77 78 79 |
# File 'lib/branding/pixel.rb', line 77 def to_rgb {r: r, g: g, b: b} end |
#to_rgba ⇒ Object
81 82 83 |
# File 'lib/branding/pixel.rb', line 81 def to_rgba {r: r, g: g, b: b, a: a} end |
#to_s ⇒ Object
85 86 87 |
# File 'lib/branding/pixel.rb', line 85 def to_s "#{ANSI.bg(*ANSI.uint32_to_rgb(@uint32))} " end |
#width ⇒ Object
89 90 91 |
# File 'lib/branding/pixel.rb', line 89 def width 2 end |