Skip to content

Commit ce14312

Browse files
committed
addressing color palette issue in issue red-data-tools#34
1 parent 793f5e9 commit ce14312

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

example/issue34.rb

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/env ruby
2+
3+
$LOAD_PATH << "#{__dir__}/../lib"
4+
require "unicode_plot"
5+
6+
#
7+
# Example of using the 256 color pallette
8+
#
9+
10+
# dummy plot-line to set canvas
11+
plt = UnicodePlot.lineplot([0,0],[0,0], title: "256-color", color: 0,
12+
xlim: [0,31], ylim: [0,31],
13+
width: 33, height: 32, canvas: :braille)
14+
# sweep some colored lines using 256 color palette
15+
(0..255).each do |colornum|
16+
x1 = (colornum / 32).floor * 4
17+
x2 = x1 + 3
18+
y = colornum % 32
19+
UnicodePlot.lineplot!(plt, [x1, x2],[y, y], color: colornum)
20+
end
21+
plt.render
22+
23+
#
24+
# Example of using standard 8-color pallete with line-plots and named colors
25+
# When lines cross over each other, some 'mixing' effect happens that will
26+
# blend colors by OR'ing their value.
27+
#
28+
# Noting that 16-color cannot be accessed with line-plot, as
29+
# the ':light_*' colors and ':black' are not accepted.
30+
31+
colorlist = %i[ normal red green yellow blue magenta cyan white ]
32+
plt = UnicodePlot.lineplot([0,0],[0,0], title: "8-color-mixing", color: 0,
33+
xlim: [0,15], ylim: [0,15],
34+
width: 33, height: 16, canvas: :braille)
35+
colorlist.each_with_index do |color, y|
36+
UnicodePlot.lineplot!(plt, [0, 15],[y*2, y*2], color: color)
37+
UnicodePlot.lineplot!(plt, [y*2, y*2],[0, 15], color: color)
38+
end
39+
plt.render

lib/unicode_plot/braille_canvas.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def pixel!(pixel_x, pixel_y, color)
4646
index = index_at(char_x - 1, char_y - 1)
4747
if index
4848
@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+
@colors[index] |= COLOR_ENCODE.fetch(color, color)
5050
end
5151
color
5252
end

lib/unicode_plot/lookup_canvas.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def pixel!(pixel_x, pixel_y, color)
2929
index = index_at(char_x - 1, char_y - 1)
3030
if index
3131
@grid[index] |= lookup_encode(char_x_off - 1, char_y_off - 1)
32-
@colors[index] |= COLOR_ENCODE[color]
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)