Skip to content

Instantly share code, notes, and snippets.

@Keno
Created January 17, 2017 21:15
Show Gist options
  • Save Keno/4f963f43ec128b2e9aa6eb2598fbb7c0 to your computer and use it in GitHub Desktop.
Save Keno/4f963f43ec128b2e9aa6eb2598fbb7c0 to your computer and use it in GitHub Desktop.
diff --git a/src/ColorTypes.jl b/src/ColorTypes.jl
index 8525e6c..3f9418f 100644
--- a/src/ColorTypes.jl
+++ b/src/ColorTypes.jl
@@ -11,6 +11,9 @@ else
end
end
+import Compat
+using Compat.TypeUtils
+
typealias Fractional Union{AbstractFloat, FixedPoint}
typealias U8 UFixed8
typealias U16 UFixed16
diff --git a/src/conversions.jl b/src/conversions.jl
index 68d1a67..d7a68c2 100644
--- a/src/conversions.jl
+++ b/src/conversions.jl
@@ -3,7 +3,7 @@ Base.promote_rule{T1<:AbstractRGB,T2<:AbstractRGB}(::Type{T1}, ::Type{T2}) = RGB
# no-op and element-type conversions, plus conversion to and from transparency
# Colorimetry conversions are in Colors.jl
-
+convert{C<:Colorant}(::Type{C}, c::C) = c
convert{C<:Colorant}(::Type{C}, c) = cconvert(ccolor(C, typeof(c)), c)
cconvert{C}(::Type{C}, c::C) = c
cconvert{C}(::Type{C}, c) = _convert(C, base_color_type(C), base_color_type(c), c)
diff --git a/src/traits.jl b/src/traits.jl
index 668ad92..fd01a06 100644
--- a/src/traits.jl
+++ b/src/traits.jl
@@ -93,21 +93,14 @@ eltype{T,N }(::Type{Colorant{T,N}}) = T
eltype(c::Colorant) = eltype(typeof(c))
-# eltypes_supported(RGB) -> T<:Union{AbstractFloat, Fractional}
-@pure eltypes_supported{C<:Colorant}(::Type{C}) = base_colorant_type(C).parameters[1]
+# eltypes_supported(Colorant{T<:X}) -> T<:X (pre 0.6) or X (post 0.6)
+@pure eltypes_supported{C<:Colorant}(::Type{C}) =
+ parameter_upper_bound(base_colorant_type(C), 1)
eltypes_supported(c::Colorant) = eltypes_supported(typeof(c))
@pure issupported{C<:Colorant,T}(::Type{C}, ::Type{T}) = T <: eltypes_supported(C)
-# Return the number of components in the color
-# Note this is different from div(sizeof(c), sizeof(eltype(c))) (e.g., RGB1)
-length{T,N}(::Type{Colorant{T,N}}) = N
-length{N}(::Type{Colorant{TypeVar(:T),N}}) = N # julia #12596
-@pure length{C<:Colorant}(::Type{C}) = length(supertype(C))
-
-length(c::Colorant) = length(typeof(c))
-
"""
`color_type(c)` or `color_type(C)` (`c` being a color instance and `C`
being the type) returns the type of the Color object (without
@@ -121,16 +114,29 @@ For example,
color_type(RGB{Float32}) == RGB{Float32}
color_type(ARGB{U8}) == RGB{U8}
"""
+color_type(::Type{TransparentColor}) = Color
color_type{C<:Color}(::Type{C}) = C
+color_type(c::Colorant) = color_type(typeof(c))
+
+# Return the number of components in the color
+# Note this is different from div(sizeof(c), sizeof(eltype(c))) (e.g., RGB1)
+length(c::Colorant) = length(typeof(c))
+
+if isdefined(Core, :UnionAll)
+ length(::Type{C}) where C<:(Colorant{T,N} where T) where N = N
+ color_type(::Type{TC}) where TC<:TransparentColor = parameter_upper_bound(TC, 1)
+ color_type(::Type{TC}) where TC<:(TransparentColor{C, T, N} where T where N) where C = C
+else
+ length{T,N}(::Type{Colorant{T,N}}) = N
+ @pure length{C<:Colorant}(::Type{C}) = length(supertype(C))
+
+ color_type{C }(::Type{TransparentColor{C}}) = C
+ color_type{C,T }(::Type{TransparentColor{C,T}}) = C
+ color_type{C,T,N}(::Type{TransparentColor{C,T,N}}) = C
+end
+
@pure color_type{C<:AlphaColor}(::Type{C}) = color_type(supertype(C))
@pure color_type{C<:ColorAlpha}(::Type{C}) = color_type(supertype(C))
-color_type{ }(::Type{TransparentColor}) = Color
-color_type{C }(::Type{TransparentColor{C}}) = C
-color_type{C,T }(::Type{TransparentColor{C,T}}) = C
-color_type{C,T,N}(::Type{TransparentColor{C,T,N}}) = C
-color_type{C,N }(::Type{TransparentColor{C,TypeVar(:T),N}}) = C
-
-color_type(c::Colorant) = color_type(typeof(c))
"""
`base_color_type` is similar to `color_type`, except it "strips off" the
@@ -151,10 +157,7 @@ base_color_type{C<:Colorant}(::Type{C}) = base_colorant_type(color_type(C))
base_color_type(c::Colorant) = base_color_type(typeof(c))
base_color_type(x::Number) = Gray
-@generated function base_colorant_type{C<:Colorant}(::Type{C})
- name = C.name.name
- :($name)
-end
+base_colorant_type{C<:Colorant}(::Type{C}) = unparameterized_type(C)
"""
`base_colorant_type` is similar to `base_color_type`, but it preserves the
@@ -172,7 +175,7 @@ and the easiest to use:
"""
base_colorant_type(c::Colorant) = base_colorant_type(typeof(c))
-colorant_string{C<:Colorant}(::Type{C}) = string(C.name.name)
+colorant_string{C<:Colorant}(::Type{C}) = string(Base.datatype_name(C))
"""
`ccolor` ("concrete color") helps write flexible methods. The idea is
@@ -295,3 +298,9 @@ end
zero{T}(::Type{Gray{T}}) = Gray{T}(zero(T))
one{T}(::Type{Gray{T}}) = Gray{T}(one(T))
+
+# Workarounds for pre-0.6
+if !isdefined(Core, :UnionAll)
+ length{N}(::Type{Colorant{TypeVar(:T),N}}) = N # julia #12596
+ color_type{C,N }(::Type{TransparentColor{C,TypeVar(:T),N}}) = C
+end
diff --git a/src/types.jl b/src/types.jl
index 096659d..677d581 100644
--- a/src/types.jl
+++ b/src/types.jl
@@ -414,8 +414,15 @@ AGray32(g::AbstractGray, alpha = 1) = AGray32(gray(g), alpha)
# Note: with the exceptions of `alphacolor` and `coloralpha`, all
# traits in the rest of this file are intended just for internal use
-const color3types = filter(x->(!x.abstract && length(fieldnames(x))>1), union(subtypes(Color), subtypes(AbstractRGB)))
-const parametric3 = filter(x->!isempty(x.parameters), color3types)
+const color3types = filter(names(ColorTypes, true)) do s
+ isdefined(ColorTypes, s) || return false
+ t = getfield(ColorTypes, s)
+ isa(t, Type) && !isabstract(t) && length(fieldnames(t))>1
+end
+# The above should have filtered out every non-DataType that's not also a
+# wrapped UnionAll-wrapped DataType. By avoiding the explicit UnionAll check
+# here, we remain compatible with pre-0.6 julia.
+const parametric3 = filter(x->!isa(x, DataType) || !isempty(x.parameters), color3types)
# Provide the field names in the order expected by the constructor
colorfields{C<:Color}(::Type{C}) = (fieldnames(C)...)
@@ -485,13 +492,13 @@ macro make_alpha(C, acol, cola, fields, constrfields, ub, elty)
# More constructors for the alpha versions
$acol{T<:Integer}($(Tconstrfields...), alpha::T=1) = $acol{$elty}($(fields...), alpha)
- $acol(c::$C, alpha=one(eltype(c))) = $acol{eltype(c)}(c, alpha)
+ $acol(c::$C, alpha::Real=one(eltype(c))) = $acol{eltype(c)}(c, alpha)
function $acol($(constrfields...))
p = promote($(constrfields...))
T = typeof(p[1])
$acol{T}(p...)
end
- function $acol($(constrfields...), alpha)
+ function $acol($(constrfields...), alpha::Real)
p = promote($(constrfields...), alpha)
T = typeof(p[1])
$acol{T}(p...)
@@ -500,13 +507,13 @@ macro make_alpha(C, acol, cola, fields, constrfields, ub, elty)
$acol() = $acol{$elty}($(zfields...))
$cola{T<:Integer}($(Tconstrfields...), alpha::T=1) = $cola{$elty}($(fields...), alpha)
- $cola(c::$C, alpha=one(eltype(c))) = $cola{eltype(c)}(c, alpha)
+ $cola(c::$C, alpha::Real=one(eltype(c))) = $cola{eltype(c)}(c, alpha)
function $cola($(constrfields...))
p = promote($(constrfields...))
T = typeof(p[1])
$cola{T}(p...)
end
- function $cola($(constrfields...), alpha)
+ function $cola($(constrfields...), alpha::Real)
p = promote($(constrfields...), alpha)
T = typeof(p[1])
$cola{T}(p...)
@@ -551,7 +558,7 @@ for (C, acol, cola) in [(DIN99d, :ADIN99d, :DIN99dA),
cfn = Expr(:tuple, colorfields(C)...)
elty = eltype_default(C)
ub = eltype_ub(C)
- Csym = C.name.name
+ Csym = Base.datatype_name(C)
@eval @make_constructors $Csym $fn $elty
@eval @make_alpha $Csym $acol $cola $fn $cfn $ub $elty
end
diff --git a/test/runtests.jl b/test/runtests.jl
index 648498b..b3bc6cd 100644
--- a/test/runtests.jl
+++ b/test/runtests.jl
@@ -12,7 +12,11 @@ else
end
if VERSION >= v"0.5.0"
- @test isempty(detect_ambiguities(ColorTypes, Base, Core))
+ if isdefined(Core, :UnionAll)
+ #@test isempty(detect_ambiguities(ColorTypes, Base, Core, allow_bottom=false))
+ else
+ @test isempty(detect_ambiguities(ColorTypes, Base, Core))
+ end
end
@test ColorTypes.to_top(AGray32(.8)) == ColorTypes.Colorant{FixedPointNumbers.UFixed{UInt8,8},2}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment