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

advertise ccolor in writing generic code #151

Open
johnnychen94 opened this issue Jul 2, 2020 · 1 comment
Open

advertise ccolor in writing generic code #151

johnnychen94 opened this issue Jul 2, 2020 · 1 comment
Labels
best practice workflows and best practice that are useful in JuliaImages and/or Julia

Comments

@johnnychen94
Copy link
Member

Functions in JuliaImages should accept both numerical array AbstractArray{<:Number} and colorant array Abstract{<:Colorant} if possible.

It is not uncommon to infer the return eltype in the beginning of the algorithm so as to pre-allocate array. I previously have two versions of codes to achieve this. The first version is:

function foo(img::AbstractArray{T}) where T<:Number
    CT = RGB{floattype(T)}
    ...
end

function foo(img::AbstractArray{T}) where T<:Colorant
    CT = RGB{floattype(eltype(T))}
    ...
end

Then one day I found out that eltype(Float32) == Float32, which make it possible to merge it into one method:

function foo(img::AbstractArray{T}) where T<:Number
    CT = RGB{floattype(eltype(T))}
    ...
end

As @kimikage pointed it out in JuliaGraphics/ColorTypes.jl#201, ccolor does a similar thing more reliably (I assume so?):

function foo(img::AbstractArray{T}) where T<:Number
    CT = ccolor(RGB, floattype(T))
    ...
end

I feel this worth documenting somewhere in the tutorials; otherwise, it's like a missing spell and people repeatedly reinvent it.

@kimikage
Copy link

kimikage commented Jul 2, 2020

The ccolor is a convenient and powerful function for such a purpose. However, ccolor is not so "generic".

For example:

julia> ccolor(HSV, Gray{Float32})
HSV{Float32}

julia> ccolor(HSV, Float32) 
ERROR: in ccolor, no automatic conversion from Float32 and HSV

(This is not a bug.)

However, at least for gray and rgb colors, it should work as expected. So, I also feel ccolor is worth documenting.

@johnnychen94 johnnychen94 added the best practice workflows and best practice that are useful in JuliaImages and/or Julia label Aug 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
best practice workflows and best practice that are useful in JuliaImages and/or Julia
Projects
None yet
Development

No branches or pull requests

2 participants