Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add component iterator #260

Merged
merged 1 commit into from
Aug 13, 2021
Merged

Conversation

kimikage
Copy link
Collaborator

@kimikage kimikage commented Jul 31, 2021

This adds a component iterator as a "private" API.
This re-implements the folding functions with the iterator.

cf. #190, #247

@kimikage kimikage mentioned this pull request Jul 31, 2021
19 tasks
@kimikage kimikage force-pushed the component_iterator branch from c1a13e9 to 0fd278c Compare July 31, 2021 16:37
@codecov
Copy link

codecov bot commented Jul 31, 2021

Codecov Report

Merging #260 (a6f4d8d) into master (87364c4) will increase coverage by 0.04%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #260      +/-   ##
==========================================
+ Coverage   83.76%   83.80%   +0.04%     
==========================================
  Files           8        8              
  Lines         733      735       +2     
==========================================
+ Hits          614      616       +2     
  Misses        119      119              
Impacted Files Coverage Δ
src/conversions.jl 85.71% <100.00%> (+0.17%) ⬆️
src/operations.jl 98.44% <100.00%> (+0.06%) ⬆️
src/show.jl 94.64% <100.00%> (-0.36%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 87364c4...a6f4d8d. Read the comment docs.

@kimikage kimikage changed the title Add component iterator [WIP] Add component iterator Aug 1, 2021
@kimikage kimikage force-pushed the component_iterator branch from 0fd278c to faded62 Compare August 1, 2021 02:55
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}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should return the iterator itself (itr) instead of the tuple, but I don't know how to handle that well. 😕

@kimikage kimikage force-pushed the component_iterator branch from faded62 to f2058d6 Compare August 9, 2021 12:41
@kimikage kimikage changed the title [WIP] Add component iterator Add component iterator Aug 9, 2021
@kimikage kimikage marked this pull request as ready for review August 9, 2021 13:01
This adds a component iterator as a private API.
This re-implements the folding functions with the iterator.
@kimikage kimikage force-pushed the component_iterator branch from f2058d6 to a6f4d8d Compare August 9, 2021 14:06
@@ -126,3 +126,6 @@ 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}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the recommended API that people should use?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strictly speaking, I don't think the Tuple constructors are a part of public APIs because they don't have docstrings.
However, I don't see a problem with ColorVectorSpace.jl etc. using it internally.

Copy link
Collaborator Author

@kimikage kimikage Aug 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course, some people might argue that Colorant is never iterable.

julia> Tuple(0.5 + 0.5im) # Should `Colorant` really be like `Complex`?
(0.5 + 0.5im,)

However, since the design of tuple() and Tuple() are clearly different, I don't think there will be any confusion.

julia> Tuple(1:3)
(1, 2, 3)

julia> tuple(1:3)
(1:3,)

Copy link
Collaborator Author

@kimikage kimikage Aug 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to discuss again whether Tuple should be overloaded, and if so, what should be returned. (cf. PR #184)
Currently (ColorTypes v0.11) Tuple(::Colorant) causes an error.

julia> Tuple(RGB(1.0, 0.0, 0.0))
ERROR: MethodError: no method matching iterate(::RGB{Float64})

cc: @timholy

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm personally in favor of the Tuple(::Colorant) usage. It's kind of like CartesianIndex, that you can't iterate it but can easily construct an iterable tuple from Tuple or x.I.

@kimikage
Copy link
Collaborator Author

There is still a lot of room for discussion and improvement, but the iteration feature is useful for addressing the rest of the issues, so I will merge this first.

@kimikage kimikage merged commit bd4bad2 into JuliaGraphics:master Aug 13, 2021
@kimikage kimikage deleted the component_iterator branch August 13, 2021 13:09
This was referenced Aug 13, 2021
johnnychen94 added a commit that referenced this pull request May 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants