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

Improve compN for non-parametric ColorAlpha types #245

Merged
merged 1 commit into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/traits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ hue(c::Union{C,AlphaColor{C},ColorAlpha{C}}) where {C<:Luv} =
_comp(::Val{N}, c::Colorant) where N = getfield(c, N)
_comp(::Val{N}, c::AlphaColor) where N = getfield(c, N + 1)
_comp(::Val{N}, c::AlphaColorN{N}) where N = alpha(c)
_comp(::Val{N}, c::ColorAlphaN{N}) where N = alpha(c)

@noinline function _comp_error(c::ColorantN{N}, n::Int) where N
io = IOBuffer()
Expand Down
15 changes: 15 additions & 0 deletions test/customtypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ using ColorTypes.FixedPointNumbers

export C2, C2A, C4, AC4
export StrangeGray, Cyanotype
export RGBA32
export AnaglyphColor, CMYK, ACMYK

struct C2{T <: Real} <: Color{T,2}
Expand Down Expand Up @@ -62,6 +63,20 @@ function Base.convert(::Type{Cout}, c::C) where {Cout <: AbstractRGB, T, C <: Cy
Cout(T(r), T(g), T(b))
end

# non-parametric color
struct RGBA32 <: AbstractRGBA{RGB24, N0f8}
color::UInt32
RGBA32(c::UInt32, ::Type{Val{true}}) = new(c)
end
function RGBA32(r, g, b, alpha=1N0f8)
u32 = reinterpret(UInt32, ARGB32(r, g, b, alpha))
RGBA32((u32 << 0x8) | (u32 >> 0x18), Val{true})
end
ColorTypes.red( c::RGBA32) = reinterpret(N0f8, (c.color >> 0x18) % UInt8)
ColorTypes.green(c::RGBA32) = reinterpret(N0f8, (c.color >> 0x10) % UInt8)
ColorTypes.blue( c::RGBA32) = reinterpret(N0f8, (c.color >> 0x08) % UInt8)
ColorTypes.alpha(c::RGBA32) = reinterpret(N0f8, c.color % UInt8)

# minimal type for testing 2-component color
struct AnaglyphColor{T} <: Color{T,2} # not `TransparentGray`
left::T
Expand Down
2 changes: 2 additions & 0 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ SP = VERSION >= v"1.6.0-DEV.771" ? " " : "" # JuliaLang/julia #37085
show(iob, AGray32(0.8))
@test String(take!(iob)) == "AGray32(0.8N0f8,1.0N0f8)"

show(iob, CustomTypes.RGBA32(0.4, 0.2, 0.8, 1.0))
@test String(take!(iob)) == "RGBA32(0.4N0f8,0.2N0f8,0.8N0f8,1.0N0f8)"
show(iob, AnaglyphColor{Float32}(0.4, 0.2))
@test String(take!(iob)) == "AnaglyphColor{Float32}(0.4f0,0.2f0)"
show(iob, CMYK{Float64}(0.1, 0.2, 0.3, 0.4))
Expand Down
12 changes: 12 additions & 0 deletions test/traits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ using .CustomTypes
@test green(argb32) === 0.5N0f8
@test blue(argb32) === 0.0N0f8
end
@testset "accessors for custom RGBA" begin
rgba32 = CustomTypes.RGBA32(1, 0.5, 0, 0.8)
@test red(rgba32) === 1.0N0f8
@test green(rgba32) === 0.5N0f8
@test blue(rgba32) === 0.0N0f8
@test alpha(rgba32) === 0.8N0f8
end

@test_throws MethodError red(HSV(100, 0.6, 0.4))
end
Expand Down Expand Up @@ -145,6 +152,11 @@ end
@test comp5(ac4) === 0.5f0
ct = Cyanotype{Float32}(0.8) # 1-component color but not a gray
@test_broken comp1(ct) === 0.8f0
rgba32 = CustomTypes.RGBA32(1, 0.5, 0, 0.8)
@test comp1(rgba32) === 1N0f8
@test comp2(rgba32) === 0.5N0f8
@test comp3(rgba32) === 0N0f8
@test comp4(rgba32) === 0.8N0f8
end

@testset "color" begin
Expand Down