Class: VisualQrcode::PixelsHandler

Inherits:
Object
  • Object
show all
Includes:
PixelTools
Defined in:
lib/visual_qrcode/pixels_handler.rb

Constant Summary

Constants included from PixelTools

PixelTools::PIXEL_DEPTH

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PixelTools

#max_depth, #pixel_of, #pixel_of_color, #transparent_pixel, #white_pixel

Constructor Details

#initialize(pixels: nil, image_path: nil) ⇒ PixelsHandler

Returns a new instance of PixelsHandler.



12
13
14
15
16
17
18
19
# File 'lib/visual_qrcode/pixels_handler.rb', line 12

def initialize(pixels: nil, image_path: nil)
  if pixels
    @pixels = pixels
  else
    @image = MiniMagick::Image.open(image_path)
    set_pixels_from_image
  end
end

Instance Attribute Details

#pixelsObject

Returns the value of attribute pixels.



10
11
12
# File 'lib/visual_qrcode/pixels_handler.rb', line 10

def pixels
  @pixels
end

Instance Method Details

#add_margin(margin, color: :transparent) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/visual_qrcode/pixels_handler.rb', line 41

def add_margin(margin, color: :transparent)
  pixel = pixel_of_color(color)
  row_margin = [pixel] * margin
  col_margin = [Array.new(size + (2 * margin), pixel)] * margin

  margined_rows = @pixels.map do |row|
    row_margin + row + row_margin
  end

  @pixels = col_margin + margined_rows + col_margin
end

#dimensionsObject



25
26
27
# File 'lib/visual_qrcode/pixels_handler.rb', line 25

def dimensions
  [size] * 2
end

#make_squareObject



53
54
55
56
57
58
59
60
61
# File 'lib/visual_qrcode/pixels_handler.rb', line 53

def make_square
  max_size = [@pixels.length, @pixels.first.length].max

  if @pixels.length < max_size
    add_transparent_columns_to_size(max_size)
  elsif @pixels.first.length < max_size
    add_transparent_rows_to_size(max_size)
  end
end

#resize(new_size) ⇒ Object



35
36
37
38
39
# File 'lib/visual_qrcode/pixels_handler.rb', line 35

def resize(new_size)
  image.resize "#{new_size}x#{new_size}"
  set_pixels_from_image
  make_square
end

#resize_with_padding(new_size, padding) ⇒ Object



29
30
31
32
33
# File 'lib/visual_qrcode/pixels_handler.rb', line 29

def resize_with_padding(new_size, padding)
  padded_size = new_size - (2 * padding)
  resize(padded_size)
  add_margin(padding)
end

#sizeObject



21
22
23
# File 'lib/visual_qrcode/pixels_handler.rb', line 21

def size
  @pixels.length
end