Class: Types::RplGrOb
- Inherits:
-
Object
- Object
- Types::RplGrOb
- Defined in:
- lib/rpl/types/grob.rb
Instance Attribute Summary collapse
-
#bits ⇒ Object
Returns the value of attribute bits.
-
#height ⇒ Object
Returns the value of attribute height.
-
#width ⇒ Object
Returns the value of attribute width.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #clear ⇒ Object
- #get_pixel(pos_x, pos_y) ⇒ Object
-
#initialize(init) ⇒ RplGrOb
constructor
A new instance of RplGrOb.
- #set_pixel(pos_x, pos_y, value) ⇒ Object
- #to_braille ⇒ Object
- #to_s ⇒ Object
- #to_text ⇒ Object
- #value ⇒ Object
Constructor Details
#initialize(init) ⇒ RplGrOb
Returns a new instance of RplGrOb.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/rpl/types/grob.rb', line 39 def initialize( init ) raise RplTypeError unless self.class.can_parse?( init ) parsed = if init.instance_of?( RplGrOb ) init.value else /^GROB:(?<width>\d+):(?<height>\d+):(?<bits>[0-9a-f]+)$/.match( init ) end @width = parsed[:width].to_i @height = parsed[:height].to_i @bits = BitArray.new @bits.from_i( parsed[:bits].to_i( 16 ) ) end |
Instance Attribute Details
#bits ⇒ Object
Returns the value of attribute bits.
35 36 37 |
# File 'lib/rpl/types/grob.rb', line 35 def bits @bits end |
#height ⇒ Object
Returns the value of attribute height.
35 36 37 |
# File 'lib/rpl/types/grob.rb', line 35 def height @height end |
#width ⇒ Object
Returns the value of attribute width.
35 36 37 |
# File 'lib/rpl/types/grob.rb', line 35 def width @width end |
Class Method Details
.can_parse?(value) ⇒ Boolean
64 65 66 67 |
# File 'lib/rpl/types/grob.rb', line 64 def self.can_parse?( value ) value.instance_of?( RplGrOb ) || ( value.instance_of?( String ) && value.match?(/^GROB:\d+:\d+:[0-9a-f]+$/) ) end |
Instance Method Details
#==(other) ⇒ Object
69 70 71 72 73 74 |
# File 'lib/rpl/types/grob.rb', line 69 def ==( other ) other.class == RplGrOb && other.width == @width && other.height == @height && other.bits.to_i == @bits.to_i end |
#clear ⇒ Object
107 108 109 |
# File 'lib/rpl/types/grob.rb', line 107 def clear @bits.from_i( 0 ) end |
#get_pixel(pos_x, pos_y) ⇒ Object
99 100 101 |
# File 'lib/rpl/types/grob.rb', line 99 def get_pixel( pos_x, pos_y ) @bits[ ( pos_y * @width ) + pos_x ] end |
#set_pixel(pos_x, pos_y, value) ⇒ Object
103 104 105 |
# File 'lib/rpl/types/grob.rb', line 103 def set_pixel( pos_x, pos_y, value ) @bits[ ( pos_y * @width ) + pos_x ] = value.to_i.zero? ? 0 : 1 end |
#to_braille ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/rpl/types/grob.rb', line 87 def to_braille canvas = Drawille::Canvas.new @height.times do |y| @width.times do |x| canvas[x, y] = @bits[ ( y * @width ) + x ] == 1 end end canvas.frame end |
#to_s ⇒ Object
60 61 62 |
# File 'lib/rpl/types/grob.rb', line 60 def to_s "GROB:#{@width}:#{@height}:#{@bits.to_i.to_s( 16 )}" end |
#to_text ⇒ Object
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/rpl/types/grob.rb', line 76 def to_text @bits.to_i .to_s(2) .ljust(@width * @height, '0') .slice(0, @width * @height) .scan(/.{1,#{@width}}/) .join("\n") .gsub( '0', '_' ) .gsub( '1', '.' ) end |
#value ⇒ Object
54 55 56 57 58 |
# File 'lib/rpl/types/grob.rb', line 54 def value { width: @width, height: @height, bits: @bits.to_i.to_s( 16 ) } end |