diff --git a/example/issue34.rb b/example/issue34.rb new file mode 100644 index 0000000..68269fd --- /dev/null +++ b/example/issue34.rb @@ -0,0 +1,39 @@ +#!/bin/env ruby + +$LOAD_PATH << "#{__dir__}/../lib" +require "unicode_plot" + +# +# Example of using the 256 color pallette +# + +# dummy plot-line to set canvas +plt = UnicodePlot.lineplot([0,0],[0,0], title: "256-color", color: 0, + xlim: [0,31], ylim: [0,31], + width: 33, height: 32, canvas: :braille) +# sweep some colored lines using 256 color palette +(0..255).each do |colornum| + x1 = (colornum / 32).floor * 4 + x2 = x1 + 3 + y = colornum % 32 + UnicodePlot.lineplot!(plt, [x1, x2],[y, y], color: colornum) +end +plt.render + +# +# Example of using standard 8-color pallete with line-plots and named colors +# When lines cross over each other, some 'mixing' effect happens that will +# blend colors by OR'ing their value. +# +# Noting that 16-color cannot be accessed with line-plot, as +# the ':light_*' colors and ':black' are not accepted. + +colorlist = %i[ normal red green yellow blue magenta cyan white ] +plt = UnicodePlot.lineplot([0,0],[0,0], title: "8-color-mixing", color: 0, + xlim: [0,15], ylim: [0,15], + width: 33, height: 16, canvas: :braille) +colorlist.each_with_index do |color, y| + UnicodePlot.lineplot!(plt, [0, 15],[y*2, y*2], color: color) + UnicodePlot.lineplot!(plt, [y*2, y*2],[0, 15], color: color) +end +plt.render diff --git a/lib/unicode_plot/braille_canvas.rb b/lib/unicode_plot/braille_canvas.rb index b509745..979fd3a 100644 --- a/lib/unicode_plot/braille_canvas.rb +++ b/lib/unicode_plot/braille_canvas.rb @@ -46,7 +46,7 @@ def pixel!(pixel_x, pixel_y, color) index = index_at(char_x - 1, char_y - 1) if index @grid[index] = (@grid[index].ord | BRAILLE_SIGNS[char_x_off - 1][char_y_off - 1]).chr(Encoding::UTF_8) - @colors[index] |= COLOR_ENCODE[color] + @colors[index] |= COLOR_ENCODE.fetch(color, color) end color end diff --git a/lib/unicode_plot/lookup_canvas.rb b/lib/unicode_plot/lookup_canvas.rb index 01eb768..9e65482 100644 --- a/lib/unicode_plot/lookup_canvas.rb +++ b/lib/unicode_plot/lookup_canvas.rb @@ -29,7 +29,7 @@ def pixel!(pixel_x, pixel_y, color) 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] + @colors[index] |= COLOR_ENCODE.fetch(color, color) end end diff --git a/lib/unicode_plot/styled_printer.rb b/lib/unicode_plot/styled_printer.rb index b3ad81c..df8bbb8 100644 --- a/lib/unicode_plot/styled_printer.rb +++ b/lib/unicode_plot/styled_printer.rb @@ -80,7 +80,7 @@ def print_styled(out, *args, bold: false, color: :normal) end def print_color(out, color, *args) - color = COLOR_DECODE[color] + color = COLOR_DECODE.fetch(color, color) print_styled(out, *args, color: color) end