Skip to content

Commit

Permalink
addressing color palette issue in issue red-data-tools#34
Browse files Browse the repository at this point in the history
  • Loading branch information
nanobowers committed Dec 31, 2020
1 parent 793f5e9 commit ce14312
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
39 changes: 39 additions & 0 deletions example/issue34.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/unicode_plot/braille_canvas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/unicode_plot/lookup_canvas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/unicode_plot/styled_printer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit ce14312

Please sign in to comment.