From c988dc276e252764d8a439b3e1996371b29ca795 Mon Sep 17 00:00:00 2001 From: t-bltg Date: Thu, 11 Aug 2022 12:02:25 +0200 Subject: [PATCH] `ascii_display` -> `ascii_show` - fix downscaling - docs - cov (#8) * rework for `ReferenceTests` * fix uncovered cond --- .JuliaFormatter.toml | 1 + Project.toml | 2 +- src/XTermColors.jl | 2 +- src/ascii.jl | 83 +++++---- src/colorant2ansi.jl | 11 +- ...x80_24bit.txt => show_bar_80x80_24bit.txt} | 0 ...80x80_8bit.txt => show_bar_80x80_8bit.txt} | 0 .../show_lighthouse_20_auto_8bit.txt | 1 + .../reference/show_lighthouse_20_big_8bit.txt | 1 + .../show_lighthouse_20_small_8bit.txt | 1 + ...xt => show_lighthouse_20x20_auto_8bit.txt} | 0 .../show_lighthouse_20x20_big_8bit.txt | 9 + .../show_lighthouse_20x20_small_8bit.txt | 8 + .../show_lighthouse_80_auto_8bit.txt | 1 + .../reference/show_lighthouse_80_big_8bit.txt | 1 + .../show_lighthouse_80_small_8bit.txt | 1 + ...xt => show_lighthouse_80x80_auto_8bit.txt} | 0 .../show_lighthouse_80x80_big_8bit.txt | 30 ++++ .../show_lighthouse_80x80_small_8bit.txt | 15 ++ test/reference/show_mandril_20_auto_24bit.txt | 1 + test/reference/show_mandril_20_big_24bit.txt | 1 + .../reference/show_mandril_20_small_24bit.txt | 1 + ....txt => show_mandril_20x20_auto_24bit.txt} | 0 .../show_mandril_20x20_big_24bit.txt | 9 + .../show_mandril_20x20_small_24bit.txt | 8 + test/reference/show_mandril_80_auto_24bit.txt | 1 + test/reference/show_mandril_80_big_24bit.txt | 1 + .../reference/show_mandril_80_small_24bit.txt | 1 + ....txt => show_mandril_80x80_auto_24bit.txt} | 0 .../show_mandril_80x80_big_24bit.txt | 30 ++++ .../show_mandril_80x80_small_24bit.txt | 15 ++ ...0_24bit.txt => show_wheel_80x80_24bit.txt} | 0 ...x80_8bit.txt => show_wheel_80x80_8bit.txt} | 0 test/runtests.jl | 2 +- test/tst_ascii.jl | 49 +++--- test/tst_colorant2ansi.jl | 166 +++++++++--------- 36 files changed, 299 insertions(+), 153 deletions(-) rename test/reference/{display_bar_80x80_24bit.txt => show_bar_80x80_24bit.txt} (100%) rename test/reference/{display_bar_80x80_8bit.txt => show_bar_80x80_8bit.txt} (100%) create mode 100644 test/reference/show_lighthouse_20_auto_8bit.txt create mode 100644 test/reference/show_lighthouse_20_big_8bit.txt create mode 100644 test/reference/show_lighthouse_20_small_8bit.txt rename test/reference/{display_lighthouse_20x20_8bit.txt => show_lighthouse_20x20_auto_8bit.txt} (100%) create mode 100644 test/reference/show_lighthouse_20x20_big_8bit.txt create mode 100644 test/reference/show_lighthouse_20x20_small_8bit.txt create mode 100644 test/reference/show_lighthouse_80_auto_8bit.txt create mode 100644 test/reference/show_lighthouse_80_big_8bit.txt create mode 100644 test/reference/show_lighthouse_80_small_8bit.txt rename test/reference/{display_lighthouse_80x80_8bit.txt => show_lighthouse_80x80_auto_8bit.txt} (100%) create mode 100644 test/reference/show_lighthouse_80x80_big_8bit.txt create mode 100644 test/reference/show_lighthouse_80x80_small_8bit.txt create mode 100644 test/reference/show_mandril_20_auto_24bit.txt create mode 100644 test/reference/show_mandril_20_big_24bit.txt create mode 100644 test/reference/show_mandril_20_small_24bit.txt rename test/reference/{display_mandril_20x20_24bit.txt => show_mandril_20x20_auto_24bit.txt} (100%) create mode 100644 test/reference/show_mandril_20x20_big_24bit.txt create mode 100644 test/reference/show_mandril_20x20_small_24bit.txt create mode 100644 test/reference/show_mandril_80_auto_24bit.txt create mode 100644 test/reference/show_mandril_80_big_24bit.txt create mode 100644 test/reference/show_mandril_80_small_24bit.txt rename test/reference/{display_mandril_80x80_24bit.txt => show_mandril_80x80_auto_24bit.txt} (100%) create mode 100644 test/reference/show_mandril_80x80_big_24bit.txt create mode 100644 test/reference/show_mandril_80x80_small_24bit.txt rename test/reference/{display_wheel_80x80_24bit.txt => show_wheel_80x80_24bit.txt} (100%) rename test/reference/{display_wheel_80x80_8bit.txt => show_wheel_80x80_8bit.txt} (100%) diff --git a/.JuliaFormatter.toml b/.JuliaFormatter.toml index 2f20158..d4bfeee 100644 --- a/.JuliaFormatter.toml +++ b/.JuliaFormatter.toml @@ -1,5 +1,6 @@ style = "blue" short_to_long_function_def = false +long_to_short_function_def = true trailing_comma = "nothing" always_use_return = false import_to_using = false diff --git a/Project.toml b/Project.toml index bd8bd03..3d555fa 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "XTermColors" uuid = "c8c2cc18-de81-4e68-b407-38a3a0c0491f" -version = "0.1.1" +version = "0.2.0" [deps] Crayons = "a8cc5b0e-0ffa-5ad4-8c14-923d3ee1735f" diff --git a/src/XTermColors.jl b/src/XTermColors.jl index 3672c46..1ae2c2d 100644 --- a/src/XTermColors.jl +++ b/src/XTermColors.jl @@ -5,7 +5,7 @@ using Crayons import OffsetArrays: Origin -export ascii_display +export ascii_show include("colorant2ansi.jl") include("ascii.jl") diff --git a/src/ascii.jl b/src/ascii.jl index b3261e9..d7f2353 100644 --- a/src/ascii.jl +++ b/src/ascii.jl @@ -11,8 +11,9 @@ const RESET = Crayon(; reset=true) function _charof(alpha) alpha_chars = ALPHA_CHARS[] - idx = round(Int, alpha * (length(alpha_chars) - 1)) - alpha_chars[clamp(idx + 1, 1, length(alpha_chars))] + nc = length(alpha_chars) + idx = round(Int, alpha * (nc - 1)) + alpha_chars[clamp(idx + 1, 1, nc)] end function _downscale_small(img::AbstractMatrix{<:Colorant}, maxsize::NTuple{2,Int}) @@ -27,20 +28,20 @@ function _downscale_small(img::AbstractMatrix{<:Colorant}, maxsize::NTuple{2,Int b. number of visible characters per line (the remaining are colorcodes). =# maxheight, maxwidth = max.(maxsize, 5) - h, w = map(length, axes(img)) + h, w = size(img) while ceil(h / 2) > maxheight || w > maxwidth img = restrict(img) - h, w = map(length, axes(img)) + h, w = size(img) end img, SmallBlocks((length(1:2:h), w)) end function _downscale_big(img::AbstractMatrix{<:Colorant}, maxsize::NTuple{2,Int}) maxheight, maxwidth = max.(maxsize, 5) - h, w = map(length, axes(img)) + h, w = size(img) while h > maxheight || 2w > maxwidth img = restrict(img) - h, w = map(length, axes(img)) + h, w = size(img) end img, BigBlocks((h, 2w)) end @@ -72,20 +73,21 @@ function _printc(io::IO, x::Crayon, args...) end """ - ascii_encode([io::IO], enc::ImageEncoder, colordepth::TermColorDepth, img, [maxheight], [maxwidth]) + ascii_encode(io::IO, enc::ImageEncoder, colordepth::TermColorDepth, img; trail_nl::Bool=false, ret::Bool=false) Transforms the pixel of the given image `img`, which has to be an array of `Colorant`, into a string of unicode characters using ansi terminal colors or directly writes into a i/o stream. -- The encoder `enc` specifies which kind of unicode represenation - should be used. +- The encoder `enc` specifies which kind of unicode represenation should be used. - The `colordepth` can either be `TermColor8bit()` or `TermColor24bit()` and specifies which terminal color codes should be used. It `ret` is set, the function returns a vector of strings containing the encoded image. Each element represent one line. The lines do not contain newline characters. + +If `trail_nl` is given, a final trailing newline is appended. """ function ascii_encode( io::IO, @@ -190,51 +192,68 @@ function ascii_encode( ret ? readlines(io) : nothing end -# use a `PipeBuffer` as io and returns encoded data reading lines of this buffer (using `readlines(io)`) -ascii_encode(enc::SmallBlocks, args...) = ascii_encode(PipeBuffer(), enc, args...; ret=true) +color_buffer() = IOContext(PipeBuffer(), :color => Base.get_have_color()) -ascii_encode(enc::BigBlocks, args...) = ascii_encode(PipeBuffer(), enc, args...; ret=true) +# use a `PipeBuffer` as io and returns encoded data reading lines of this buffer (using `readlines(io)`) +ascii_encode(enc::SmallBlocks, args...; kws...) = + ascii_encode(color_buffer(), enc, args...; ret=true, kws...) +ascii_encode(enc::BigBlocks, args...; kws...) = + ascii_encode(color_buffer(), enc, args...; ret=true, kws...) """ - ascii_display([stream], img, [depth::TermColorDepth], [maxsize]) + ascii_show(stream, img, colordepth::TermColorDepth, encoder::Symbol=:auto, maxsize::Tuple=displaysize(io)) Displays the given image `img` using unicode characters and terminal colors. `img` has to be an array of `Colorant`. -- `maxheight` and `maxwidth` specify the maximum numbers of - string characters that should be used for the resulting image. +- `maxsize` specifies the maximum numbers of string characters (lines, columns) + that should be used for the resulting image. Larger images are downscaled automatically using `restrict`. -If working in the REPL, the function tries to choose the encoding -based on the current display size. The image will also be -downsampled to fit into the display (using `restrict`). +If working in the REPL, the function tries to choose the encoding based on the current display size. +The image will also be downsampled to fit into the display (using `restrict`). """ -function ascii_display( +function ascii_show( io::IO, img::AbstractMatrix{<:Colorant}, colordepth::TermColorDepth, + encoder::Symbol=:auto, maxsize::Tuple=displaysize(io); - kwargs... + kws... ) io_h, io_w = maxsize - img_h, img_w = map(length, axes(img)) - downscale = img_h ≤ io_h - 4 && 2img_w ≤ io_w ? _downscale_big : _downscale_small - img, enc = downscale(img, (io_h - 4, io_w)) - ascii_encode(io, enc, colordepth, img; kwargs...) - io + downscale = if encoder === :auto + img_h, img_w = size(img) + img_h ≤ io_h && 2img_w ≤ io_w ? _downscale_big : _downscale_small + elseif encoder === :small + _downscale_small + elseif encoder === :big + _downscale_big + end + img, enc = downscale(img, (io_h, io_w)) + ascii_encode(io, enc, colordepth, img; kws...) end -function ascii_display( +function ascii_show( io::IO, img::AbstractVector{<:Colorant}, colordepth::TermColorDepth, + encoder::Symbol=:auto, maxsize::Tuple=displaysize(io); - kwargs... + kws... ) - io_h, io_w = maxsize - img_w = length(img) - downscale = 3img_w ≤ io_w ? _downscale_big : _downscale_small + _, io_w = maxsize + downscale = if encoder === :auto + img_w = length(img) + 3img_w ≤ io_w ? _downscale_big : _downscale_small + elseif encoder === :small + _downscale_small + elseif encoder === :big + _downscale_big + end img, enc = downscale(img, io_w) - ascii_encode(io, enc, colordepth, img; kwargs...) - io + ascii_encode(io, enc, colordepth, img; kws...) end + +ascii_show(img::AbstractArray{<:Colorant}, args...; kws...) = + ascii_show(color_buffer(), img, args...; ret=true, kws...) diff --git a/src/colorant2ansi.jl b/src/colorant2ansi.jl index abb1553..8ad3931 100644 --- a/src/colorant2ansi.jl +++ b/src/colorant2ansi.jl @@ -32,16 +32,11 @@ colorant2ansi(color) = _colorant2ansi(color, TermColor8bit()) function _colorant2ansi( col::Union{AbstractRGB,AbstractGray,Color{<:Any,1}}, ::TermColor8bit ) - r, g, b = rgb = clamped01_rgb(col) - cube = 16 + 36trunc(Int, 5r) + 6trunc(Int, 5g) + trunc(Int, 5b) # 216 colors levels: cube 6x6x6 + r, g, b = clamped01_rgb(col) if r == g == b - if r % 43 == 16 # prefer cube gray levels: 16, 59, 102, 145, 188, 231 - cube - else - 232 + clamp(trunc(Int, (23 + 2)r), 0, 23) # 24 gray levels + black + white = 26 - end + 232 + clamp(trunc(Int, (23 + 2)r), 0, 23) # 24 gray levels + black + white = 26 else - cube + 16 + 36trunc(Int, 5r) + 6trunc(Int, 5g) + trunc(Int, 5b) # 216 colors levels: cube 6x6x6 end end _colorant2ansi(col::Union{AbstractRGB,AbstractGray,Color{<:Any,1}}, ::TermColor24bit) = diff --git a/test/reference/display_bar_80x80_24bit.txt b/test/reference/show_bar_80x80_24bit.txt similarity index 100% rename from test/reference/display_bar_80x80_24bit.txt rename to test/reference/show_bar_80x80_24bit.txt diff --git a/test/reference/display_bar_80x80_8bit.txt b/test/reference/show_bar_80x80_8bit.txt similarity index 100% rename from test/reference/display_bar_80x80_8bit.txt rename to test/reference/show_bar_80x80_8bit.txt diff --git a/test/reference/show_lighthouse_20_auto_8bit.txt b/test/reference/show_lighthouse_20_auto_8bit.txt new file mode 100644 index 0000000..ad7b77b --- /dev/null +++ b/test/reference/show_lighthouse_20_auto_8bit.txt @@ -0,0 +1 @@ +████████████████ \ No newline at end of file diff --git a/test/reference/show_lighthouse_20_big_8bit.txt b/test/reference/show_lighthouse_20_big_8bit.txt new file mode 100644 index 0000000..ba8b301 --- /dev/null +++ b/test/reference/show_lighthouse_20_big_8bit.txt @@ -0,0 +1 @@ +██ ██ ██  … ██ ██ ██  \ No newline at end of file diff --git a/test/reference/show_lighthouse_20_small_8bit.txt b/test/reference/show_lighthouse_20_small_8bit.txt new file mode 100644 index 0000000..ad7b77b --- /dev/null +++ b/test/reference/show_lighthouse_20_small_8bit.txt @@ -0,0 +1 @@ +████████████████ \ No newline at end of file diff --git a/test/reference/display_lighthouse_20x20_8bit.txt b/test/reference/show_lighthouse_20x20_auto_8bit.txt similarity index 100% rename from test/reference/display_lighthouse_20x20_8bit.txt rename to test/reference/show_lighthouse_20x20_auto_8bit.txt diff --git a/test/reference/show_lighthouse_20x20_big_8bit.txt b/test/reference/show_lighthouse_20x20_big_8bit.txt new file mode 100644 index 0000000..1b7e582 --- /dev/null +++ b/test/reference/show_lighthouse_20x20_big_8bit.txt @@ -0,0 +1,9 @@ +██████████████████ +██████████████████ +██████████████████ +██████████████████ +██████████████████ +██████████████████ +██████████████████ +██████████████████ +██████████████████ \ No newline at end of file diff --git a/test/reference/show_lighthouse_20x20_small_8bit.txt b/test/reference/show_lighthouse_20x20_small_8bit.txt new file mode 100644 index 0000000..970cb68 --- /dev/null +++ b/test/reference/show_lighthouse_20x20_small_8bit.txt @@ -0,0 +1,8 @@ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ \ No newline at end of file diff --git a/test/reference/show_lighthouse_80_auto_8bit.txt b/test/reference/show_lighthouse_80_auto_8bit.txt new file mode 100644 index 0000000..7772c6e --- /dev/null +++ b/test/reference/show_lighthouse_80_auto_8bit.txt @@ -0,0 +1 @@ +██████████████████████████████ \ No newline at end of file diff --git a/test/reference/show_lighthouse_80_big_8bit.txt b/test/reference/show_lighthouse_80_big_8bit.txt new file mode 100644 index 0000000..23e6a83 --- /dev/null +++ b/test/reference/show_lighthouse_80_big_8bit.txt @@ -0,0 +1 @@ +██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██  … ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██  \ No newline at end of file diff --git a/test/reference/show_lighthouse_80_small_8bit.txt b/test/reference/show_lighthouse_80_small_8bit.txt new file mode 100644 index 0000000..7772c6e --- /dev/null +++ b/test/reference/show_lighthouse_80_small_8bit.txt @@ -0,0 +1 @@ +██████████████████████████████ \ No newline at end of file diff --git a/test/reference/display_lighthouse_80x80_8bit.txt b/test/reference/show_lighthouse_80x80_auto_8bit.txt similarity index 100% rename from test/reference/display_lighthouse_80x80_8bit.txt rename to test/reference/show_lighthouse_80x80_auto_8bit.txt diff --git a/test/reference/show_lighthouse_80x80_big_8bit.txt b/test/reference/show_lighthouse_80x80_big_8bit.txt new file mode 100644 index 0000000..3d1d854 --- /dev/null +++ b/test/reference/show_lighthouse_80x80_big_8bit.txt @@ -0,0 +1,30 @@ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ \ No newline at end of file diff --git a/test/reference/show_lighthouse_80x80_small_8bit.txt b/test/reference/show_lighthouse_80x80_small_8bit.txt new file mode 100644 index 0000000..e3a7fce --- /dev/null +++ b/test/reference/show_lighthouse_80x80_small_8bit.txt @@ -0,0 +1,15 @@ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ \ No newline at end of file diff --git a/test/reference/show_mandril_20_auto_24bit.txt b/test/reference/show_mandril_20_auto_24bit.txt new file mode 100644 index 0000000..adf4a37 --- /dev/null +++ b/test/reference/show_mandril_20_auto_24bit.txt @@ -0,0 +1 @@ +████████████████ \ No newline at end of file diff --git a/test/reference/show_mandril_20_big_24bit.txt b/test/reference/show_mandril_20_big_24bit.txt new file mode 100644 index 0000000..2d6349d --- /dev/null +++ b/test/reference/show_mandril_20_big_24bit.txt @@ -0,0 +1 @@ +██ ██ ██  … ██ ██ ██  \ No newline at end of file diff --git a/test/reference/show_mandril_20_small_24bit.txt b/test/reference/show_mandril_20_small_24bit.txt new file mode 100644 index 0000000..adf4a37 --- /dev/null +++ b/test/reference/show_mandril_20_small_24bit.txt @@ -0,0 +1 @@ +████████████████ \ No newline at end of file diff --git a/test/reference/display_mandril_20x20_24bit.txt b/test/reference/show_mandril_20x20_auto_24bit.txt similarity index 100% rename from test/reference/display_mandril_20x20_24bit.txt rename to test/reference/show_mandril_20x20_auto_24bit.txt diff --git a/test/reference/show_mandril_20x20_big_24bit.txt b/test/reference/show_mandril_20x20_big_24bit.txt new file mode 100644 index 0000000..457d416 --- /dev/null +++ b/test/reference/show_mandril_20x20_big_24bit.txt @@ -0,0 +1,9 @@ +██████████████████ +██████████████████ +██████████████████ +██████████████████ +██████████████████ +██████████████████ +██████████████████ +██████████████████ +██████████████████ \ No newline at end of file diff --git a/test/reference/show_mandril_20x20_small_24bit.txt b/test/reference/show_mandril_20x20_small_24bit.txt new file mode 100644 index 0000000..aa15262 --- /dev/null +++ b/test/reference/show_mandril_20x20_small_24bit.txt @@ -0,0 +1,8 @@ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ \ No newline at end of file diff --git a/test/reference/show_mandril_80_auto_24bit.txt b/test/reference/show_mandril_80_auto_24bit.txt new file mode 100644 index 0000000..41becb8 --- /dev/null +++ b/test/reference/show_mandril_80_auto_24bit.txt @@ -0,0 +1 @@ +██████████████████████████████ \ No newline at end of file diff --git a/test/reference/show_mandril_80_big_24bit.txt b/test/reference/show_mandril_80_big_24bit.txt new file mode 100644 index 0000000..71ea68b --- /dev/null +++ b/test/reference/show_mandril_80_big_24bit.txt @@ -0,0 +1 @@ +██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██  … ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██  \ No newline at end of file diff --git a/test/reference/show_mandril_80_small_24bit.txt b/test/reference/show_mandril_80_small_24bit.txt new file mode 100644 index 0000000..41becb8 --- /dev/null +++ b/test/reference/show_mandril_80_small_24bit.txt @@ -0,0 +1 @@ +██████████████████████████████ \ No newline at end of file diff --git a/test/reference/display_mandril_80x80_24bit.txt b/test/reference/show_mandril_80x80_auto_24bit.txt similarity index 100% rename from test/reference/display_mandril_80x80_24bit.txt rename to test/reference/show_mandril_80x80_auto_24bit.txt diff --git a/test/reference/show_mandril_80x80_big_24bit.txt b/test/reference/show_mandril_80x80_big_24bit.txt new file mode 100644 index 0000000..47efc25 --- /dev/null +++ b/test/reference/show_mandril_80x80_big_24bit.txt @@ -0,0 +1,30 @@ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████ \ No newline at end of file diff --git a/test/reference/show_mandril_80x80_small_24bit.txt b/test/reference/show_mandril_80x80_small_24bit.txt new file mode 100644 index 0000000..b863b4e --- /dev/null +++ b/test/reference/show_mandril_80x80_small_24bit.txt @@ -0,0 +1,15 @@ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ +▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ \ No newline at end of file diff --git a/test/reference/display_wheel_80x80_24bit.txt b/test/reference/show_wheel_80x80_24bit.txt similarity index 100% rename from test/reference/display_wheel_80x80_24bit.txt rename to test/reference/show_wheel_80x80_24bit.txt diff --git a/test/reference/display_wheel_80x80_8bit.txt b/test/reference/show_wheel_80x80_8bit.txt similarity index 100% rename from test/reference/display_wheel_80x80_8bit.txt rename to test/reference/show_wheel_80x80_8bit.txt diff --git a/test/runtests.jl b/test/runtests.jl index 97f139d..b97d046 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -2,7 +2,7 @@ using Test, TestImages, ReferenceTests using ImageTransformations using OffsetArrays using XTermColors -using ImageBase +using ImageBase # for `N0f8` import XTermColors: TermColorDepth, TermColor8bit, TermColor24bit import XTermColors: ascii_encode, _downscale_small, _downscale_big diff --git a/test/tst_ascii.jl b/test/tst_ascii.jl index b11d648..e2b0a97 100644 --- a/test/tst_ascii.jl +++ b/test/tst_ascii.jl @@ -19,8 +19,7 @@ end ) end @testset "transparent gray square" begin - # alpha is ignored for small block encoding. - # So this yields the exact same results as above. + # alpha is ignored for small block encoding: this yields the exact same results as above. img, enc = _downscale_small(gray_square_alpha, (2, 2)) @test enc.size === (1, 2) @check_enc( @@ -413,28 +412,25 @@ end end end -@testset "ascii_display (frontend interface)" begin +@testset "ascii_show (frontend interface)" begin @testset "lighthouse 8bit" begin img = imresize(lighthouse, (30, 30)) - res = readlines( - @ensurecolor ascii_display(PipeBuffer(), img, TermColor8bit(), (20, 20)) - ) - @test_reference "reference/display_lighthouse_20x20_8bit.txt" res - res = readlines( - @ensurecolor ascii_display(PipeBuffer(), img, TermColor8bit(), (80, 80)) - ) - @test_reference "reference/display_lighthouse_80x80_8bit.txt" res + for sz in [(20, 20), (80, 80)], enc in (:small, :big, :auto) + res = @ensurecolor ascii_show(img, TermColor8bit(), enc, sz) + @test_reference "reference/show_lighthouse_$(sz[1])x$(sz[2])_$(enc)_8bit.txt" res + res = @ensurecolor ascii_show(img[1, :], TermColor8bit(), enc, sz) + @test_reference "reference/show_lighthouse_$(sz[2])_$(enc)_8bit.txt" res + end end @testset "mandril 24bit" begin img = imresize(mandril, (30, 30)) - res = readlines( - @ensurecolor ascii_display(PipeBuffer(), img, TermColor24bit(), (20, 20)) - ) - @test_reference "reference/display_mandril_20x20_24bit.txt" res - res = readlines( - @ensurecolor ascii_display(PipeBuffer(), img, TermColor24bit(), (80, 80)) - ) - @test_reference "reference/display_mandril_80x80_24bit.txt" res + for sz in [(20, 20), (80, 80)], enc in (:small, :big, :auto) + suffix = "mandril_$(sz[1])x$(sz[2])_$(enc)_24bit" + res = @ensurecolor ascii_show(img, TermColor24bit(), enc, sz) + @test_reference "reference/show_mandril_$(sz[1])x$(sz[2])_$(enc)_24bit.txt" res + res = @ensurecolor ascii_show(img[1, :], TermColor24bit(), enc, sz) + @test_reference "reference/show_mandril_$(sz[2])_$(enc)_24bit.txt" res + end end end @@ -443,17 +439,16 @@ end for I in CartesianIndices(wheel) x, y = I.I ./ (size(wheel) .÷ 2) - r = sqrt(x^2 + y^2) - if r < 1 + if (r = sqrt(x^2 + y^2)) < 1 h = atand(x, y) + 90 wheel[I] = RGB(HSV(h, r, 1)) end end for depth in (8, 24) - enc = depth == 8 ? TermColor8bit() : TermColor24bit() - res = readlines(@ensurecolor ascii_display(PipeBuffer(), wheel, enc, (80, 80))) - @test_reference "reference/display_wheel_80x80_$(depth)bit.txt" res + colordepth = depth == 8 ? TermColor8bit() : TermColor24bit() + res = @ensurecolor ascii_show(wheel, colordepth, :auto, (80, 80)) + @test_reference "reference/show_wheel_80x80_$(depth)bit.txt" res end end @@ -461,8 +456,8 @@ end bar = Gray{N0f8}.(repeat(range(0; stop=1, length=64), 1, 2)') for depth in (8, 24) - enc = depth == 8 ? TermColor8bit() : TermColor24bit() - res = readlines(@ensurecolor ascii_display(PipeBuffer(), bar, enc, (80, 80))) - @test_reference "reference/display_bar_80x80_$(depth)bit.txt" res + colordepth = depth == 8 ? TermColor8bit() : TermColor24bit() + res = @ensurecolor ascii_show(bar, colordepth, :auto, (80, 80)) + @test_reference "reference/show_bar_80x80_$(depth)bit.txt" res end end diff --git a/test/tst_colorant2ansi.jl b/test/tst_colorant2ansi.jl index fd27409..0923395 100644 --- a/test/tst_colorant2ansi.jl +++ b/test/tst_colorant2ansi.jl @@ -1,108 +1,108 @@ -@testset "Un-exported Interface" begin - @test supertype(TermColor8bit) <: TermColorDepth - @test supertype(TermColor24bit) <: TermColorDepth +@test supertype(TermColor8bit) <: TermColorDepth +@test supertype(TermColor24bit) <: TermColorDepth - # This tests if the mapping from RGB to the - # 8bit (256) ansi color codes is correct - @testset "8bit colors" begin - @testset "RGB - Gray" begin - for idx in 16:255 - idx % 43 == 16 && continue # skip values mapping to cube levels - hex = XTermColors.TERMCOLOR256_LOOKUP[idx] - col = RGB(reinterpret(RGB24, hex)) - @test _colorant2ansi(col, TermColor8bit()) == idx - end - end - - @testset "decoder" begin - enc = TermColor8bit() +# This tests if the mapping from RGB to the +# 8bit (256) ansi color codes is correct +@testset "8bit colors" begin + enc = TermColor8bit() - @testset "Gray scale" begin - for idx in 232:255 - hex = XTermColors.TERMCOLOR256_LOOKUP[idx] - c = RGB(reinterpret(RGB24, hex)) - @test enc(Gray(red(c))) == c - end - end - - @testset "RGB scale" begin - for idx in 16:231 - idx % 43 == 16 && continue - hex = XTermColors.TERMCOLOR256_LOOKUP[idx] - c = RGB(reinterpret(RGB24, hex)) - @test enc(c) == c - end + @testset "RGB - Gray" begin + for idx in 16:255 + hex = XTermColors.TERMCOLOR256_LOOKUP[idx] + col = RGB(reinterpret(RGB24, hex)) + ansi = _colorant2ansi(col, enc) + if idx % 43 == 16 # cube levels mapping to gray levels instead + @test ansi > 231 + else + @test ansi == idx end end end - # This tests if the mapping from RGB to the 24 bit r g b tuples - # (which are in the set {0,1,...,255}) is correct. - @testset "24 bit colors" begin - @testset "RGB" begin - for col in rand(RGB, 10) - r, g, b = red(col), green(col), blue(col) - ri, gi, bi = map(c -> round(Int, 255c), (r, g, b)) - @test _colorant2ansi(col, TermColor24bit()) === (ri, gi, bi) - end + @testset "Decoder - Gray scale" begin + for idx in 232:255 + hex = XTermColors.TERMCOLOR256_LOOKUP[idx] + c = RGB(reinterpret(RGB24, hex)) + @test enc(Gray(red(c))) == c end - @testset "Gray" begin - for col in rand(Gray, 10) - r = round(Int, 255real(col)) - @test _colorant2ansi(col, TermColor24bit()) === (r, r, r) - end + end + + @testset "Decoder - RGB scale" begin + for idx in 16:231 + idx % 43 == 16 && continue # skip values mapping to cube levels + hex = XTermColors.TERMCOLOR256_LOOKUP[idx] + c = RGB(reinterpret(RGB24, hex)) + @test enc(c) == c end end +end + +# This tests if the mapping from RGB to the 24 bit r g b tuples +# (which are in the set {0,1,...,255}) is correct. +@testset "24 bit colors" begin + enc = TermColor24bit() - # Internally non RGB Colors should be converted to RGB - # This tests if the result reflects that assumption - @testset "Non RGB" begin - for col_rgb in rand(RGB, 10) - col_other = convert(HSV, col_rgb) - @test _colorant2ansi(col_rgb, TermColor24bit()) === - _colorant2ansi(col_other, TermColor24bit()) + @testset "RGB" begin + for col in rand(RGB, 10) + @test enc(col) == col + r, g, b = red(col), green(col), blue(col) + ri, gi, bi = map(c -> round(Int, 255c), (r, g, b)) + @test _colorant2ansi(col, enc) === (ri, gi, bi) end end - # Internally all Alpha Colors should be stripped of their alpha - # channel. This tests if the result reflects that assumption - @testset "TransparentColor" begin - for col in (rand(RGB, 10)..., rand(HSV, 10)...) - acol = alphacolor(col, rand()) - @test _colorant2ansi(col, TermColor24bit()) === - _colorant2ansi(acol, TermColor24bit()) + @testset "Gray" begin + for col in rand(Gray, 10) + @test enc(col) == col + r = round(Int, 255real(col)) + @test _colorant2ansi(col, enc) === (r, r, r) end end end -# Tests that we don't pollute the calling namespace with -# exports that they don't need. -# Also compare functionality against the functions tested above -@testset "Exported Interface" begin - @testset "Validate exported interface boundaries" begin - @test_throws MethodError colorant2ansi(RGB(1.0, 1.0, 1.0), TermColor8bit()) - @test_throws MethodError colorant2ansi(RGB(1.0, 1.0, 1.0), TermColor24bit()) +# Internally non RGB Colors should be converted to RGB +# This tests if the result reflects that assumption. +@testset "Non RGB" begin + for col_rgb in rand(RGB, 10) + col_hsv = convert(HSV, col_rgb) + @test _colorant2ansi(col_rgb, TermColor24bit()) === + _colorant2ansi(col_hsv, TermColor24bit()) end +end - @testset "8bit colors" begin - for col in (rand(RGB, 10)..., rand(Gray, 10)...) - # compare against non-exported interface, - # which we already tested above - @test colorant2ansi(col) === _colorant2ansi(col, TermColor8bit()) - end +# Internally all Alpha Colors should be stripped of their alpha channel. +# This tests if the result reflects that assumption. +@testset "TransparentColor" begin + for col in (rand(RGB, 10)..., rand(HSV, 10)...) + acol = alphacolor(col, rand()) + @test _colorant2ansi(col, TermColor24bit()) === + _colorant2ansi(acol, TermColor24bit()) end +end - # Check if exported interface propagates conversions - @testset "Non RGB" begin - for col_rgb in rand(RGB, 10) - @test colorant2ansi(col_rgb) === colorant2ansi(convert(HSV, col_rgb)) - end +# Compare functionality against the functions tested above +@testset "Validate exported interface boundaries" begin + @test_throws MethodError colorant2ansi(RGB(1.0, 1.0, 1.0), TermColor8bit()) + @test_throws MethodError colorant2ansi(RGB(1.0, 1.0, 1.0), TermColor24bit()) +end + +@testset "8bit colors" begin + for col in (rand(RGB, 10)..., rand(Gray, 10)...) + # compare against non-exported interface, which we already tested above + @test colorant2ansi(col) === _colorant2ansi(col, TermColor8bit()) end +end - # Check if exported interface propagates conversions - @testset "TransparentColor" begin - for col in (rand(RGB, 10)..., rand(HSV, 10)...) - @test colorant2ansi(col) === colorant2ansi(alphacolor(col, rand())) - end +# Check if exported interface propagates conversions +@testset "Non RGB" begin + for col_rgb in rand(RGB, 10) + @test colorant2ansi(col_rgb) === colorant2ansi(convert(HSV, col_rgb)) + end +end + +# Check if exported interface propagates conversions +@testset "TransparentColor" begin + for col in (rand(RGB, 10)..., rand(HSV, 10)...) + @test colorant2ansi(col) === colorant2ansi(alphacolor(col, rand())) end end