Skip to content

Commit

Permalink
Bump compatibility for ColorTypes v0.12
Browse files Browse the repository at this point in the history
In ColorTypes v0.12, the definitions of `one` and `AbstractGray` have changed.
Also, as of ColorTypes v0.11, some methods have been migrated from this package to ColorTypes.
For these reasons, this drops the compatibility with ColorTypes v0.11 and earlier.
  • Loading branch information
kimikage committed Sep 9, 2021
1 parent 445ce7c commit e895055
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 63 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/UnitTest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,16 @@ jobs:
env:
cache-name: cache-artifacts
with:
path: ~/.julia/artifacts
path: ~/.julia/artifacts
key: ${{ runner.os }}-test-${{ env.cache-name }}-${{ hashFiles('**/Project.toml') }}
restore-keys: |
${{ runner.os }}-test-${{ env.cache-name }}-
${{ runner.os }}-test-
${{ runner.os }}-
- name: "add ColorTypes#master"
shell: bash
run: |
julia --project=. -e 'using Pkg; Pkg.add(PackageSpec(name="ColorTypes", rev="master")); Pkg.instantiate()'
- name: "Unit Test"
uses: julia-actions/julia-runtest@master

Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
TensorCore = "62fd8b95-f654-4bbd-a8a5-9c27f68ccd50"

[compat]
ColorTypes = "0.10, 0.11"
ColorTypes = "0.11, 0.12"
FixedPointNumbers = "0.8.2"
SpecialFunctions = "0.8, 0.9, 0.10, 1"
TensorCore = "0.1"
Expand Down
58 changes: 7 additions & 51 deletions src/ColorVectorSpace.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
module ColorVectorSpace

using ColorTypes, FixedPointNumbers, SpecialFunctions
using ColorTypes: nan
using TensorCore
import TensorCore: ,

using FixedPointNumbers: ShorterThanInt

import Base: ==, +, -, *, /, ^, <, ~
import Base: abs, clamp, convert, copy, div, eps, float,
isfinite, isinf, isnan, isless, length, mapreduce, oneunit,
promote_op, promote_rule, zero, trunc, floor, round, ceil, bswap,
mod, mod1, rem, atan, hypot, max, min, real, typemin, typemax
import Base: +, -, *, /, ^, ~
import Base: abs, clamp, copy, div, eps, float,
promote_rule, zero, trunc, floor, round, ceil, bswap,
mod, mod1, rem, atan, hypot, max, min, typemin, typemax
# More unaryOps (mostly math functions)
import Base: conj, sin, cos, tan, sinh, cosh, tanh,
asin, acos, atan, asinh, acosh, atanh,
Expand All @@ -31,50 +31,6 @@ export RGBRGB, complement, nan, dotc, dot, ⋅, hadamard, ⊙, tensor, ⊗, norm

MathTypes{T,C<:Union{AbstractGray{T},AbstractRGB{T}}} = Union{C,TransparentColor{C,T}}

## Version compatibility with ColorTypes
### TODO: Remove the definitons other than `one` when dropping ColorTypes v0.10 support

if !hasmethod(zero, (Type{TransparentGray},))
zero(::Type{C}) where {C<:TransparentGray} = C(0,0)
zero(::Type{C}) where {C<:AbstractRGB} = C(0,0,0)
zero(::Type{C}) where {C<:TransparentRGB} = C(0,0,0,0)
zero(p::Colorant) = zero(typeof(p))
end

if !hasmethod(one, (Type{TransparentGray},)) # specification change is planned for ColorTypes v0.12
Base.one(::Type{C}) where {C<:TransparentGray} = C(1,1)
Base.one(::Type{C}) where {C<:AbstractRGB} = C(1,1,1)
Base.one(::Type{C}) where {C<:TransparentRGB} = C(1,1,1,1)
Base.one(p::Colorant) = one(typeof(p))
end

if !hasmethod(isfinite, (Colorant,))
isfinite(c::Colorant) = mapreducec(isfinite, &, true, c)
isinf(c::Colorant) = mapreducec(isinf, |, false, c)
isnan(c::Colorant) = mapreducec(isnan, |, false, c)
end

if which(<, Tuple{AbstractGray,AbstractGray}).module === Base
(<)(g1::AbstractGray, g2::AbstractGray) = gray(g1) < gray(g2)
(<)(c::AbstractGray, r::Real) = gray(c) < r
(<)(r::Real, c::AbstractGray) = r < gray(c)
end
if !hasmethod(isless, Tuple{AbstractGray,Real})
isless(c::AbstractGray, r::Real) = isless(gray(c), r)
isless(r::Real, c::AbstractGray) = isless(r, gray(c))
end

if isdefined(ColorTypes, :nan)
using ColorTypes: nan
else
nan(::Type{T}) where {T<:AbstractFloat} = convert(T, NaN)
nan(::Type{C}) where {T<:AbstractFloat, C<:MathTypes{T}} = mapc(_ -> nan(T), zero(C))
end

if which(real, (Type{<:AbstractGray},)).module === Base
real(::Type{C}) where {C<:AbstractGray} = real(eltype(C))
end

# To help type inference
promote_rule(::Type{T}, ::Type{C}) where {T<:Real,C<:AbstractGray} = promote_type(T, eltype(C))

Expand Down Expand Up @@ -243,8 +199,8 @@ copy(c::MathTypes) = c
abs(c::MathTypes) = mapc(abs, c)
norm(c::MathTypes, p::Real=2) = (cc = channels(c); norm(cc, p)/(p == 0 ? length(cc) : length(cc)^(1/p)))
()(a::C, b::C) where {C<:MathTypes} = _mapc(rettype(*, a, b), _mul, a, b)
()(a::C, b::C) where {C<:MathTypes} = throw(MethodError(dot, (a, b)))
()(a::C, b::C) where {C<:MathTypes} = throw(MethodError(tensor, (a, b)))
()(a::C, b::C) where {C<:Union{TransparentGray, TransparentRGB}} = throw(MethodError(dot, (a, b)))
()(a::C, b::C) where {C<:Union{TransparentGray, TransparentRGB}} = throw(MethodError(tensor, (a, b)))

## Mixed types
(+)(a::MathTypes, b::MathTypes) = (+)(promote(a, b)...)
Expand Down
14 changes: 5 additions & 9 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ ColorTypes.comp2(c::RGBA32) = alpha(c)
for x in (0.5, 0.5f0, NaN, NaN32, N0f8(0.5))
@test @inferred(convert(Gray{typeof(x)}, x)) === @inferred(convert(Gray, x)) === Gray(x)
@test @inferred(convert(RGB{typeof(x)}, x)) === @inferred(convert(RGB, x)) === RGB(x, x, x)
# These should be fixed by a future release of ColorTypes
@test_broken @inferred(convert(AGray{typeof(x)}, x)) === @inferred(convert(AGray, x)) === AGray(x, 1)
@test_broken @inferred(convert(ARGB{typeof(x)}, x)) === @inferred(convert(ARGB, x)) === ARGB(x, x, x, 1)
@test_broken @inferred(convert(GrayA{typeof(x)}, x)) === @inferred(convert(GrayA, x)) === GrayA(x, 1)
@test_broken @inferred(convert(RGBA{typeof(x)}, x)) === @inferred(convert(RGBA, x)) === RGBA(x, x, x, 1)
@test @inferred(convert(AGray{typeof(x)}, x)) === @inferred(convert(AGray, x)) === AGray(x, 1)
@test @inferred(convert(ARGB{typeof(x)}, x)) === @inferred(convert(ARGB, x)) === ARGB(x, x, x, 1)
@test @inferred(convert(GrayA{typeof(x)}, x)) === @inferred(convert(GrayA, x)) === GrayA(x, 1)
@test @inferred(convert(RGBA{typeof(x)}, x)) === @inferred(convert(RGBA, x)) === RGBA(x, x, x, 1)
end
end

@testset "nan" begin
# `nan` for `Colorant` is defined in ColorTypes. It should be exported by ColorVectorSpace
function make_checked_nan(::Type{T}) where T
x = nan(T)
isa(x, T) && mapreducec(isnan, &, true, x)
Expand All @@ -105,10 +105,6 @@ ColorTypes.comp2(c::RGBA32) = alpha(c)
end
end

@testset "traits" begin
@test floattype(Gray{N0f8}) === Gray{float(N0f8)}
end

@testset "_mul" begin
n0f8p = ((x, y) for x = N0f8(0):eps(N0f8):N0f8(1), y = N0f8(0):eps(N0f8):N0f8(1))
@test all(((x, y),) -> _mul(x, y) === N0f8(Float32(x) * Float32(y)), n0f8p)
Expand Down

0 comments on commit e895055

Please sign in to comment.