Skip to content

Commit cff5f4b

Browse files
committed
Support abs2
1 parent 65cc014 commit cff5f4b

File tree

4 files changed

+13
-12
lines changed

4 files changed

+13
-12
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ColorVectorSpace"
22
uuid = "c3611d14-8923-5661-9e6a-0046d554d3a4"
3-
version = "0.9.5"
3+
version = "0.9.6"
44

55
[deps]
66
ColorTypes = "3da002f7-5984-5a60-b8a6-cbb66c0b333f"

README.md

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,9 @@ RGBRGB{Float64}:
113113

114114
### `abs` and `abs2`
115115

116-
To begin with, there is no general and straightforward definition of the
117-
absolute value of a vector.
118116
There are roughly two possible definitions of `abs`/`abs2`: as a channel-wise
119117
operator or as a function which returns a real number based on the norm.
120118
For the latter, there are also variations in the definition of norm.
121119

122120
In ColorVectorSpace v0.9 and later, `abs` is defined as a channel-wise operator
123-
and `abs2` is undefined.
124-
The following are alternatives for the definitions in ColorVectorSpace v0.8 and
125-
earlier.
126-
```julia
127-
_abs(c) = mapreducec(v->abs(float(v)), +, 0, c)
128-
_abs2(c) = mapreducec(v->float(v)^2, +, 0, c)
129-
```
121+
and `abs2` returns a scalar, and `abs2(c) == c⋅c ≈ norm(c)^2`.

src/ColorVectorSpace.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ if !hasmethod(zero, (Type{TransparentGray},))
4141
zero(p::Colorant) = zero(typeof(p))
4242
end
4343

44+
if !hasmethod(one, (Type{Gray24},))
45+
Base.one(::Type{Gray24}) = Gray24(1)
46+
end
47+
4448
if !hasmethod(one, (Type{TransparentGray},)) # specification change is planned for ColorTypes v0.12
4549
Base.one(::Type{C}) where {C<:TransparentGray} = C(1,1)
4650
Base.one(::Type{C}) where {C<:AbstractRGB} = C(1,1,1)
@@ -242,6 +246,7 @@ copy(c::MathTypes) = c
242246
(-)(c::MathTypes{Bool}) = c
243247
abs(c::MathTypes) = mapc(abs, c)
244248
norm(c::MathTypes, p::Real=2) = (cc = channels(c); norm(cc, p)/(p == 0 ? length(cc) : length(cc)^(1/p)))
249+
Base.abs2(c::MathTypes) = c c
245250
()(a::C, b::C) where {C<:MathTypes} = _mapc(rettype(*, a, b), _mul, a, b)
246251
()(a::C, b::C) where {C<:MathTypes} = throw(MethodError(dot, (a, b)))
247252
()(a::C, b::C) where {C<:MathTypes} = throw(MethodError(tensor, (a, b)))

test/runtests.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ ColorTypes.comp2(c::RGBA32) = alpha(c)
116116
@test_throws MethodError cf ÷ cf
117117
@test cf + 0.1 === 0.1 + cf === Gray(Float64(0.1f0) + 0.1)
118118
@test cf64 - 0.1f0 === -(0.1f0 - cf64) === Gray( 0.2 - Float64(0.1f0))
119-
@test_throws MethodError abs2(ccmp)
119+
@test abs2(ccmp) === abs2(gray(ccmp))
120120
@test norm(cf) == norm(cf, 2) == norm(gray(cf))
121121
@test norm(cf, 1) == norm(gray(cf), 1)
122122
@test norm(cf, Inf) == norm(gray(cf), Inf)
@@ -384,7 +384,9 @@ ColorTypes.comp2(c::RGBA32) = alpha(c)
384384
@test isinf(RGB(1, Inf, 0.5))
385385
@test !isnan(RGB(1, Inf, 0.5))
386386
@test abs(RGB(0.1,0.2,0.3)) == RGB(0.1,0.2,0.3)
387-
@test_throws MethodError abs2(RGB(0.1,0.2,0.3))
387+
cv = RGB(0.1,0.2,0.3)
388+
@test abs2(cv) == cv cv
389+
@test abs2(cv) norm(cv)^2
388390
@test_throws MethodError sum(abs2, RGB(0.1,0.2,0.3))
389391
@test norm(RGB(0.1,0.2,0.3)) sqrt(0.14)/sqrt(3)
390392

@@ -774,6 +776,8 @@ ColorTypes.comp2(c::RGBA32) = alpha(c)
774776
@test varmult(, cs, dims=2) 2*[v1, v1, v1]
775777
v2 = RGB(0.1^2, 0.01^2, 0.03^2)
776778
@test varmult(, cs, dims=1) [v2 v2]
779+
780+
@test var(cs) == varmult(, cs)
777781
end
778782

779783
@testset "copy" begin

0 commit comments

Comments
 (0)