Skip to content
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
12 changes: 11 additions & 1 deletion src/ColorVectorSpace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import SpecialFunctions: gamma, lgamma, lfact
using Statistics
import Statistics: middle, _mean_promote

export RGBRGB, nan, dotc, dot, ⋅, hadamard, ⊙, tensor, ⊗, norm, varmult
export RGBRGB, complement, nan, dotc, dot, ⋅, hadamard, ⊙, tensor, ⊗, norm, varmult

MathTypes{T,C} = Union{AbstractRGB{T},TransparentRGB{C,T},AbstractGray{T},TransparentGray{C,T}}

Expand Down Expand Up @@ -225,6 +225,16 @@ for op in unaryOps
@eval ($op)(c::AbstractGray) = Gray($op(gray(c)))
end

"""
y = complement(x)

Take the complement `1-x` of `x`. If `x` is a color with an alpha channel,
the alpha channel is left untouched. Don't forget to add a dot when `x` is
an array: `complement.(x)`
"""
complement(x::Union{Number,Colorant}) = oneunit(x)-x
complement(x::TransparentColor) = typeof(x)(complement(color(x)), alpha(x))

middle(c::AbstractGray) = arith_colorant_type(c)(middle(gray(c)))
middle(x::C, y::C) where {C<:AbstractGray} = arith_colorant_type(C)(middle(gray(x), gray(y)))

Expand Down
14 changes: 14 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,20 @@ ColorTypes.blue(c::RatRGB) = c.b
@test cf ⋅ cf === (Float64(red(cf))^2 + Float64(green(cf))^2 + Float64(blue(cf))^2)/3
end

@testset "Complement" begin
@test complement(Gray(0.2)) === Gray(0.8)
@test complement(AGray(0.2f0, 0.7f0)) === AGray(0.8f0, 0.7f0)
@test complement(GrayA{N0f8}(0.2, 0.7)) === GrayA{N0f8}(0.8, 0.7)
@test_broken complement(Gray24(0.2)) === Gray24(0.8)
@test_broken complement(AGray32(0.2, 0.7)) === AGray32(0.8, 0.7)
Comment on lines +486 to +487
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, oneunit(::AbstractGray) has been missing. This may be fixed in ColorTypes v0.11.


@test complement(RGB(0, 0.3, 1)) === RGB(1, 0.7, 0)
@test complement(ARGB(0, 0.3f0, 1, 0.7f0)) === ARGB(1, 0.7f0, 0, 0.7f0)
@test complement(RGBA{N0f8}(0, 0.6, 1, 0.7)) === RGBA{N0f8}(1, 0.4, 0.0, 0.7)
@test complement(RGB24(0, 0.6, 1)) === RGB24(1, 0.4, 0.0)
@test complement(ARGB32(0, 0.6, 1, 0.7)) === ARGB32(1, 0.4, 0.0, 0.7)
end

@testset "dotc" begin
@test dotc(0.2, 0.2) == 0.2^2
@test dotc(Int8(3), Int16(6)) === 18
Expand Down