Class: UnicodePlot::LookupCanvas

Inherits:
Canvas
  • Object
show all
Defined in:
lib/unicode_plot/lookup_canvas.rb

Direct Known Subclasses

AsciiCanvas, DotCanvas

Constant Summary

Constants included from StyledPrinter

StyledPrinter::COLOR_DECODE, StyledPrinter::COLOR_ENCODE, StyledPrinter::DISABLE_TEXT_STYLE, StyledPrinter::TEXT_COLORS

Instance Attribute Summary

Attributes inherited from Canvas

#height, #origin_x, #origin_y, #pixel_height, #pixel_width, #plot_height, #plot_width, #width, #x_pixel_per_char, #y_pixel_per_char

Instance Method Summary collapse

Methods inherited from Canvas

#char_at, #color_at, create, #index_at, #line!, #lines!, #point!, #points!, #print, #show

Methods included from BorderPrinter

#print_border_bottom, #print_border_top

Methods included from StyledPrinter

#color?, #print_color, #print_styled

Constructor Details

#initialize(width, height, x_pixel_per_char, y_pixel_per_char, fill_char = 0, **kw) ⇒ LookupCanvas

Returns a new instance of LookupCanvas.



3
4
5
6
7
8
9
10
11
# File 'lib/unicode_plot/lookup_canvas.rb', line 3

def initialize(width, height, x_pixel_per_char, y_pixel_per_char, fill_char=0, **kw)
  super(width, height,
        width * x_pixel_per_char,
        height * y_pixel_per_char,
        fill_char,
        x_pixel_per_char: x_pixel_per_char,
        y_pixel_per_char: y_pixel_per_char,
        **kw)
end

Instance Method Details

#pixel!(pixel_x, pixel_y, color) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/unicode_plot/lookup_canvas.rb', line 13

def pixel!(pixel_x, pixel_y, color)
  unless 0 <= pixel_x && pixel_x <= pixel_width &&
         0 <= pixel_y && pixel_y <= pixel_height
    return color
  end
  pixel_x -= 1 unless pixel_x < pixel_width
  pixel_y -= 1 unless pixel_y < pixel_height

  tx = pixel_x.fdiv(pixel_width) * width
  char_x = tx.floor + 1
  char_x_off = pixel_x % x_pixel_per_char + 1
  char_x += 1 if char_x < tx.round + 1 && char_x_off == 1

  char_y = (pixel_y.fdiv(pixel_height) * height).floor + 1
  char_y_off = pixel_y % y_pixel_per_char + 1

  index = index_at(char_x - 1, char_y - 1)
  if index
    @grid[index] |= lookup_encode(char_x_off - 1, char_y_off - 1)
    @colors[index] |= COLOR_ENCODE[color]
  end
end


36
37
38
39
40
41
42
43
44
# File 'lib/unicode_plot/lookup_canvas.rb', line 36

def print_row(out, row_index)
  unless 0 <= row_index && row_index < height
    raise ArgumentError, "row_index out of bounds"
  end
  y = row_index
  (0 ... width).each do |x|
    print_color(out, color_at(x, y), lookup_decode(char_at(x, y)))
  end
end