Skip to content

Commit 56fa176

Browse files
authored
Add stdmult (#175)
This is the complement to `varmult`. Useful for finishing JuliaImages/Images.jl#971
1 parent 445ce7c commit 56fa176

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/ColorVectorSpace.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import SpecialFunctions: gamma, logabsgamma, lfact
2727
using Statistics
2828
import Statistics: middle # and `_mean_promote`
2929

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

3232
MathTypes{T,C<:Union{AbstractGray{T},AbstractRGB{T}}} = Union{C,TransparentColor{C,T}}
3333

@@ -464,6 +464,14 @@ function varmult(op, itr; corrected::Bool=true, dims=:, mean=Statistics.mean(itr
464464
end
465465
return v / (corrected ? max(1, n-1) : max(1, n))
466466
end
467+
function stdmult(op, itr; kwargs...)
468+
_sqrt(x::Real) = sqrt(x)
469+
_sqrt(c::Colorant) = mapc(sqrt, c)
470+
_sqrt(rgbrgb::RGBRGB) = RGBRGB(sqrt.(Matrix(rgbrgb)))
471+
472+
result = varmult(op, itr; kwargs...)
473+
return isa(result, Union{Real,Colorant,RGBRGB}) ? _sqrt(result) : _sqrt.(result)
474+
end
467475

468476
function __init__()
469477
if isdefined(Base, :Experimental) && isdefined(Base.Experimental, :register_error_hint)

test/runtests.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,14 +787,23 @@ ColorTypes.comp2(c::RGBA32) = alpha(c)
787787
@test varmult(, cs; mean=RGB(0, 0, 0)) (0.2^2+0.3^2+0.4^2 + 0.5^2+0.3^2+0.2^2)/3
788788
@test varmult(, cs) 2*RGB(0.15^2, 0, 0.1^2)
789789
@test Matrix(varmult(, cs)) 2*[0.15^2 0 -0.1*0.15; 0 0 0; -0.1*0.15 0 0.1^2]
790+
@test stdmult(, cs) sqrt(2*(0.15^2 + 0.1^2)/3) # the /3 is for the 3 color channels, i.e., equivalence
791+
@test stdmult(, cs; corrected=false) sqrt((0.15^2 + 0.1^2)/3)
792+
@test stdmult(, cs; mean=RGB(0, 0, 0)) sqrt((0.2^2+0.3^2+0.4^2 + 0.5^2+0.3^2+0.2^2)/3)
793+
@test stdmult(, cs) RGB(sqrt(2*0.15^2), 0, sqrt(2*0.1^2))
794+
@test_throws DomainError stdmult(, cs)
790795

791796
cs = [RGB(0.1, 0.2, 0.3) RGB(0.3, 0.5, 0.3);
792797
RGB(0.2, 0.21, 0.33) RGB(0.4, 0.51, 0.33);
793798
RGB(0.3, 0.22, 0.36) RGB(0.5, 0.52, 0.36)]
794799
v1 = RGB(0.1^2, 0.15^2, 0)
800+
s2v1 = mapc(sqrt, 2*v1)
795801
@test varmult(, cs, dims=2) 2*[v1, v1, v1]
802+
@test stdmult(, cs, dims=2) [s2v1, s2v1, s2v1]
796803
v2 = RGB(0.1^2, 0.01^2, 0.03^2)
804+
sv2 = mapc(sqrt, v2)
797805
@test varmult(, cs, dims=1) [v2 v2]
806+
@test stdmult(, cs, dims=1) [sv2 sv2]
798807
end
799808

800809
@testset "copy" begin

0 commit comments

Comments
 (0)