Skip to content

Commit

Permalink
Simplify display of colors and arrays of colors
Browse files Browse the repository at this point in the history
This simplifies the display of array elements based on the :typeinfo property.
This also simplifies the display of color components by setting the :typeinfo property.
This prints the elements of gray arrays as real numbers.
  • Loading branch information
kimikage committed Jul 23, 2020
1 parent 967cacb commit 9fc0c6d
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 50 deletions.
72 changes: 53 additions & 19 deletions src/show.jl
Original file line number Diff line number Diff line change
@@ -1,34 +1,66 @@

function show(io::IO, c::Colorant)
show_colorant_string_with_eltype(io, typeof(c))
if get(io, :typeinfo, Any) === typeof(c)
print(io, colorant_string(typeof(c)))
else
show_colorant_string_with_eltype(io, typeof(c))
end
_show_components(io, c)
end

# Special handling for Normed types: don't print the giant type name
function show(io::IO, c::ColorantNormed)
show_colorant_string_with_eltype(io, typeof(c))
if typeof(c) <: Union{RGB24, ARGB32, Gray24} # isempty(typeof(c).parameters) # Nonparametric types
_show_components(io, c) # with trailing "N0f8" unless :compat=>true
function show(io::IO, c::AbstractGray)
if get(io, :typeinfo, Any) === typeof(c)
show(_components_iocontext(io, c), comp1(c))
else
_show_components(IOContext(io, :compact=>true), c)
show_colorant_string_with_eltype(io, typeof(c))
_show_components(io, c)
end
end

function _show_components(io::IO, c::ColorantN{N}) where N
function show(io::IO, c::AbstractGray{Bool})
if get(io, :typeinfo, Any) === typeof(c)
print(io, gray(c) ? '1' : '0')
else
show_colorant_string_with_eltype(io, typeof(c))
print(io, gray(c) ? "(1)" : "(0)")
end
end


@inline function _components_iocontext(io::IO, c::Colorant{T}) where T
if isempty(typeof(c).parameters)
# For non-parametric colors, we do not set :typeinfo since they do not
# explicitly show their element type. Therefore, the suffix "N0f8" is
# displayed in RGB24 etc. unless :compact=>true.
return io
elseif T === Float64
return io
elseif T <: FixedPoint # workaround for FPN v0.8 or earlier
return IOContext(io, :typeinfo => T, :compact => true)
else
return IOContext(io, :typeinfo => T)
end
end

function _show_components(io::IO, c::Colorant{T, N}) where {T, N}
ioc = _components_iocontext(io, c)
comp_n = (comp1, comp2, comp3, comp4, comp5)
print(io, '(')
print(ioc, '(')
for i = 1:N
show(io, (comp_n[i])(c))
print(io, i < N ? ',' : ')') # without spaces
show(ioc, (comp_n[i])(c)::T)
print(ioc, i < N ? ',' : ')') # without spaces
end
end

function Base.showarg(io::IO, a::Array{C}, toplevel) where C<:Colorant
toplevel || print(io, "::")
print(io, "Array{")
show_colorant_string_with_eltype(io, C)
print(io, ",$(ndims(a))}")
toplevel && print(io, " with eltype ", C)
if VERSION < v"1.6.0-DEV.356" # JuliaLang/julia#36107
function Base.showarg(io::IO, a::Array{C}, toplevel) where C<:Colorant
toplevel || print(io, "::")
print(io, "Array{")
show_colorant_string_with_eltype(io, C)
print(io, ',', ndims(a), '}')
is_parametric_fixed = C <: Colorant{<:FixedPoint} && !isempty(C.parameters)
toplevel && is_parametric_fixed && print(io, " with eltype ", C)
end
end


Expand All @@ -46,10 +78,12 @@ colorant_string_with_eltype(io::IO, ::Type{C}) where {C<:Colorant} =

show_colorant_string_with_eltype(io::IO, ::Type{Union{}}) = show(io, Union{})
function show_colorant_string_with_eltype(io::IO, ::Type{C}) where {C<:Colorant}
if !isconcretetype(C) || isempty(C.parameters)
if !isconcretetype(C) # TODO: strip module names
print(io, C)
elseif C === base_colorant_type(C) # non-parametric
print(io, nameof(C))
else
print(io, colorant_string(C))
print(io, nameof(C))
print(io, '{')
showcoloranttype(io, eltype(C))
print(io, '}')
Expand Down
93 changes: 62 additions & 31 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,22 @@ end

@testset "single color" begin
iob = IOBuffer()
cf = RGB{Float32}(0.32218,0.14983,0.87819)
c = convert(RGB{N0f8}, cf)
c16 = RGB{N0f16}(0.32218,0.14983,0.87819)
ca = RGBA{N0f8}(0.32218,0.14983,0.87819,0.99241)
ac = alphacolor(ca)

show(iob, c)
@test String(take!(iob)) == "RGB{N0f8}(0.322,0.149,0.878)"
show(iob, c16)
@test String(take!(iob)) == "RGB{N0f16}(0.32218,0.14983,0.87819)"
show(iob, cf)
@test String(take!(iob)) == "RGB{Float32}(0.32218f0,0.14983f0,0.87819f0)"
show(IOContext(iob, :compact => true), cf)
@test String(take!(iob)) == "RGB{Float32}(0.32218,0.14983,0.87819)"
show(iob, ca)
@test String(take!(iob)) == "RGBA{N0f8}(0.322,0.149,0.878,0.992)"
show(IOContext(iob, :compact => true), ca)
@test String(take!(iob)) == "RGBA{N0f8}(0.322,0.149,0.878,0.992)"
show(iob, ac)
@test String(take!(iob)) == "ARGB{N0f8}(0.322,0.149,0.878,0.992)"
show(IOContext(iob, :compact => true), ac)
@test String(take!(iob)) == "ARGB{N0f8}(0.322,0.149,0.878,0.992)"
show(iob, RGB{N0f8}(0, 1/3, 1))
@test String(take!(iob)) == "RGB{N0f8}(0.0,0.333,1.0)"
show(iob, RGB{N0f16}(0, 1/3, 1))
@test String(take!(iob)) == "RGB{N0f16}(0.0,0.33333,1.0)"
show(iob, RGB{Float64}(0, 1/3, 1))
@test String(take!(iob)) == "RGB{Float64}(0.0,0.3333333333333333,1.0)"
show(IOContext(iob, :compact => true), RGB{Float64}(0, 1/3, 1))
@test String(take!(iob)) == "RGB{Float64}(0.0,0.333333,1.0)"
show(iob, RGBA{N0f8}(0, 1/3, 1, 0.5))
@test String(take!(iob)) == "RGBA{N0f8}(0.0,0.333,1.0,0.502)"
show(IOContext(iob, :compact => true), RGBA{N0f8}(0, 1/3, 1, 0.5))
@test String(take!(iob)) == "RGBA{N0f8}(0.0,0.333,1.0,0.502)"
show(iob, ARGB{N0f8}(0, 1/3, 1, 0.5))
@test String(take!(iob)) == "ARGB{N0f8}(0.0,0.333,1.0,0.502)"
show(IOContext(iob, :compact => true), ARGB{N0f8}(0, 1/3, 1, 0.5))
@test String(take!(iob)) == "ARGB{N0f8}(0.0,0.333,1.0,0.502)"

show(iob, RGB24(0.4,0.2,0.8))
@test String(take!(iob)) == "RGB24(0.4N0f8,0.2N0f8,0.8N0f8)"
Expand All @@ -54,20 +48,57 @@ end
@test String(take!(iob)) == "GrayA{Float64}(0.8,1.0)"
show(iob, AGray(0.8))
@test String(take!(iob)) == "AGray{Float64}(0.8,1.0)"
show(iob, Gray{Bool}(1))
@test String(take!(iob)) == "Gray{Bool}(1)"
show(iob, Gray24(0.4))
@test String(take!(iob)) == "Gray24(0.4N0f8)"
show(iob, AGray32(0.8))
@test_broken String(take!(iob)) == "AGray32(0.8N0f8,1.0N0f8)"
@test String(take!(iob)) == "AGray32(0.8N0f8,1.0N0f8)"

show(iob, AnaglyphColor{Float32}(0.4, 0.2))
@test String(take!(iob)) == "AnaglyphColor{Float32}(0.4f0,0.2f0)"
@test String(take!(iob)) == "AnaglyphColor{Float32}(0.4,0.2)"
show(iob, CMYK{Float64}(0.1, 0.2, 0.3, 0.4))
@test String(take!(iob)) == "CMYK{Float64}(0.1,0.2,0.3,0.4)"
show(iob, ACMYK{N0f8}(0.2, 0.4, 0.6, 0.8))
@test String(take!(iob)) == "ACMYK{N0f8}(0.2,0.4,0.6,0.8,1.0)"
end

@testset "collection of colors" begin
@testset "array element" begin
iob = IOBuffer()
rgbf64 = RGB{Float64}(0, 1/3, 1)
show(IOContext(iob, :typeinfo=>RGB{Float64}), rgbf64)
@test String(take!(iob)) == "RGB(0.0,0.3333333333333333,1.0)"
show(IOContext(iob, :typeinfo=>RGB{Float64}, :compact=>true), rgbf64)
@test String(take!(iob)) == "RGB(0.0,0.333333,1.0)"
show(IOContext(iob, :typeinfo=>RGB), rgbf64)
@test String(take!(iob)) == "RGB{Float64}(0.0,0.3333333333333333,1.0)"

argb32 = ARGB32(0.4,0.2,0.8,1.0)
show(IOContext(iob, :typeinfo=>ARGB32), argb32)
@test String(take!(iob)) == "ARGB32(0.4N0f8,0.2N0f8,0.8N0f8,1.0N0f8)"
show(IOContext(iob, :typeinfo=>ARGB32, :compact=>true), argb32)
@test String(take!(iob)) == "ARGB32(0.4,0.2,0.8,1.0)"
show(IOContext(iob, :typeinfo=>ARGB), argb32)
@test String(take!(iob)) == "ARGB32(0.4N0f8,0.2N0f8,0.8N0f8,1.0N0f8)"

grayf64 = Gray{Float64}(1/3)
show(IOContext(iob, :typeinfo=>Gray{Float64}), grayf64)
@test String(take!(iob)) == "0.3333333333333333"
show(IOContext(iob, :typeinfo=>Gray{Float64}, :compact=>true), grayf64)
@test String(take!(iob)) == "0.333333"
show(IOContext(iob, :typeinfo=>Gray), grayf64)
@test String(take!(iob)) == "Gray{Float64}(0.3333333333333333)"

grayb = Gray{Bool}(1)
show(IOContext(iob, :typeinfo=>Gray{Bool}), grayb)
@test String(take!(iob)) == "1"
show(IOContext(iob, :typeinfo=>Gray{Bool}, :compact=>true), grayb)
@test String(take!(iob)) == "1"
show(IOContext(iob, :typeinfo=>Gray), grayb)
@test String(take!(iob)) == "Gray{Bool}(1)"
end

@testset "summary" begin
iob = IOBuffer()
summary(iob, Gray{N0f8}[0.2, 0.4, 0.6])
vec_summary = String(take!(iob))
Expand All @@ -83,15 +114,15 @@ end
avec_summary = String(take!(iob))

if VERSION >= v"1.6.0-DEV.356"
@test_broken vec_summary == "3-element Vector{Gray{N0f8}}"
@test_broken mat_summary == "2×2 Matrix{RGB{Float64}}"
@test_broken view_summary == "2×2 view(::Matrix{RGB{Float64}}, :, :) with eltype RGB{Float64}"
@test_broken avec_summary == "2-element Vector{TransparentColor}"
@test vec_summary == "3-element Vector{Gray{N0f8}}"
@test mat_summary == "2×2 Matrix{RGB{Float64}}"
@test view_summary == "2×2 view(::Matrix{RGB{Float64}}, :, :) with eltype RGB{Float64}"
@test avec_summary == "2-element Vector{TransparentColor}"
else
@test vec_summary == "3-element Array{Gray{N0f8},1} with eltype Gray{Normed{UInt8,8}}"
@test mat_summary == "2×2 Array{RGB{Float64},2} with eltype RGB{Float64}"
@test mat_summary == "2×2 Array{RGB{Float64},2}"
@test view_summary == "2×2 view(::Array{RGB{Float64},2}, :, :) with eltype RGB{Float64}"
@test avec_summary == "2-element Array{TransparentColor,1} with eltype TransparentColor"
@test avec_summary == "2-element Array{TransparentColor,1}"
end
end

Expand Down

0 comments on commit 9fc0c6d

Please sign in to comment.