Skip to content

Commit 61e78ec

Browse files
authored
conditionally use SpecialFunctions through weak deps or Requires (#186)
1 parent 35ddfef commit 61e78ec

File tree

4 files changed

+54
-22
lines changed

4 files changed

+54
-22
lines changed

Project.toml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,28 @@ version = "0.9.10"
66
ColorTypes = "3da002f7-5984-5a60-b8a6-cbb66c0b333f"
77
FixedPointNumbers = "53c48c17-4a7d-5ca2-90c5-79b7896eea93"
88
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
9-
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
9+
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
1010
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
1111
TensorCore = "62fd8b95-f654-4bbd-a8a5-9c27f68ccd50"
1212

1313
[compat]
1414
ColorTypes = "0.10, 0.11"
1515
FixedPointNumbers = "0.8.2"
16+
Requires = "1"
1617
SpecialFunctions = "0.8, 0.9, 0.10, 1, 2.0"
1718
TensorCore = "0.1"
1819
julia = "1"
1920

21+
[weakdeps]
22+
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
23+
24+
[extensions]
25+
SpecialFunctionsExt = "SpecialFunctions"
26+
2027
[extras]
2128
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
29+
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
2230
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2331

2432
[targets]
25-
test = ["Colors", "Test"]
33+
test = ["SpecialFunctions", "Colors", "Test"]

ext/SpecialFunctionsExt.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module SpecialFunctionsExt
2+
import ColorVectorSpace
3+
using ColorTypes
4+
5+
isdefined(Base, :get_extension) ? (import SpecialFunctions) : (import ..SpecialFunctions)
6+
7+
const unaryops = (
8+
:gamma, :logfactorial, :erf, :erfc, :erfcx, :erfi, :dawson,
9+
:airyai, :airyaiprime, :airybi, :airybiprime,
10+
:besselj0, :besselj1, :bessely0, :bessely1,
11+
:eta, :zeta, :digamma
12+
)
13+
14+
for op in unaryops
15+
@eval SpecialFunctions.$op(c::AbstractGray) = Gray(SpecialFunctions.$op(gray(c)))
16+
end
17+
18+
function SpecialFunctions.logabsgamma(c::AbstractGray)
19+
lagc, s = SpecialFunctions.logabsgamma(gray(c))
20+
return Gray(lagc), s
21+
end
22+
end

src/ColorVectorSpace.jl

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module ColorVectorSpace
22

3-
using ColorTypes, FixedPointNumbers, SpecialFunctions
3+
using ColorTypes, FixedPointNumbers
44
using TensorCore
55
import TensorCore: ,
66

@@ -11,7 +11,7 @@ import Base: abs, clamp, convert, copy, div, eps, float,
1111
isfinite, isinf, isnan, isless, length, mapreduce, oneunit,
1212
promote_op, promote_rule, zero, trunc, floor, round, ceil, bswap,
1313
mod, mod1, rem, atan, hypot, max, min, real, typemin, typemax
14-
# More unaryOps (mostly math functions)
14+
# More unaryops (mostly math functions)
1515
import Base: conj, sin, cos, tan, sinh, cosh, tanh,
1616
asin, acos, atan, asinh, acosh, atanh,
1717
sec, csc, cot, asec, acsc, acot,
@@ -23,7 +23,6 @@ import Base: conj, sin, cos, tan, sinh, cosh, tanh,
2323
exp2, exp10, expm1, cbrt, sqrt,
2424
significand, frexp, modf
2525
import LinearAlgebra: norm, , dot, promote_leaf_eltypes # norm1, norm2, normInf
26-
import SpecialFunctions: gamma, logabsgamma, lfact
2726
using Statistics
2827
import Statistics: middle # and `_mean_promote`
2928

@@ -293,7 +292,7 @@ dotc(x::T, y::T) where {T<:AbstractRGB} = 0.200f0 * acc(red(x))*acc(red(y)) + 0.
293292
dotc(x::AbstractRGB, y::AbstractRGB) = dotc(promote(x, y)...)
294293

295294
# Scalar Gray
296-
const unaryOps = (:~, :conj, :abs,
295+
const unaryops = (:~, :conj, :abs,
297296
:sin, :cos, :tan, :sinh, :cosh, :tanh,
298297
:asin, :acos, :atan, :asinh, :acosh, :atanh,
299298
:sec, :csc, :cot, :asec, :acsc, :acot,
@@ -303,21 +302,10 @@ const unaryOps = (:~, :conj, :abs,
303302
:asind, :atand, :rad2deg, :deg2rad,
304303
:log, :log2, :log10, :log1p, :exponent, :exp,
305304
:exp2, :exp10, :expm1, :cbrt, :sqrt,
306-
:significand,
307-
:gamma, :lfact, :frexp, :modf,
308-
:(SpecialFunctions.erf), :(SpecialFunctions.erfc),
309-
:(SpecialFunctions.erfcx), :(SpecialFunctions.erfi), :(SpecialFunctions.dawson),
310-
:(SpecialFunctions.airyai),
311-
:(SpecialFunctions.airyaiprime), :(SpecialFunctions.airybi), :(SpecialFunctions.airybiprime),
312-
:(SpecialFunctions.besselj0), :(SpecialFunctions.besselj1), :(SpecialFunctions.bessely0), :(SpecialFunctions.bessely1),
313-
:(SpecialFunctions.eta), :(SpecialFunctions.zeta), :(SpecialFunctions.digamma))
314-
for op in unaryOps
305+
:significand, :frexp, :modf)
306+
for op in unaryops
315307
@eval ($op)(c::AbstractGray) = Gray($op(gray(c)))
316308
end
317-
function logabsgamma(c::AbstractGray)
318-
lagc, s = logabsgamma(gray(c))
319-
return Gray(lagc), s
320-
end
321309

322310
middle(c::AbstractGray) = arith_colorant_type(c)(middle(gray(c)))
323311
middle(x::C, y::C) where {C<:AbstractGray} = arith_colorant_type(C)(middle(gray(x), gray(y)))
@@ -523,6 +511,8 @@ For more information about the transition, see ColorVectorSpace's README.
523511
abs2(c::Union{Real,AbstractGray,AbstractRGB}) = c c
524512
end
525513

514+
isdefined(Base, :get_extension) || using Requires
515+
526516
function __init__()
527517
if isdefined(Base, :Experimental) && isdefined(Base.Experimental, :register_error_hint)
528518
Base.Experimental.register_error_hint(MethodError) do io, exc, argtypes, kwargs
@@ -549,6 +539,9 @@ function __init__()
549539
end
550540
end
551541
end
542+
@static if !isdefined(Base, :get_extension)
543+
@require SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b" include("../ext/SpecialFunctionsExt.jl")
544+
end
552545
end
553546

554547
## Precompilation

test/runtests.jl

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,18 @@ ColorTypes.comp2(c::RGBA32) = alpha(c)
280280
for g in (Gray(0.4), Gray{N0f8}(0.4))
281281
@test @inferred(zero(g)) === typeof(g)(0)
282282
@test @inferred(oneunit(g)) === typeof(g)(1)
283-
for opgroup in (ColorVectorSpace.unaryOps, (:trunc, :floor, :round, :ceil, :eps, :bswap))
284-
for op in opgroup
285-
op (:frexp, :exponent, :modf, :lfact) && continue
283+
SFE = if isdefined(Base, :get_extension)
284+
Base.get_extension(ColorVectorSpace, :SpecialFunctionsExt)
285+
else
286+
ColorVectorSpace.SpecialFunctionsExt
287+
end
288+
for mod in (
289+
ColorVectorSpace,
290+
SFE,
291+
(; unaryops = (:trunc, :floor, :round, :ceil, :eps, :bswap)),
292+
)
293+
for op in mod.unaryops
294+
op (:frexp, :exponent, :modf, :logfactorial) && continue
286295
op === :~ && eltype(g) === Float64 && continue
287296
op === :significand && eltype(g) === N0f8 && continue
288297
try

0 commit comments

Comments
 (0)