Skip to content

Commit 5f139d7

Browse files
committed
256 -> 8bit, rework test, rework imgshow
1 parent e28ed63 commit 5f139d7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+287
-419
lines changed

AsciiPixel/Project.toml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,12 @@ ImageCore = "0.9"
1414
julia = "1.6"
1515

1616
[extras]
17-
CoordinateTransformations = "150eb455-5306-5404-9cee-2592286d6298"
1817
ImageIO = "82e4d734-157c-48bb-816b-45c225c6df19"
1918
ImageMagick = "6218d12a-5da1-5696-b52f-db25d2ecc6d1"
20-
ImageTransformations = "02fcd773-0e25-5acc-982a-7f6622650795"
2119
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
2220
ReferenceTests = "324d217c-45ce-50fc-942e-d289b448e8cf"
23-
Rotations = "6038ab10-8711-5258-84ad-4b1120ba62dc"
24-
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
2521
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2622
TestImages = "5e47fb64-e119-507b-a336-dd2b206d9990"
2723

2824
[targets]
29-
test = ["CoordinateTransformations", "ImageIO", "ImageMagick", "ImageTransformations", "OffsetArrays", "Rotations", "ReferenceTests", "SparseArrays", "Test", "TestImages"]
25+
test = ["ImageIO", "ImageMagick", "OffsetArrays", "ReferenceTests", "Test", "TestImages"]

AsciiPixel/src/AsciiPixel.jl

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,31 @@ export ascii_encode
99
include("colorant2ansi.jl")
1010
include("ascii_encode.jl")
1111

12-
const colormode = Ref{TermColorDepth}(TermColor256())
12+
const colormode = Ref{TermColorDepth}(TermColor8bit())
1313

1414
"""
15-
use_256()
15+
set_colordepth(bit::Int)
1616
17-
Triggers `ascii_encode256` (256 colors, 8bit) automatically if an array of colorants is to
18-
be displayed in the julia REPL. (This is the default)
17+
Sets the terminal color depth to the given argument.
1918
"""
20-
use_256() = (colormode[] = TermColor256())
21-
22-
"""
23-
use_24bit()
24-
25-
Triggers `ascii_encode24bit` automatically if an array of colorants is to
26-
be displayed in the julia REPL.
27-
Call `AsciiPixel.use_256()` to restore default behaviour.
28-
"""
29-
use_24bit() = (colormode[] = TermColor24bit())
19+
function set_colordepth(bit::Int)
20+
if bit == 8
21+
colormode[] = TermColor8bit()
22+
elseif bit == 24
23+
colormode[] = TermColor24bit()
24+
else
25+
error("Setting color depth to $bit-bit is not supported, valid mode are:
26+
- 8bit (256 colors)
27+
- 24bit")
28+
end
29+
colormode[]
30+
end
31+
32+
set_colordepth(bit::AbstractString) = set_colordepth(parse(Int, replace(bit, r"[^0-9]"=>"")))
33+
34+
function __init__()
35+
# use 24bit if the terminal supports it
36+
lowercase(get(ENV, "COLORTERM", "")) in ("24bit", "truecolor") && set_colordepth(24)
37+
end
3038

3139
end # module

AsciiPixel/src/ascii_encode.jl

Lines changed: 3 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ ansi terminal colors.
1818
- The encoder `enc` specifies which kind of unicode represenation
1919
should be used.
2020
21-
- The `colordepth` can either be `TermColor256()` or `TermColor24bit()`
21+
- The `colordepth` can either be `TermColor8bit()` or `TermColor24bit()`
2222
and specifies which terminal color codes should be used.
2323
2424
- `maxheight` and `maxwidth` specify the maximum numbers of
@@ -152,8 +152,7 @@ end
152152
"""
153153
ascii_encode([stream], img, [depth::TermColorDepth], [maxsize])
154154
155-
Displays the given image `img` using unicode characters and
156-
terminal colors (defaults to 256 colors).
155+
Displays the given image `img` using unicode characters and terminal colors.
157156
`img` has to be an array of `Colorant`.
158157
159158
If working in the REPL, the function tries to choose the encoding
@@ -193,55 +192,4 @@ function ascii_encode(
193192
end
194193
end
195194

196-
"""
197-
ascii_encode([stream], img, [depth::TermColorDepth], [maxsize])
198-
199-
Displays the given image `img` using unicode characters and
200-
terminal colors (defaults to 256 colors).
201-
`img` has to be an array of `Colorant`.
202-
203-
If working in the REPL, the function tries to choose the encoding
204-
based on the current display size. The image will also be
205-
downsampled to fit into the display (using `restrict`).
206-
"""
207-
function ascii_encode(
208-
io::IO,
209-
img::AbstractArray{<:Colorant},
210-
colordepth::TermColorDepth,
211-
maxsize::Tuple = displaysize(io))
212-
# otherwise, use our own implementation
213-
print_matrix(io, x) = ascii_encode(io, x, colordepth, maxsize)
214-
Base.show_nd(io, img, print_matrix, true)
215-
end
216-
217-
ascii_encode(io::IO, img, args...) = ascii_encode(io, img, colormode[], args...)
218-
ascii_encode(img, args...) = ascii_encode(stdout, img, colormode[], args...)
219-
ascii_encode(io::IO, img, colordepth::TermColorDepth, args...) = throw(ArgumentError("imshow only supports colorant arrays with 1 or 2 dimensions"))
220-
221-
"""
222-
ascii_encode256([stream], img, [maxsize])
223-
224-
Displays the given image `img` using unicode characters and
225-
the widely supported 256 terminal colors.
226-
`img` has to be an array of `Colorant`.
227-
228-
If working in the REPL, the function tries to choose the encoding
229-
based on the current display size. The image will also be
230-
downsampled to fit into the display (using `restrict`).
231-
"""
232-
ascii_encode256(io::IO, img, args...) = ascii_encode(io, img, TermColor256(), args...)
233-
ascii_encode256(img, args...) = ascii_encode256(stdout, img, args...)
234-
235-
"""
236-
ascii_encode24bit([stream], img, [maxsize])
237-
238-
Displays the given image `img` using unicode characters and
239-
the 24 terminal colors that some modern terminals support.
240-
`img` has to be an array of `Colorant`.
241-
242-
If working in the REPL, the function tries to choose the encoding
243-
based on the current display size. The image will also be
244-
downsampled to fit into the display (using `restrict`).
245-
"""
246-
ascii_encode24bit(io::IO, img, args...) = ascii_encode(io, img, TermColor24bit(), args...)
247-
ascii_encode24bit(img, args...) = ascii_encode24bit(stdout, img, args...)
195+
ascii_encode(io::IO, img::AbstractArray{<:Colorant}) = ascii_encode(io, img, colormode[])

AsciiPixel/src/colorant2ansi.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
abstract type TermColorDepth end
2-
struct TermColor256 <: TermColorDepth end
2+
struct TermColor8bit <: TermColorDepth end
33
struct TermColor24bit <: TermColorDepth end
44

55
"""
@@ -24,16 +24,16 @@ julia> colorant2ansi(Gray(.5))
2424
244
2525
```
2626
"""
27-
colorant2ansi(color) = _colorant2ansi(color, TermColor256())
27+
colorant2ansi(color) = _colorant2ansi(color, TermColor8bit())
2828

2929
# Fallback for non-rgb and transparent colors (convert to rgb)
3030
_colorant2ansi(gr::Color, colordepth::TermColorDepth) =
3131
_colorant2ansi(convert(RGB, gr), colordepth)
3232
_colorant2ansi(gr::TransparentColor, colordepth::TermColorDepth) =
3333
_colorant2ansi(color(gr), colordepth)
3434

35-
# 256 colors
36-
function _colorant2ansi(col::AbstractRGB, ::TermColor256)
35+
# 8bit (256) colors
36+
function _colorant2ansi(col::AbstractRGB, ::TermColor8bit)
3737
r, g, b = clamp01nan(red(col)), clamp01nan(green(col)), clamp01nan(blue(col))
3838
r24, g24, b24 = map(c->round(Int, 23c), (r, g, b))
3939
if r24 == g24 == b24
@@ -46,7 +46,7 @@ function _colorant2ansi(col::AbstractRGB, ::TermColor256)
4646
end
4747
end
4848

49-
_colorant2ansi(gr::Color{<:Any,1}, ::TermColor256) = round(Int, 232 + 23clamp01nan(real(gr)))
49+
_colorant2ansi(gr::Color{<:Any,1}, ::TermColor8bit) = round(Int, 232 + 23clamp01nan(real(gr)))
5050

5151
# 24 bit colors
5252
function _colorant2ansi(col::AbstractRGB, ::TermColor24bit)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

AsciiPixel/test/reference/lighthouse_rotated.txt

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)