Skip to content

Necessity of color_rettype #155

@kimikage

Description

@kimikage

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions