Skip to content

Commit 3072185

Browse files
kimikagewizofe
andauthored
Add complement migrated from Images.jl (#144)
Co-Authored-by: Ioannis Valasakis <[email protected]>
1 parent 578fe82 commit 3072185

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/ColorVectorSpace.jl

+11-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import SpecialFunctions: gamma, lgamma, lfact
2727
using Statistics
2828
import Statistics: middle, _mean_promote
2929

30-
export RGBRGB, nan, dotc, dot, , hadamard, , tensor, , norm, varmult
30+
export RGBRGB, complement, nan, dotc, dot, , hadamard, , tensor, , norm, varmult
3131

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

@@ -225,6 +225,16 @@ for op in unaryOps
225225
@eval ($op)(c::AbstractGray) = Gray($op(gray(c)))
226226
end
227227

228+
"""
229+
y = complement(x)
230+
231+
Take the complement `1-x` of `x`. If `x` is a color with an alpha channel,
232+
the alpha channel is left untouched. Don't forget to add a dot when `x` is
233+
an array: `complement.(x)`
234+
"""
235+
complement(x::Union{Number,Colorant}) = oneunit(x)-x
236+
complement(x::TransparentColor) = typeof(x)(complement(color(x)), alpha(x))
237+
228238
middle(c::AbstractGray) = arith_colorant_type(c)(middle(gray(c)))
229239
middle(x::C, y::C) where {C<:AbstractGray} = arith_colorant_type(C)(middle(gray(x), gray(y)))
230240

test/runtests.jl

+14
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,20 @@ ColorTypes.blue(c::RatRGB) = c.b
479479
@test cf cf === (Float64(red(cf))^2 + Float64(green(cf))^2 + Float64(blue(cf))^2)/3
480480
end
481481

482+
@testset "Complement" begin
483+
@test complement(Gray(0.2)) === Gray(0.8)
484+
@test complement(AGray(0.2f0, 0.7f0)) === AGray(0.8f0, 0.7f0)
485+
@test complement(GrayA{N0f8}(0.2, 0.7)) === GrayA{N0f8}(0.8, 0.7)
486+
@test_broken complement(Gray24(0.2)) === Gray24(0.8)
487+
@test_broken complement(AGray32(0.2, 0.7)) === AGray32(0.8, 0.7)
488+
489+
@test complement(RGB(0, 0.3, 1)) === RGB(1, 0.7, 0)
490+
@test complement(ARGB(0, 0.3f0, 1, 0.7f0)) === ARGB(1, 0.7f0, 0, 0.7f0)
491+
@test complement(RGBA{N0f8}(0, 0.6, 1, 0.7)) === RGBA{N0f8}(1, 0.4, 0.0, 0.7)
492+
@test complement(RGB24(0, 0.6, 1)) === RGB24(1, 0.4, 0.0)
493+
@test complement(ARGB32(0, 0.6, 1, 0.7)) === ARGB32(1, 0.4, 0.0, 0.7)
494+
end
495+
482496
@testset "dotc" begin
483497
@test dotc(0.2, 0.2) == 0.2^2
484498
@test dotc(Int8(3), Int16(6)) === 18

0 commit comments

Comments
 (0)