Skip to content

Commit 248d381

Browse files
committed
move colorant2ansi from ImageInTerminal to AsciiPixel
Except for small simplification on tests, this commit introduces no functionality changes.
1 parent 795418b commit 248d381

File tree

9 files changed

+37
-25
lines changed

9 files changed

+37
-25
lines changed

AsciiPixel/Project.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
name = "AsciiPixel"
22
uuid = "d64ea8f1-9d40-4cda-bb88-5de2505cd5c8"
33
version = "0.1.0"
4+
5+
[deps]
6+
ImageBase = "c817782e-172a-44cc-b673-b171935fbb9e"
7+
8+
[compat]
9+
ImageBase = "0.1.1"
10+
julia = "1.6"

AsciiPixel/src/AsciiPixel.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module AsciiPixel
22

3-
hello(x) = println("Hello", x)
3+
using ImageBase
4+
5+
include("colorant2ansi.jl")
46

57
end # module
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ _colorant2ansi(gr::Color, colordepth::TermColorDepth) =
3232
_colorant2ansi(gr::TransparentColor, colordepth::TermColorDepth) =
3333
_colorant2ansi(color(gr), colordepth)
3434

35-
# 256 colors
35+
# 8bit (256) colors
3636
function _colorant2ansi(col::AbstractRGB, ::TermColor256)
3737
r, g, b = clamp01nan(red(col)), clamp01nan(green(col)), clamp01nan(blue(col))
3838
r24, g24, b24 = map(c->round(Int, c * 23), (r, g, b))
3939
if r24 == g24 == b24
40-
# Use grayscales because of higher resultion
40+
# Use grayscales because of higher resolution
4141
# This way even grayscale RGB images look good.
4242
232 + r24
4343
else
@@ -58,4 +58,3 @@ function _colorant2ansi(gr::Color{<:Any,1}, ::TermColor24bit)
5858
r = round(Int, clamp01nan(real(gr)) * 255)
5959
r, r, r
6060
end
61-

AsciiPixel/test/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
[deps]
2+
ImageBase = "c817782e-172a-44cc-b673-b171935fbb9e"
23
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@testset "Not exported Interface" begin
2-
@test supertype(ImageInTerminal.TermColor256) <: ImageInTerminal.TermColorDepth
3-
@test supertype(ImageInTerminal.TermColor24bit) <: ImageInTerminal.TermColorDepth
2+
@test supertype(TermColor256) <: TermColorDepth
3+
@test supertype(TermColor24bit) <: TermColorDepth
44

55
# This tests if the mapping from RGB to the
66
# 256 ansi color codes is correct
@@ -18,16 +18,16 @@
1818
r, g, b = red(col), green(col), blue(col)
1919
ri, gi, bi = map(c->round(Int, 23c), (r, g, b))
2020
if ri == gi == bi
21-
@test ImageInTerminal._colorant2ansi(col, ImageInTerminal.TermColor256()) === _ref_col2ansi(r)
21+
@test _colorant2ansi(col, TermColor256()) === _ref_col2ansi(r)
2222
else
23-
@test ImageInTerminal._colorant2ansi(col, ImageInTerminal.TermColor256()) === _ref_col2ansi(r, g, b)
23+
@test _colorant2ansi(col, TermColor256()) === _ref_col2ansi(r, g, b)
2424
end
2525
end
2626
end
2727
@testset "Gray" begin
2828
for col in rand(Gray, 10)
2929
r = real(col)
30-
@test ImageInTerminal._colorant2ansi(col, ImageInTerminal.TermColor256()) === _ref_col2ansi(r)
30+
@test _colorant2ansi(col, TermColor256()) === _ref_col2ansi(r)
3131
end
3232
end
3333
end
@@ -39,13 +39,13 @@
3939
for col in rand(RGB, 10)
4040
r, g, b = red(col), green(col), blue(col)
4141
ri, gi, bi = map(c->round(Int,255c), (r,g,b))
42-
@test ImageInTerminal._colorant2ansi(col, ImageInTerminal.TermColor24bit()) === (ri, gi, bi)
42+
@test _colorant2ansi(col, TermColor24bit()) === (ri, gi, bi)
4343
end
4444
end
4545
@testset "Gray" begin
4646
for col in rand(Gray, 10)
4747
r = round(Int, 255*real(col))
48-
@test ImageInTerminal._colorant2ansi(col, ImageInTerminal.TermColor24bit()) === (r, r, r)
48+
@test _colorant2ansi(col, TermColor24bit()) === (r, r, r)
4949
end
5050
end
5151
end
@@ -55,7 +55,7 @@
5555
@testset "Non RGB" begin
5656
for col_rgb in rand(RGB, 10)
5757
col_other = convert(HSV, col_rgb)
58-
@test ImageInTerminal._colorant2ansi(col_rgb, ImageInTerminal.TermColor24bit()) === ImageInTerminal._colorant2ansi(col_other, ImageInTerminal.TermColor24bit())
58+
@test _colorant2ansi(col_rgb, TermColor24bit()) === _colorant2ansi(col_other, TermColor24bit())
5959
end
6060
end
6161

@@ -64,7 +64,7 @@
6464
@testset "TransparentColor" begin
6565
for col in (rand(RGB, 10)..., rand(HSV, 10)...)
6666
acol = alphacolor(col, rand())
67-
@test ImageInTerminal._colorant2ansi(col, ImageInTerminal.TermColor24bit()) === ImageInTerminal._colorant2ansi(acol, ImageInTerminal.TermColor24bit())
67+
@test _colorant2ansi(col, TermColor24bit()) === _colorant2ansi(acol, TermColor24bit())
6868
end
6969
end
7070
end
@@ -74,17 +74,15 @@ end
7474
# Also compare functionality against the functions tested above
7575
@testset "Exported Interface" begin
7676
@testset "Validate exported interface boundaries" begin
77-
@test_throws UndefVarError TermColor256()
78-
@test_throws UndefVarError TermColor24bit()
79-
@test_throws MethodError colorant2ansi(RGB(1.,1.,1.), ImageInTerminal.TermColor256())
80-
@test_throws MethodError colorant2ansi(RGB(1.,1.,1.), ImageInTerminal.TermColor24bit())
77+
@test_throws MethodError colorant2ansi(RGB(1.,1.,1.), TermColor256())
78+
@test_throws MethodError colorant2ansi(RGB(1.,1.,1.), TermColor24bit())
8179
end
8280

8381
@testset "256 colors" begin
8482
for col in (rand(RGB, 10)..., rand(Gray, 10)...)
8583
# compare against non-exported interface,
8684
# which we already tested above
87-
@test colorant2ansi(col) === ImageInTerminal._colorant2ansi(col, ImageInTerminal.TermColor256())
85+
@test colorant2ansi(col) === _colorant2ansi(col, TermColor256())
8886
end
8987
end
9088

@@ -104,4 +102,3 @@ end
104102
end
105103
end
106104
end
107-

AsciiPixel/test/runtests.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using Test
22
using AsciiPixel
3+
using ImageBase
4+
using AsciiPixel: TermColorDepth, TermColor256, TermColor24bit
5+
using AsciiPixel: colorant2ansi, _colorant2ansi
36

47
@testset "AsciiPixel" begin
5-
@test_nowarn AsciiPixel.hello("AsciiPixel")
6-
@test true
8+
include("colorant2ansi.jl")
79
end

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ uuid = "d8c32880-2388-543b-8c61-d9f865259254"
33
version = "0.4.7"
44

55
[deps]
6+
AsciiPixel = "d64ea8f1-9d40-4cda-bb88-5de2505cd5c8"
67
Crayons = "a8cc5b0e-0ffa-5ad4-8c14-923d3ee1735f"
78
ImageBase = "c817782e-172a-44cc-b673-b171935fbb9e"
89
ImageCore = "a09fc81d-aa75-5fe9-8630-4744c3626534"
910
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
1011

1112
[compat]
13+
AsciiPixel = "0.1"
1214
Crayons = "0.5, 1, 2, 3, 4"
1315
ImageBase = "0.1"
1416
ImageCore = "0.9"

src/ImageInTerminal.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ using Requires
44
using Crayons
55
using ImageCore
66
using ImageBase: restrict
7+
using AsciiPixel
78

89
export
910

@@ -12,7 +13,9 @@ export
1213
imshow256,
1314
imshow24bit
1415

15-
include("colorant2ansi.jl")
16+
using AsciiPixel: TermColorDepth, TermColor256, TermColor24bit
17+
using AsciiPixel: colorant2ansi, _colorant2ansi
18+
1619
include("encodeimg.jl")
1720
include("imshow.jl")
1821

@@ -64,7 +67,7 @@ enable_encoding() = (should_render_image[1] = true)
6467
function Base.show(
6568
io::IO, mime::MIME"text/plain",
6669
img::AbstractArray{<:Colorant})
67-
if should_render_image[1]
70+
if should_render_image[1]
6871
println(io, summary(img), ":")
6972
ImageInTerminal.imshow(io, img, colormode[1])
7073
else
@@ -89,7 +92,7 @@ function __init__()
8992
# use 24bit if the terminal supports it
9093
lowercase(get(ENV, "COLORTERM", "")) in ("24bit", "truecolor") && use_24bit()
9194
enable_encoding()
92-
95+
9396
if VERSION < v"1.6.0-DEV.888" && Sys.iswindows()
9497
# https://discourse.julialang.org/t/image-in-repl-does-not-correct/46359
9598
@warn "ImageInTerminal is not supported for Windows platform: Julia at least v1.6.0 is required."

test/runtests.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ lena = testimage("lena_color_256")
3333
# ====================================================================
3434

3535
tests = [
36-
"tst_colorant2ansi.jl",
3736
"tst_encodeimg.jl",
3837
"tst_imshow.jl",
3938
"tst_baseshow.jl",

0 commit comments

Comments
 (0)