Skip to content

Addressing color palette issue in issue #34 #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an example script to show usage of 256-color palette and 8-color (with mixing).


$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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is that if color is a symbol (e.g. :blue), then it will fetch a number. If color is a number it will fail to find in the Hash, and just pass the color through.

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