Skip to content

Commit 2863903

Browse files
committed
1 parent f725c88 commit 2863903

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

lib/unicode_plot/braille_canvas.rb

+12-8
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,21 @@ def pixel!(pixel_x, pixel_y, color)
3636
pixel_x -= 1 unless pixel_x < pixel_width
3737
pixel_y -= 1 unless pixel_y < pixel_height
3838
tx = pixel_x.fdiv(pixel_width) * width
39-
char_x = tx.floor + 1
40-
char_x_off = pixel_x % X_PIXEL_PER_CHAR + 1
41-
char_x += 1 if char_x < tx.round + 1 && char_x_off == 1
4239

43-
char_y = (pixel_y.fdiv(pixel_height) * height).floor + 1
44-
char_y_off = pixel_y % Y_PIXEL_PER_CHAR + 1
40+
char_x = tx.floor
41+
char_x_off = pixel_x % X_PIXEL_PER_CHAR
42+
char_x += 1 if char_x < tx.round && char_x_off == 0
4543

46-
index = index_at(char_x - 1, char_y - 1)
44+
char_y_off = pixel_y % Y_PIXEL_PER_CHAR
45+
char_y = (pixel_y - char_y_off) / Y_PIXEL_PER_CHAR
46+
47+
index = index_at(char_x, char_y)
4748
if index
48-
@grid[index] = (@grid[index].ord | BRAILLE_SIGNS[char_x_off - 1][char_y_off - 1]).chr(Encoding::UTF_8)
49-
@colors[index] |= COLOR_ENCODE[color]
49+
@grid[index] = (@grid[index].ord | BRAILLE_SIGNS[char_x_off][char_y_off]).chr(Encoding::UTF_8)
50+
# If we can fetch color from color-encode then or with the existing color.
51+
# this works for cases where color is 0..7 and we implement a 'mixer'
52+
# if color is beyond 7, then use it directly.
53+
@colors[index] |= COLOR_ENCODE.fetch(color, color)
5054
end
5155
color
5256
end

lib/unicode_plot/lookup_canvas.rb

+8-8
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ def pixel!(pixel_x, pixel_y, color)
1919
pixel_y -= 1 unless pixel_y < pixel_height
2020

2121
tx = pixel_x.fdiv(pixel_width) * width
22-
char_x = tx.floor + 1
23-
char_x_off = pixel_x % x_pixel_per_char + 1
24-
char_x += 1 if char_x < tx.round + 1 && char_x_off == 1
22+
char_x = tx.floor
23+
char_x_off = pixel_x % x_pixel_per_char
24+
char_x += 1 if char_x < tx.round && char_x_off == 0
2525

26-
char_y = (pixel_y.fdiv(pixel_height) * height).floor + 1
27-
char_y_off = pixel_y % y_pixel_per_char + 1
26+
char_y_off = pixel_y % y_pixel_per_char
27+
char_y = (pixel_y - char_y_off) / y_pixel_per_char
2828

29-
index = index_at(char_x - 1, char_y - 1)
29+
index = index_at(char_x, char_y)
3030
if index
31-
@grid[index] |= lookup_encode(char_x_off - 1, char_y_off - 1)
32-
@colors[index] |= COLOR_ENCODE[color]
31+
@grid[index] |= lookup_encode(char_x_off, char_y_off)
32+
@colors[index] |= COLOR_ENCODE.fetch(color, color)
3333
end
3434
end
3535

lib/unicode_plot/styled_printer.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def print_styled(out, *args, bold: false, color: :normal)
8080
end
8181

8282
def print_color(out, color, *args)
83-
color = COLOR_DECODE[color]
83+
color = COLOR_DECODE.fetch(color, color)
8484
print_styled(out, *args, color: color)
8585
end
8686

0 commit comments

Comments
 (0)