Skip to content

Commit

Permalink
Revert "Redesign color constructors (#197)"
Browse files Browse the repository at this point in the history
This reverts commit 87364c4.
  • Loading branch information
johnnychen94 committed May 15, 2022
1 parent 6f03992 commit 676730c
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 219 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -423,4 +423,4 @@ In most cases, adding a new color space is quite straightforward:
In special cases, there may be other considerations:
- For `AbstractRGB`/`AbstractGray` types, `0` means "black" and `1` means
"saturated."
- If your type has extra fields, check the "Generated code" section of `types.jl` carefully. You may need to define a `colorfields` function and/or call `@make_alpha` manually.
- If your type has extra fields, check the "Generated code" section of `types.jl` carefully. You may need to define a `colorfields` function and/or call `@make_constructors` or `@make_alpha` manually.
9 changes: 5 additions & 4 deletions src/conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ cconvert(::Type{C}, c) where {C} = _convert(C, base_color_type(C), base_color_ty

convert(::Type{C}, c::Color, alpha) where {C<:TransparentColor} = cconvert(ccolor(C, typeof(c)), c, alpha)
cconvert(::Type{C}, c::Color, alpha) where {C<:TransparentColor} =_convert(C, base_color_type(C), base_color_type(c), c, alpha)
cconvert(::Type{ARGB32}, c::AbstractRGB, alpha) = ARGB32(c, alpha) # optimization for speed

# Fallback definitions that print nice error messages
_convert(::Type{C}, ::Any, ::Any, c) where {C} = error("No conversion of ", c, " to ", C, " has been defined")
Expand All @@ -86,7 +87,7 @@ _convert(::Type{Cout}, ::Type{C1}, ::Type{C2}, c) where {Cout<:AbstractRGB,C1<:A
_convert(::Type{A}, ::Type{C1}, ::Type{C2}, c, alpha=alpha(c)) where {A<:TransparentRGB,C1<:AbstractRGB,C2<:AbstractRGB} = A(red(c), green(c), blue(c), alpha)

# Implementations for when the base color type is not changing
# These might strip/add transparency, however
# These might trip/add transparency, however
_convert(::Type{Cout}, ::Type{Ccmp}, ::Type{Ccmp}, c) where {Cout<:Color3,Ccmp<:Color3} = Cout(comp1(c), comp2(c), comp3(c))
_convert(::Type{A}, ::Type{Ccmp}, ::Type{Ccmp}, c, alpha=alpha(c)) where {A<:Transparent3,Ccmp<:Color3} = A(comp1(c), comp2(c), comp3(c), alpha)

Expand All @@ -97,10 +98,10 @@ _convert(::Type{A}, ::Type{C1}, ::Type{C2}, c, alpha=alpha(c)) where {A<:Transpa
_convert(::Type{Cout}, ::Type{C1}, ::Type{C2}, c) where {Cout<:AbstractRGB,C1<:AbstractRGB,C2<:AbstractGray} = (g = convert(eltype(Cout), gray(c)); Cout(g, g, g))
_convert(::Type{A}, ::Type{C1}, ::Type{C2}, c, alpha=alpha(c)) where {A<:TransparentRGB,C1<:AbstractRGB,C2<:AbstractGray} = (g = convert(eltype(A), gray(c)); A(g, g, g, alpha))

convert(::Type{C}, x::Real) where {C<:Union{ColorantN{1}, TransparentColorN{2}}} = C(x)
convert(::Type{C}, x::Real) where {C<:Union{AbstractRGB, TransparentRGB}} = C(x)
convert(::Type{C}, x::Real) where {C<:ColorantN{1}} = C(x)
convert(::Type{C}, x::Real) where {C<:AbstractRGB} = C(x, x, x)
convert(::Type{C}, x::Real, alpha) where {C<:TransparentColorN{2}} = C(x, alpha)
convert(::Type{C}, x::Real, alpha) where {C<:TransparentRGB} = C(x, alpha)
convert(::Type{C}, x::Real, alpha) where {C<:TransparentRGB} = C(x, x, x, alpha)

convert(::Type{T}, x::ColorantN{1}) where {T<:Real} = convert(T, comp1(x))

Expand Down
8 changes: 0 additions & 8 deletions src/traits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,6 @@ to_top(::Type{Colorant{T,N}}) where {T,N} = Colorant{T,N}

to_top(c::Colorant) = to_top(typeof(c))


# Return the number of components in the color
# Note this is different from div(sizeof(c), sizeof(eltype(c))) (e.g., XRGB)
length(::Type{<:ColorantN{N}}) where N = N

length(c::Colorant) = length(typeof(c))


# eltype(RGB{Float32}) -> Float32
eltype(::Type{C}) where {C<:Colorant{T}} where {T} = T

Expand Down
Loading

0 comments on commit 676730c

Please sign in to comment.