Skip to content

Commit

Permalink
Revert "Add component iterator (#260)"
Browse files Browse the repository at this point in the history
This reverts commit bd4bad2.
  • Loading branch information
johnnychen94 committed May 15, 2022
1 parent 1050513 commit 6f03992
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 78 deletions.
3 changes: 0 additions & 3 deletions src/conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,3 @@ coloralpha(c::C) where {C<:Color} = coloralpha(C)(c)
coloralpha(c::C,a) where {C<:Color} = coloralpha(C)(c,a)
coloralpha(c::C) where {C<:TransparentColor} = coloralpha(base_color_type(C))(color(c), alpha(c))
coloralpha(c::C,a) where {C<:TransparentColor} = coloralpha(base_color_type(C))(color(c), a)

# Tuple
Tuple(c::Colorant{T, N}) where {T, N} = (comps(c)...,)::NTuple{N, T}
87 changes: 41 additions & 46 deletions src/operations.jl
Original file line number Diff line number Diff line change
@@ -1,43 +1,5 @@
# iterator
struct ComponentIterator{C<:Colorant}
c::C
end

eltype(::Type{ComponentIterator{C}}) where {T, C <: Colorant{T}} = T
length(::ComponentIterator{C}) where {N, C <: ColorantN{N}} = N

function Base.iterate(itr::ComponentIterator{C}, state::Int=0) where {N, C <: ColorantN{N}}
state < 0 && return nothing
state >= N && return nothing
state += 1
return (itr[state], state)
end

@inline function Base.getindex(itr::ComponentIterator{C}, i::Integer) where {N, C <: ColorantN{N}}
N > 0 && i == 1 && return comp1(itr.c)
N > 1 && i == 2 && return comp2(itr.c)
N > 2 && i == 3 && return comp3(itr.c)
N > 3 && i == 4 && return comp4(itr.c)
N > 4 && i == 5 && return comp5(itr.c)
throw(BoundsError(itr, i))
end
Base.getindex(itr::ComponentIterator, r::AbstractRange) = Tuple(itr[i] for i in r)
Base.getindex(itr::ComponentIterator, ::Colon) = itr

Base.firstindex(::ComponentIterator) = 1
Base.lastindex(itr::ComponentIterator) = length(itr)

function Base.BroadcastStyle(::Type{<:ComponentIterator{C}}) where {T, N, C <: Colorant{T, N}}
Base.BroadcastStyle(NTuple{N, T})
end
Base.axes(::ComponentIterator{C}) where {N, C <: ColorantN{N}} = (Base.OneTo(N),)
Base.ndims(::Type{ComponentIterator{C}}) where {C} = 1
function Base.broadcastable(itr::ComponentIterator{C}) where {T, N, C <: Colorant{T, N}}
(itr...,)::NTuple{N, T}
end

comps(c::Colorant) = ComponentIterator(c) # TODO: design public APIs

struct BoolTuple end
@inline BoolTuple(args::Bool...) = args

# comparison
_is_same_colorspace(a, b) = base_colorant_type(a) === base_colorant_type(b)
Expand All @@ -49,7 +11,7 @@ _is_same_colorspace(a::TransparentRGB, b::TransparentRGB) = true

function ==(a::ColorantN{N}, b::ColorantN{N}) where {N}
_is_same_colorspace(a, b) || return false
all(comps(a) .== comps(b))
all(_mapc(BoolTuple, ==, a, b))
end
==(x::Number, y::AbstractGray) = x == gray(y)
==(x::AbstractGray, y::Number) = ==(y, x)
Expand All @@ -58,7 +20,7 @@ end
function isapprox(a::ColorantN{N}, b::ColorantN{N}; kwargs...) where {N}
_is_same_colorspace(a, b) || return false
componentapprox(x, y) = isapprox(x, y; kwargs...)
all(componentapprox.(comps(a), comps(b)))
all(_mapc(BoolTuple, componentapprox, a, b))
end
isapprox(a::Colorant, b::Colorant; kwargs...) = false
isapprox(a::Number, b::AbstractGray; kwargs...) = isapprox(a, gray(b); kwargs...)
Expand Down Expand Up @@ -230,10 +192,30 @@ color(s), returning an output color in the same colorspace.
julia> mapc(+, RGB(0.1,0.8,0.3), RGB(0.5,0.5,0.5))
RGB{Float64}(0.6,1.3,0.8)
"""
@inline mapc(f, c::C) where {C<:Colorant} = base_colorant_type(C)(f.(comps(c))...)
@inline mapc(f, c::C) where {C<:ColorantN{1}} =
base_colorant_type(C)(f(comp1(c)))
@inline mapc(f, c::C) where {C<:ColorantN{2}} =
base_colorant_type(C)(f(comp1(c)), f(comp2(c)))
@inline mapc(f, c::C) where {C<:ColorantN{3}} =
base_colorant_type(C)(f(comp1(c)), f(comp2(c)), f(comp3(c)))
@inline mapc(f, c::C) where {C<:ColorantN{4}} =
base_colorant_type(C)(f(comp1(c)), f(comp2(c)), f(comp3(c)), f(comp4(c)))
@inline mapc(f, c::C) where {C<:ColorantN{5}} =
base_colorant_type(C)(f(comp1(c)), f(comp2(c)), f(comp3(c)), f(comp4(c)), f(comp5(c)))

@inline mapc(f, x::Number) = f(x)

@inline mapc(f::F, x, y) where {F} = _same_colorspace(x, y)(f.(comps(x), comps(y))...)
mapc(f::F, x, y) where F = _mapc(_same_colorspace(x,y), f, x, y)
_mapc(::Type{C}, f, x::ColorantN{1}, y::ColorantN{1}) where C =
C(f(comp1(x), comp1(y)))
_mapc(::Type{C}, f, x::ColorantN{2}, y::ColorantN{2}) where C =
C(f(comp1(x), comp1(y)), f(comp2(x), comp2(y)))
_mapc(::Type{C}, f, x::ColorantN{3}, y::ColorantN{3}) where C =
C(f(comp1(x), comp1(y)), f(comp2(x), comp2(y)), f(comp3(x), comp3(y)))
_mapc(::Type{C}, f, x::ColorantN{4}, y::ColorantN{4}) where C =
C(f(comp1(x), comp1(y)), f(comp2(x), comp2(y)), f(comp3(x), comp3(y)), f(comp4(x), comp4(y)))
_mapc(::Type{C}, f, x::ColorantN{5}, y::ColorantN{5}) where C =
C(f(comp1(x), comp1(y)), f(comp2(x), comp2(y)), f(comp3(x), comp3(y)), f(comp4(x), comp4(y)), f(comp5(x), comp5(y)))

_same_colorspace(x::Colorant, y::Colorant) = _same_colorspace(base_colorant_type(x),
base_colorant_type(y))
Expand All @@ -256,7 +238,12 @@ whereas for RGB
If `c` has an alpha channel, it is always the last one to be folded into the reduction.
"""
@inline reducec(op, v0, c::C) where {C <: Colorant} = reduce(op, Tuple(c); init=v0)
@inline reducec(op, v0, c::ColorantN{1}) = op(v0, comp1(c))
@inline reducec(op, v0, c::ColorantN{2}) = op(comp2(c), op(v0, comp1(c)))
@inline reducec(op, v0, c::ColorantN{3}) = op(comp3(c), op(comp2(c), op(v0, comp1(c))))
@inline reducec(op, v0, c::ColorantN{4}) = op(comp4(c), op(comp3(c), op(comp2(c), op(v0, comp1(c)))))
@inline reducec(op, v0, c::ColorantN{5}) = op(comp5(c), op(comp4(c), op(comp3(c), op(comp2(c), op(v0, comp1(c))))))

@inline reducec(op, v0, x::Number) = op(v0, x)

"""
Expand All @@ -274,5 +261,13 @@ whereas for RGB
If `c` has an alpha channel, it is always the last one to be folded into the reduction.
"""
@inline mapreducec(f, op, v0, c::C) where {C <: Colorant} = reduce(op, f.(comps(c)); init=v0)
@inline mapreducec(f, op, v0, c::ColorantN{1}) = op(v0, f(comp1(c)))
@inline mapreducec(f, op, v0, c::ColorantN{2}) = op(f(comp2(c)), op(v0, f(comp1(c))))
@inline mapreducec(f, op, v0, c::ColorantN{3}) =
op(f(comp3(c)), op(f(comp2(c)), op(v0, f(comp1(c)))))
@inline mapreducec(f, op, v0, c::ColorantN{4}) =
op(f(comp4(c)), op(f(comp3(c)), op(f(comp2(c)), op(v0, f(comp1(c))))))
@inline mapreducec(f, op, v0, c::ColorantN{5}) =
op(f(comp5(c)), op(f(comp4(c)), op(f(comp3(c)), op(f(comp2(c)), op(v0, f(comp1(c)))))))

@inline mapreducec(f, op, v0, x::Number) = op(v0, f(x))
6 changes: 5 additions & 1 deletion src/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ function _show_components(io::IO, c::Colorant{T, N}) where {T, N}
io = _components_iocontext(io, c)
print(io, '(')
for i = 1:N
show(io, comps(c)[i])
i == 1 && show(io, comp1(c))
i == 2 && show(io, comp2(c))
i == 3 && show(io, comp3(c))
i == 4 && show(io, comp4(c))
i == 5 && show(io, comp5(c))
print(io, i < N ? ", " : ")")
end
end
Expand Down
28 changes: 0 additions & 28 deletions test/operations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,6 @@ using Test
@isdefined(CustomTypes) || include("customtypes.jl")
using .CustomTypes

@testset "iterators" begin
@testset "ComponentIterator" begin
argb_comps = ColorTypes.comps(ARGB(1.0f0, 0.5f0, 0.0f0))
@test Iterators.IteratorSize(typeof(argb_comps)) === Iterators.HasLength()
@test Iterators.IteratorEltype(typeof(argb_comps)) === Iterators.HasEltype()
@test length(argb_comps) == 4
@test eltype(typeof(argb_comps)) === Float32
@test axes(argb_comps) === (Base.OneTo(4),)
@test ndims(typeof(argb_comps)) == 1
a = []
for v in argb_comps
push!(a, v)
end
@test all(a .=== (1.0f0, 0.5f0, 0.0f0, 1.0f0))
@test argb_comps[3] === 0.0f0
@test_throws BoundsError argb_comps[5]
@test_throws MethodError argb_comps[4] = 0.0f0 # read-only
@test argb_comps[2:2:4] === (0.5f0, 1.0f0)
@test argb_comps[:] === argb_comps
@test firstindex(argb_comps) == 1
@test lastindex(argb_comps) == 4
@test Base.BroadcastStyle(typeof(argb_comps)) === Base.Broadcast.Style{Tuple}()
@test argb_comps .* 0.5 === (0.5, 0.25, 0.0, 0.5)
@test argb_comps .+ (1:4) == Float32[2.0f0, 2.5f0, 3.0f0, 5.0f0]
@test argb_comps ./ (1, 2, 3, 4) === (1.0f0, 0.25f0, 0.0f0, 0.25f0)
end
end

@testset "comparisons" begin
Cp3 = ColorTypes.parametric3
for C in unique(vcat(Cp3, coloralpha.(Cp3), alphacolor.(Cp3)))
Expand Down

0 comments on commit 6f03992

Please sign in to comment.