Skip to content

Commit 4b69dc1

Browse files
authored
Add more support for color type promotions (#158)
This uses the promotion rules defined in ColorTypes.jl v0.10 or later.
1 parent 4e39011 commit 4b69dc1

File tree

2 files changed

+10
-15
lines changed

2 files changed

+10
-15
lines changed

src/ColorVectorSpace.jl

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,7 @@ acctype(::Type{T1}, ::Type{T2}) where {T1,T2} = acctype(promote_type(T1, T2))
9494

9595
acc(x::Number) = convert(acctype(typeof(x)), x)
9696

97-
# Scalar binary RGB operations require the same RGB type for each element,
98-
# otherwise we don't know which to return
99-
color_rettype(::Type{A}, ::Type{B}) where {A<:AbstractRGB,B<:AbstractRGB} = _color_rettype(base_colorant_type(A), base_colorant_type(B))
100-
color_rettype(::Type{A}, ::Type{B}) where {A<:AbstractGray,B<:AbstractGray} = _color_rettype(base_colorant_type(A), base_colorant_type(B))
101-
color_rettype(::Type{A}, ::Type{B}) where {A<:TransparentRGB,B<:TransparentRGB} = _color_rettype(base_colorant_type(A), base_colorant_type(B))
102-
color_rettype(::Type{A}, ::Type{B}) where {A<:TransparentGray,B<:TransparentGray} = _color_rettype(base_colorant_type(A), base_colorant_type(B))
103-
_color_rettype(::Type{C}, ::Type{C}) where {C<:Colorant} = C
104-
97+
color_rettype(::Type{C1}, ::Type{C2}) where {C1, C2} = base_colorant_type(promote_type(C1, C2))
10598
color_rettype(c1::Colorant, c2::Colorant) = color_rettype(typeof(c1), typeof(c2))
10699

107100
arith_colorant_type(::C) where {C<:Colorant} = arith_colorant_type(C)
@@ -433,11 +426,7 @@ end
433426
function __init__()
434427
if isdefined(Base, :Experimental) && isdefined(Base.Experimental, :register_error_hint)
435428
Base.Experimental.register_error_hint(MethodError) do io, exc, argtypes, kwargs
436-
if exc.f === _color_rettype && length(argtypes) >= 2
437-
# Color is not necessary, this is just to show it's possible.
438-
A, B = argtypes
439-
A !== B && print(io, "\nIn binary operation with $A and $B, the return type is ambiguous")
440-
elseif exc.f === dot && length(argtypes) == 2
429+
if exc.f === dot && length(argtypes) == 2
441430
if any(C -> C <: Union{TransparentGray, TransparentRGB}, argtypes)
442431
print(io, "\n`dot` (or `⋅`) for transparent colors is not supported ")
443432
print(io, "because the normalization is designed for opaque grays and RGBs.")

test/runtests.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,6 @@ ColorTypes.comp2(c::RGBA32) = alpha(c)
364364
@test_throws MethodError sum(abs2, RGB(0.1,0.2,0.3))
365365
@test norm(RGB(0.1,0.2,0.3)) sqrt(0.14)/sqrt(3)
366366

367-
@test_throws MethodError RGBX(0, 0, 1) + XRGB(1, 0, 0)
368-
369367
acu = RGB{N0f8}[cu]
370368
acf = RGB{Float32}[cf]
371369
@test typeof(acu+acf) == Vector{RGB{Float32}}
@@ -529,7 +527,14 @@ ColorTypes.comp2(c::RGBA32) = alpha(c)
529527
end
530528

531529
@testset "Mixed-type arithmetic" begin
530+
# issue 155
531+
@test @inferred(Gray(0.2f0) + Gray24(0.2)) === Gray{Float32}(0.2 + 0.2N0f8)
532+
@test @inferred(RGBX(0, 0, 1) + XRGB(1, 0, 0)) === XRGB{N0f8}(1, 0, 1)
533+
@test @inferred(BGR(0, 0, 1) + RGB24(1, 0, 0)) === RGB{N0f8}(1, 0, 1)
534+
@test_throws Exception HSV(100, 0.2, 0.4) + Gray(0.2)
535+
532536
@test AGray32(0.2, 0.4) + Gray24(0.2) === AGray32(0.4, 0.4N0f8+1N0f8)
537+
@test AGray32(0.2, 0.4) + Gray(0.2f0) === AGray{Float32}(0.2+0.2N0f8, 0.4N0f8+1)
533538
@test RGB(1, 0, 0) + Gray(0.2f0) === RGB{Float32}(1.2, 0.2, 0.2)
534539
@test RGB(1, 0, 0) - Gray(0.2f0) === RGB{Float32}(0.8, -0.2, -0.2)
535540
@test RGB24(1, 0, 0) + Gray(0.2f0) === RGB{Float32}(1.2, 0.2, 0.2)
@@ -541,6 +546,7 @@ ColorTypes.comp2(c::RGBA32) = alpha(c)
541546
@test RGB24(0.4, 0.6, 0.5) - AGray32(0.4, 0.2) === ARGB32(0, 0.2, 0.1, 0.8)
542547
@test ARGB32(0.4, 0, 0.2, 0.5) + Gray24(0.4) === ARGB32(0.8, 0.4, 0.6, 0.5N0f8+1N0f8)
543548
@test ARGB32(0.4, 0, 0.2, 0.5) + AGray32(0.4, 0.2) === ARGB32(0.8, 0.4, 0.6, 0.5N0f8+0.2N0f8)
549+
@test ARGB32(0.4, 0, 0.2, 0.5) + RGB(0.4f0, 0, 0) === ARGB{Float32}(0.4N0f8+0.4, 0, 0.2N0f8, 0.5N0f8+1)
544550

545551
g, rgb = Gray{Float32}(0.2), RGB{Float64}(0.1, 0.2, 0.3)
546552
ag, argb = AGray{Float64}(0.2, 0.8), ARGB{Float32}(0.1, 0.2, 0.3, 0.4)

0 commit comments

Comments
 (0)