Skip to content

Commit 00236e0

Browse files
authored
Support reductions with Gray{Bool} (#176)
1 parent 5504756 commit 00236e0

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/ColorVectorSpace.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,15 @@ _div(x, y) = (T = divtype(typeof(x), typeof(y)); _div(T(x), T(y)))
193193

194194
## Generic algorithms
195195
Base.add_sum(c1::MathTypes,c2::MathTypes) = mapc(Base.add_sum, c1, c2)
196+
Base.add_sum(c1::MathTypes{Bool}, c2::MathTypes{Bool}) = mapc((x1, x2) -> FixedPointNumbers.Treduce(x1) + FixedPointNumbers.Treduce(x2), c1, c2)
196197
Base.reduce_first(::typeof(Base.add_sum), c::MathTypes) = mapc(x->Base.reduce_first(Base.add_sum, x), c)
198+
Base.reduce_first(::typeof(Base.add_sum), c::MathTypes{Bool}) = mapc(x->Base.reduce_first(Base.add_sum, N0f8(x)), c)
197199
function Base.reduce_empty(::typeof(Base.add_sum), ::Type{T}) where {T<:MathTypes}
198200
z = Base.reduce_empty(Base.add_sum, eltype(T))
199201
return zero(base_colorant_type(T){typeof(z)})
200202
end
203+
Base.reduce_empty(::typeof(Base.add_sum), ::Type{T}) where {T<:MathTypes{Bool}} =
204+
Base.reduce_empty(Base.add_sum, base_colorant_type(T){N0f8})
201205

202206

203207
## Rounding & mod

test/runtests.jl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,14 @@ ColorTypes.comp2(c::RGBA32) = alpha(c)
211211
@test sum(a) == Gray(n8sum(0.8,0.7))
212212
@test sum(a[1:1]) == a[1]
213213
@test abs( varmult(*, a) - (a[1]-a[2])^2 / 2 ) <= 0.001
214+
ab = Gray[true, true]
215+
@test sum(ab) === Gray(n8sum(true, true))
216+
@test prod(ab) === Gray(true)
217+
ab = Gray[true]
218+
@test sum(ab) === Gray(n8sum(true, false))
219+
@test prod(ab) === Gray(true)
220+
@test sum(Gray{Bool}[]) === Gray(n8sum(false, false))
221+
@test prod(Gray{Bool}[]) === one(Gray{Bool})
214222

215223
@test real(Gray{Float32}) <: Real
216224
@test zero(ColorTypes.Gray) == 0
@@ -427,10 +435,14 @@ ColorTypes.comp2(c::RGBA32) = alpha(c)
427435
a = RGB{N0f8}[RGB(1,0,0), RGB(1,0.8,0)]
428436
@test sum(a) == RGB(2.0,0.8,0)
429437
@test sum(typeof(a)()) == RGB(0.0,0.0,0)
438+
ab = [RGB(true, false, true), RGB(true, false, false)]
439+
@test sum(ab) === RGB(n8sum(true, true), n8sum(false, false), n8sum(true, false))
440+
ab = [RGB(true, false, true)]
441+
@test sum(ab) === RGB(n8sum(true, false), n8sum(false, false), n8sum(true, false))
442+
430443
@test isapprox(a, a)
431444
a = RGB{Float64}(1.0, 1.0, 0.9999999999999999)
432445
b = RGB{Float64}(1.0, 1.0, 1.0)
433-
434446
@test isapprox(a, b)
435447
a = RGB{Float64}(1.0, 1.0, 0.99)
436448
@test !(isapprox(a, b, rtol = 0.01))

0 commit comments

Comments
 (0)