Skip to content

Commit dad1e06

Browse files
authored
Implement promotion in multiplication (#141)
Fixes #109
1 parent 66dda94 commit dad1e06

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ RGBRGB{Float64}(
8383

8484
Note that `c1⋅c2 = (c1.r*c2.r + c1.g*c2.g + c1.b*c2.b)/3`, where the division by 3 ensures the equivalence `norm(x) == norm(Gray(x)) == norm(RGB(x, x, x))`.
8585

86-
It is designed to not support the ordinary multiplication operation `*` because it is not obvious which one of these should be the default option.
86+
Ordinary multiplication `*` is not supported because it is not obvious which one of these should be the default option.
8787

8888
However, `*` is defined for grayscale since all these three multiplication operations (i.e., ``, `` and ``) are equivalent in the 1D vector space.
8989

src/ColorVectorSpace.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,9 @@ end
180180

181181
# New multiplication operators
182182
()(x::AbstractRGB, y::AbstractRGB) = (T = acctype(eltype(x), eltype(y)); T(red(x))*T(red(y)) + T(green(x))*T(green(y)) + T(blue(x))*T(blue(y)))/3
183+
()(x::Union{AbstractRGB,AbstractGray}, y::Union{AbstractRGB,AbstractGray}) = (promote(x, y)...)
183184
()(x::C, y::C) where C<:AbstractRGB = base_color_type(C)(red(x)*red(y), green(x)*green(y), blue(x)*blue(y))
184-
()(x::AbstractRGB, y::AbstractRGB) = (promote(x, y)...)
185+
()(x::Union{AbstractRGB,AbstractGray}, y::Union{AbstractRGB,AbstractGray}) = (promote(x, y)...)
185186
# ⊗ defined below
186187

187188
isfinite(c::Colorant{T}) where {T<:Normed} = true
@@ -362,6 +363,7 @@ function ⊗(a::AbstractRGB, b::AbstractRGB)
362363
agbr, abbg, arbb, abbr, arbg, agbb = ag*br, ab*bg, ar*bb, ab*br, ar*bg, ag*bb
363364
return RGBRGB(ar*br, agbr, abbr, arbg, ag*bg, abbg, arbb, agbb, ab*bb)
364365
end
366+
(a::Union{AbstractRGB,AbstractGray}, b::Union{AbstractRGB,AbstractGray}) = (promote(a, b)...)
365367

366368
"""
367369
varmult(op, itr; corrected::Bool=true, mean=Statistics.mean(itr), dims=:)

test/runtests.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,12 @@ ColorTypes.blue(c::RatRGB) = c.b
466466
@test RGB24(0.4, 0.6, 0.5) - AGray32(0.4, 0.2) === ARGB32(0, 0.2, 0.1, 0.8)
467467
@test ARGB32(0.4, 0, 0.2, 0.5) + Gray24(0.4) === ARGB32(0.8, 0.4, 0.6, 0.5N0f8+1N0f8)
468468
@test ARGB32(0.4, 0, 0.2, 0.5) + AGray32(0.4, 0.2) === ARGB32(0.8, 0.4, 0.6, 0.5N0f8+0.2N0f8)
469+
470+
g, rgb = Gray(0.2), RGB(0.1, 0.2, 0.3)
471+
@test g rgb == rgb g 0.2*(0.1 + 0.2 + 0.3)/3
472+
@test g rgb == rgb g RGB(0.2*0.1, 0.2^2, 0.2*0.3)
473+
@test g rgb == RGB(g) rgb
474+
@test rgb g == rgb RGB(g)
469475
end
470476

471477
@testset "Custom RGB arithmetic" begin

0 commit comments

Comments
 (0)