Skip to content

Necessity of color_rettype #155

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

Closed
kimikage opened this issue Apr 9, 2021 · 2 comments · Fixed by #158
Closed

Necessity of color_rettype #155

kimikage opened this issue Apr 9, 2021 · 2 comments · Fixed by #158

Comments

@kimikage
Copy link
Collaborator

kimikage commented Apr 9, 2021

The color_rettype has been around since the initial working commit of ColorVectorSpace, but now, I'm not sure what the point of it is.

# Scalar binary RGB operations require the same RGB type for each element,
# otherwise we don't know which to return
color_rettype(::Type{A}, ::Type{B}) where {A<:AbstractRGB,B<:AbstractRGB} = _color_rettype(base_colorant_type(A), base_colorant_type(B))
color_rettype(::Type{A}, ::Type{B}) where {A<:AbstractGray,B<:AbstractGray} = _color_rettype(base_colorant_type(A), base_colorant_type(B))
color_rettype(::Type{A}, ::Type{B}) where {A<:TransparentRGB,B<:TransparentRGB} = _color_rettype(base_colorant_type(A), base_colorant_type(B))
color_rettype(::Type{A}, ::Type{B}) where {A<:TransparentGray,B<:TransparentGray} = _color_rettype(base_colorant_type(A), base_colorant_type(B))
_color_rettype(::Type{C}, ::Type{C}) where {C<:Colorant} = C

The revamp of the color promotion rules is relatively recent (i.e. it was in ColorTypes v0.10.0), but I think there is less ambiguity now. As you know, ColorVectorSpace v0.9 requires ColorTypes v0.10.

julia> RGB24(0.1, 0.2, 0.3) + RGB{Float32}(0.4, 0.5, 0.6)
ERROR: MethodError: no method matching _color_rettype(::Type{RGB24}, ::Type{RGB})
In binary operation with Type{RGB24} and Type{RGB}, the return type is ambiguous

julia> BGR(0.1, 0.2, 0.3) + RGB{N0f8}(0.4, 0.5, 0.6)
ERROR: MethodError: no method matching _color_rettype(::Type{BGR}, ::Type{RGB})
In binary operation with Type{BGR} and Type{RGB}, the return type is ambiguous
julia> promote_type(RGB24, RGB)
RGB

julia> promote_type(BGR, RGB)
RGB
@kimikage
Copy link
Collaborator Author

kimikage commented Apr 9, 2021

If we keep the color_rettype, we can generalize it as follows.

color_rettype(::Type{C1}, ::Type{C2}) where {C1, C2} = base_colorant_type(promote_type(C1, C2))
color_rettype(c1::Colorant, c2::Colorant) = color_rettype(typeof(c1), typeof(c2))

@kimikage
Copy link
Collaborator Author

Here is another example of inconsistency in this:

julia> Gray24(1) * Gray{N0f8}(1)
ERROR: MethodError: no method matching _color_rettype(::Type{Gray24}, ::Type{Gray})
In binary operation with Type{Gray24} and Type{Gray}, the return type is ambiguous

julia> Gray24(1)  Gray{N0f8}(1)
Gray{N0f8}(1.0)

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 a pull request may close this issue.

1 participant