Skip to content

Commit

Permalink
Generalize conversions from/to Real (#255)
Browse files Browse the repository at this point in the history
  • Loading branch information
kimikage authored May 29, 2021
1 parent e674ba9 commit 1cea549
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
19 changes: 6 additions & 13 deletions src/conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,12 @@ _convert(::Type{A}, ::Type{C1}, ::Type{C2}, c, alpha=alpha(c)) where {A<:Transpa
_convert(::Type{Cout}, ::Type{C1}, ::Type{C2}, c) where {Cout<:AbstractRGB,C1<:AbstractRGB,C2<:AbstractGray} = (g = convert(eltype(Cout), gray(c)); Cout(g, g, g))
_convert(::Type{A}, ::Type{C1}, ::Type{C2}, c, alpha=alpha(c)) where {A<:TransparentRGB,C1<:AbstractRGB,C2<:AbstractGray} = (g = convert(eltype(A), gray(c)); A(g, g, g, alpha))

convert(::Type{RGB24}, x::Real) = RGB24(x, x, x)
convert(::Type{ARGB32}, x::Real) = ARGB32(x, x, x)
convert(::Type{ARGB32}, x::Real, alpha) = ARGB32(x, x, x, alpha)
convert(::Type{Gray24}, x::Real) = Gray24(x)
convert(::Type{AGray32}, x::Real) = AGray32(x)
convert(::Type{AGray32}, x::Real, alpha) = AGray32(x, alpha)

convert(::Type{Gray{T}}, x::Real) where {T} = Gray{T}(x)
convert(::Type{AGray{T}}, x::Real) where {T} = AGray{T}(x)
convert(::Type{GrayA{T}}, x::Real) where {T} = GrayA{T}(x)

convert(::Type{T}, x::Gray ) where {T<:Real} = convert(T, x.val)
convert(::Type{T}, x::Gray24) where {T<:Real} = convert(T, gray(x))
convert(::Type{C}, x::Real) where {C<:ColorantN{1}} = C(x)
convert(::Type{C}, x::Real) where {C<:AbstractRGB} = C(x, x, x)
convert(::Type{C}, x::Real, alpha) where {C<:TransparentColorN{2}} = C(x, alpha)
convert(::Type{C}, x::Real, alpha) where {C<:TransparentRGB} = C(x, x, x, alpha)

convert(::Type{T}, x::ColorantN{1}) where {T<:Real} = convert(T, comp1(x))

(::Type{T})(x::ColorantN{1}) where {T<:Real} = T(comp1(x))

Expand Down
10 changes: 5 additions & 5 deletions test/conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -571,10 +571,10 @@ end

@test_throws ColorTypeResolutionError convert(Colorant, 0.6)
@test_throws ColorTypeResolutionError convert(Color, 0.6)
@test_throws ColorTypeResolutionError convert(Color{N0f8,1}, 0.6)
@test_throws MethodError convert(Color{N0f8,1}, 0.6)

@test_throws MethodError convert(GrayA, 0.6, 0.8)
@test_throws MethodError convert(AGray, 0, 1)
@test convert(GrayA, 0.6, 0.8) === GrayA{Float64}(0.6, 0.8)
@test convert(AGray, 0, 1) === AGray{N0f8}(0, 1)

@test convert(Gray, 2.0) === Gray{Float64}(2.0)
@test_throws ArgumentError convert(Gray, 2)
Expand All @@ -588,8 +588,8 @@ end
@test convert(ARGB, 0.6) === ARGB(0.6, 0.6, 0.6, 1)
@test convert(RGBA, 0.6N0f8) === RGBA{N0f8}(0.6, 0.6, 0.6, 1)

@test_broken convert(ARGB, 0.6f0, 0.8f0) === ARGB{Float32}(0.6, 0.6, 0.6, 0.8)
@test_broken convert(RGBA{Float32}, 0.6, 0.8) === RGBA{Float32}(0.6, 0.6, 0.6, 0.8)
@test convert(ARGB, 0.6f0, 0.8f0) === ARGB{Float32}(0.6, 0.6, 0.6, 0.8)
@test convert(RGBA{Float32}, 0.6, 0.8) === RGBA{Float32}(0.6, 0.6, 0.6, 0.8)

end

Expand Down

0 comments on commit 1cea549

Please sign in to comment.