Skip to content

Commit 9b699ae

Browse files
committed
Add some compatibility for current releases of ColorTypes
1 parent 7da5597 commit 9b699ae

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

src/ColorVectorSpace.jl

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,30 @@ export RGBRGB, nan, dotc, dot, ⋅, hadamard, ⊙, tensor, ⊗, norm, varmult
3131

3232
MathTypes{T,C} = Union{AbstractRGB{T},TransparentRGB{C,T},AbstractGray{T},TransparentGray{C,T}}
3333

34+
## Version compatibility with ColorTypes
35+
36+
if !hasmethod(zero, (Type{AGray{N0f8}},))
37+
zero(::Type{C}) where {C<:TransparentGray} = C(0,0)
38+
zero(::Type{C}) where {C<:AbstractRGB} = C(0,0,0)
39+
zero(::Type{C}) where {C<:TransparentRGB} = C(0,0,0,0)
40+
zero(p::Colorant) = zero(typeof(p))
41+
end
42+
43+
if !hasmethod(one, (Gray{N0f8},))
44+
Base.one(::Type{C}) where {C<:AbstractGray} = C(1)
45+
Base.one(::Type{C}) where {C<:TransparentGray} = C(1,1)
46+
Base.one(::Type{C}) where {C<:AbstractRGB} = C(1,1,1)
47+
Base.one(::Type{C}) where {C<:TransparentRGB} = C(1,1,1,1)
48+
Base.one(p::Colorant) = one(typeof(p))
49+
end
50+
51+
if !hasmethod(oneunit, (Type{AGray{N0f8}},))
52+
oneunit(::Type{C}) where {C<:TransparentGray} = C(1,1)
53+
oneunit(::Type{C}) where {C<:AbstractRGB} = C(1,1,1)
54+
oneunit(::Type{C}) where {C<:TransparentRGB} = C(1,1,1,1)
55+
oneunit(p::Colorant) = oneunit(typeof(p))
56+
end
57+
3458
## Traits and key utilities
3559

3660
# Return types for arithmetic operations
@@ -264,15 +288,15 @@ min(a::AbstractGray, b::Number) = min(promote(a,b)...)
264288
atan(x::AbstractGray, y::AbstractGray) = atan(gray(x), gray(y))
265289
hypot(x::AbstractGray, y::AbstractGray) = hypot(gray(x), gray(y))
266290

267-
if !hasmethod(<, Tuple{AbstractGray,AbstractGray}) # this was moved to ColorTypes 0.11
291+
if !hasmethod(<, Tuple{AbstractGray,AbstractGray}) # planned for ColorTypes 0.11
268292
(<)(g1::AbstractGray, g2::AbstractGray) = gray(g1) < gray(g2)
269293
(<)(c::AbstractGray, r::Real) = gray(c) < r
270294
(<)(r::Real, c::AbstractGray) = r < gray(c)
271295
end
272296
if !hasmethod(isless, Tuple{AbstractGray,AbstractGray}) # this was moved to ColorTypes 0.10
273297
isless(g1::AbstractGray, g2::AbstractGray) = isless(gray(g1), gray(g2))
274298
end
275-
if !hasmethod(isless, Tuple{AbstractGray,Real}) # this was moved to ColorTypes 0.11
299+
if !hasmethod(isless, Tuple{AbstractGray,Real}) # planned for ColorTypes 0.11
276300
isless(c::AbstractGray, r::Real) = isless(gray(c), r)
277301
isless(r::Real, c::AbstractGray) = isless(r, gray(c))
278302
end

test/runtests.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ end
2323
for x in (0.5, 0.5f0, NaN, NaN32, N0f8(0.5))
2424
@test @inferred(convert(Gray{typeof(x)}, x)) === @inferred(convert(Gray, x)) === Gray(x)
2525
@test @inferred(convert(RGB{typeof(x)}, x)) === @inferred(convert(RGB, x)) === RGB(x, x, x)
26-
@test @inferred(convert(AGray{typeof(x)}, x)) === @inferred(convert(AGray, x)) === AGray(x, 1)
27-
@test @inferred(convert(ARGB{typeof(x)}, x)) === @inferred(convert(ARGB, x)) === ARGB(x, x, x, 1)
28-
@test @inferred(convert(GrayA{typeof(x)}, x)) === @inferred(convert(GrayA, x)) === GrayA(x, 1)
29-
@test @inferred(convert(RGBA{typeof(x)}, x)) === @inferred(convert(RGBA, x)) === RGBA(x, x, x, 1)
26+
# These should be fixed by a future release of ColorTypes
27+
@test_broken @inferred(convert(AGray{typeof(x)}, x)) === @inferred(convert(AGray, x)) === AGray(x, 1)
28+
@test_broken @inferred(convert(ARGB{typeof(x)}, x)) === @inferred(convert(ARGB, x)) === ARGB(x, x, x, 1)
29+
@test_broken @inferred(convert(GrayA{typeof(x)}, x)) === @inferred(convert(GrayA, x)) === GrayA(x, 1)
30+
@test_broken @inferred(convert(RGBA{typeof(x)}, x)) === @inferred(convert(RGBA, x)) === RGBA(x, x, x, 1)
3031
end
3132
end
3233

0 commit comments

Comments
 (0)