Skip to content

Commit

Permalink
Remove eltype_ub (#258)
Browse files Browse the repository at this point in the history
`eltype_ub` did not necessarily return the actual upper bound.
Algorithms based on `eltype_default` are recommended over `eltype_ub`.
This also removes `ColorTypes.ctypes`.
  • Loading branch information
kimikage authored Jun 5, 2021
1 parent 754a397 commit 9e2550f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
23 changes: 8 additions & 15 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -558,20 +558,13 @@ eltype_default(::Type{C}) where {C<:AbstractGray } = N0f8
eltype_default(::Type{C}) where {C<:Color } = Float32
eltype_default(::Type{P}) where {P<:Colorant } = eltype_default(color_type(P))

# Upper bound on element type for each color type
eltype_ub(::Type{P}) where {P<:Colorant } = eltype_ub(eltype_default(P))
eltype_ub(::Type{T}) where {T<:FixedPoint } = Fractional
eltype_ub(::Type{T}) where {T<:AbstractFloat} = AbstractFloat

@inline promote_eltype(::Type{C}, vals...) where {C<:Colorant} = _promote_eltype(eltype_ub(C), eltype_default(C), promote_type(map(typeof, vals)...))
_promote_eltype(::Type{AbstractFloat}, ::Type{Tdef}, ::Type{T}) where {Tdef,T<:AbstractFloat} = T
_promote_eltype(::Type{AbstractFloat}, ::Type{Tdef}, ::Type{T}) where {Tdef,T<:Real} = Tdef
_promote_eltype(::Type{Fractional}, ::Type{Tdef}, ::Type{T}) where {Tdef,T<:Fractional} = T
_promote_eltype(::Type{Fractional}, ::Type{Tdef}, ::Type{T}) where {Tdef,T<:Real} = Tdef

ctypes = union(setdiff(parametric3, [XRGB,RGBX]), [Gray])
@inline function promote_eltype(::Type{C}, vals...) where C<:Colorant
_promote_eltype(eltype_default(C), promote_type(map(typeof, vals)...))
end
_promote_eltype(::Type{Tdef}, ::Type{T}) where {Tdef<:AbstractFloat, T<:AbstractFloat} = T
_promote_eltype(::Type{Tdef}, ::Type{T}) where {Tdef<:FixedPoint, T<:Fractional} = T
_promote_eltype(::Type{Tdef}, ::Type{T}) where {Tdef, T} = Tdef

# the arg list for C below should be identical to ctypes above.
for (C, acol, cola) in [(DIN99d, :ADIN99d, :DIN99dA),
(DIN99o, :ADIN99o, :DIN99oA),
(DIN99, :ADIN99, :DIN99A),
Expand All @@ -593,8 +586,8 @@ for (C, acol, cola) in [(DIN99d, :ADIN99d, :DIN99dA),
fn = Expr(:tuple, fieldnames(C)...)
cfn = Expr(:tuple, colorfields(C)...)
elty = eltype_default(C)
ub = eltype_ub(C)
Csym = nameof(Base.unwrap_unionall(C))
ub = elty <: FixedPoint ? Fractional : AbstractFloat
Csym = nameof(C)
@eval @make_constructors $Csym $fn $elty
@eval @make_alpha $Csym $acol $cola $fn $cfn $ub $elty
end
Expand Down
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ doctest(ColorTypes, manual = false)

# if the test below fails, please extend the list of types at the call to
# make_alpha in types.jl (this is the price of making that list explicit)
@test Set(ColorTypes.ctypes) ==
ctypes = union(setdiff(ColorTypes.parametric3, (XRGB, RGBX)), (Gray,))
@test Set(ctypes) ==
Set([DIN99d, DIN99o, DIN99, HSI, HSL, HSV, LCHab, LCHuv,
LMS, Lab, Luv, XYZ, YCbCr, YIQ, xyY, BGR, RGB, Gray])

Expand Down

0 comments on commit 9e2550f

Please sign in to comment.