From 26d940a20c24eccedf368fe890be3d37e167c0ed Mon Sep 17 00:00:00 2001 From: Ioannis Valasakis Date: Sat, 7 Mar 2020 20:52:54 +0000 Subject: [PATCH 1/2] Add var, std, complement. Migrated from Images.jl --- src/ColorVectorSpace.jl | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/ColorVectorSpace.jl b/src/ColorVectorSpace.jl index 3a97e50..16822ab 100644 --- a/src/ColorVectorSpace.jl +++ b/src/ColorVectorSpace.jl @@ -10,7 +10,7 @@ import Base: abs, abs2, clamp, convert, copy, div, eps, isfinite, isinf, import LinearAlgebra: norm import StatsBase: histrange, varm import SpecialFunctions: gamma, lgamma, lfact -import Statistics: middle +import Statistics: middle, var, std export nan @@ -207,6 +207,35 @@ for op in unaryOps @eval ($op)(c::AbstractGray) = $op(gray(c)) end +function var(A::AbstractArray{C}; kwargs...) where C<:AbstractGray + imgc = channelview(A) + base_colorant_type(C)(var(imgc; kwargs...)) +end + +function var(A::AbstractArray{C,N}; kwargs...) where {C<:Colorant,N} + imgc = channelview(A) + colons = ntuple(d->Colon(), Val(N)) + inds1 = axes(imgc, 1) + val1 = var(view(imgc, first(inds1), colons...); kwargs...) + vals = similar(imgc, typeof(val1), inds1) + vals[1] = val1 + for i in first(inds1)+1:last(inds1) + vals[i] = var(view(imgc, i, colons...); kwargs...) + end + base_colorant_type(C)(vals...) +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)) + +std(A::AbstractArray{C}; kwargs...) where {C<:Colorant} = mapc(sqrt, var(A; kwargs...)) 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))) @@ -343,4 +372,5 @@ _precompile_() @deprecate (*)(A::AbstractArray{T}, b::TransparentGray) where {T<:Number} A.*b @deprecate (*)(b::TransparentGray, A::AbstractArray{T}) where {T<:Number} A.*b +@deprecate complement(x::AbstractArray) complement.(x) end From e6041e0449cbad2a6e280b789b5b654e650c260b Mon Sep 17 00:00:00 2001 From: Ioannis Valasakis Date: Sat, 7 Mar 2020 20:53:52 +0000 Subject: [PATCH 2/2] Add test for complement. Migrated from Images.jl --- test/runtests.jl | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/runtests.jl b/test/runtests.jl index 8b8030e..210fef7 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -126,6 +126,13 @@ end @test Gray24(0.8)*0.5 === Gray(0.4) @test Gray24(0.8)/2 === Gray(0.5f0*N0f8(0.8)) @test Gray24(0.8)/2.0 === Gray(0.4) + + # Complement + @test complement(Gray(0.5)) == Gray(0.5) + @test complement(Gray(0.2)) == Gray(0.8) + @test all(complement.(img) .== 1 .- img) + # deprecated (#690) + @test all(complement.(img) .== 1 .- img) end @testset "Comparisons with Gray" begin @@ -276,6 +283,14 @@ end @test RGB24(1,0,0)/2.0 === RGB(0.5,0,0) end + @testset "Complement" begin + @test complement.([Gray(0.2)]) == [Gray(0.8)] + @test complement.([Gray{N0f8}(0.2)]) == [Gray{N0f8}(0.8)] + @test complement.([RGB(0,0.3,1)]) == [RGB(1,0.7,0)] + @test complement.([RGBA(0,0.3,1,0.7)]) == [RGBA(1.0,0.7,0.0,0.7)] + @test complement.([RGBA{N0f8}(0,0.6,1,0.7)]) == [RGBA{N0f8}(1.0,0.4,0.0,0.7)] + end + @testset "Arithemtic with RGBA" begin cf = RGBA{Float32}(0.1,0.2,0.3,0.4) @test @inferred(zero(cf)) === RGBA{Float32}(0,0,0,0)