9090multype (:: Type{A} , :: Type{B} ) where {A,B} = coltype (typeof (zero (A)* zero (B)))
9191sumtype (:: Type{A} , :: Type{B} ) where {A,B} = coltype (typeof (zero (A)+ zero (B)))
9292divtype (:: Type{A} , :: Type{B} ) where {A,B} = coltype (typeof (zero (A)/ oneunit (B)))
93+ divtype (:: Type{A} , :: Type{B} ) where {A,B<: FixedPoint } = coltype (typeof (zero (A)/ typemax (B)))
9394powtype (:: Type{A} , :: Type{B} ) where {A,B} = coltype (typeof (zero (A)^ zero (B)))
9495sumtype (a:: Colorant , b:: Colorant ) = coltype (sumtype (eltype (a),eltype (b)))
9596
@@ -131,6 +132,7 @@ rettype(::typeof(-), a, b) = parametric(color_rettype(a, b), sumtype(a, b))
131132rettype (:: typeof (* ), a, b) = parametric (color_rettype (a, b), multype (eltype (a), eltype (b))) # gray * gray
132133rettype (:: typeof (* ), a:: Real , b) = arith_colorant_type (b){multype (typeof (a), eltype (b))}
133134rettype (:: typeof (/ ), a, b:: Real ) = arith_colorant_type (a){divtype (eltype (a), typeof (b))}
135+ rettype (:: typeof (/ ), a, b) = arith_colorant_type (a){divtype (eltype (a), eltype (b))}
134136rettype (:: typeof (^ ), a, b) = arith_colorant_type (a){powtype (eltype (a), typeof (b))}
135137rettype (:: typeof (^ ), a, b:: Integer ) = arith_colorant_type (a){powtype (eltype (a), Int)}
136138
155157_mul (x:: T , y:: T ) where {T} = x * y
156158_mul (x, y) = (T = multype (typeof (x), typeof (y)); _mul (T (x), T (y)))
157159
158- _mapc (:: Type{C} , f, c) where {C<: MathTypes } = C (f .(channels (c))... )
159- _mapc (:: Type{C} , f, a, b) where {C<: MathTypes } = C (f .(channels (a), channels (b))... )
160+ function _div (x:: T , y:: T ) where {T <: FixedPoint }
161+ F = floattype (T)
162+ # range check should be done in the color constructor
163+ F (reinterpret (x)) / F (reinterpret (y))
164+ end
165+ function _div (x:: AbstractFloat , y:: Normed )
166+ F = divtype (typeof (x), typeof (y))
167+ F (x) * F (reinterpret (oneunit (y))) / F (reinterpret (y))
168+ end
169+ _div (x:: AbstractFloat , y:: Integer ) = _mul (x, oneunit (x) / y)
170+ _div (x:: T , y:: T ) where {T} = x / y
171+ _div (x, y) = (T = divtype (typeof (x), typeof (y)); _div (T (x), T (y)))
172+
173+ @inline _mapc (:: Type{C} , f, c) where {C<: MathTypes } = C (f .(channels (c))... )
174+ @inline _mapc (:: Type{C} , f, a, b) where {C<: MathTypes } = C (f .(channels (a), channels (b))... )
160175
161176# # Generic algorithms
162177Base. add_sum (c1:: MathTypes ,c2:: MathTypes ) = mapc (Base. add_sum, c1, c2)
@@ -202,12 +217,11 @@ complement(x::TransparentColor) = typeof(x)(complement(color(x)), alpha(x))
202217copy (c:: MathTypes ) = c
203218(* )(f:: Real , c:: MathTypes ) = _mapc (rettype (* , f, c), v -> _mul (f, v), c)
204219(* )(c:: MathTypes , f:: Real ) = (* )(f, c)
220+ (/ )(c:: MathTypes , f:: Real ) = _mapc (rettype (/ , c, f), v -> _div (v, f), c)
205221(+ )(c:: MathTypes ) = mapc (+ , c)
206222(+ )(c:: MathTypes{Bool} ) = c
207223(- )(c:: MathTypes ) = mapc (- , c)
208224(- )(c:: MathTypes{Bool} ) = c
209- (/ )(c:: MathTypes , f:: Real ) = (one (f)/ f)* c
210- (/ )(c:: MathTypes , f:: Integer ) = (one (eltype (c))/ f)* c
211225abs (c:: MathTypes ) = mapc (abs, c)
212226norm (c:: MathTypes , p:: Real = 2 ) = (cc = channels (c); norm (cc, p)/ (p == 0 ? length (cc) : length (cc)^ (1 / p)))
213227(⊙ )(a:: C , b:: C ) where {C<: MathTypes } = _mapc (rettype (* , a, b), _mul, a, b)
@@ -223,13 +237,9 @@ norm(c::MathTypes, p::Real=2) = (cc = channels(c); norm(cc, p)/(p == 0 ? length(
223237
224238
225239# Scalar RGB
226- function (/ )(c:: AbstractRGB{T} , f:: Real ) where T<: Normed
227- fs = (one (f)/ reinterpret (oneunit (T)))/ f
228- rettype (/ , c, f)(fs* reinterpret (red (c)), fs* reinterpret (green (c)), fs* reinterpret (blue (c)))
229- end
230- function (/ )(c:: AbstractRGB{T} , f:: Integer ) where T<: Normed
231- fs = (1 / reinterpret (oneunit (T)))/ f
232- rettype (/ , c, f)(fs* reinterpret (red (c)), fs* reinterpret (green (c)), fs* reinterpret (blue (c)))
240+ function (/ )(c:: C , f:: AbstractFloat ) where {C<: Union{AbstractRGB, TransparentRGB} }
241+ r = oneunit (divtype (eltype (c), typeof (f))) / f
242+ _mapc (rettype (/ , c, f), v -> v * r, c)
233243end
234244(+ )(a:: AbstractRGB , b:: AbstractRGB ) = rettype (+ , a, b)(red (a)+ red (b), green (a)+ green (b), blue (a)+ blue (b))
235245(- )(a:: AbstractRGB , b:: AbstractRGB ) = rettype (- , a, b)(red (a)- red (b), green (a)- green (b), blue (a)- blue (b))
@@ -279,16 +289,15 @@ end
279289middle (c:: AbstractGray ) = arith_colorant_type (c)(middle (gray (c)))
280290middle (x:: C , y:: C ) where {C<: AbstractGray } = arith_colorant_type (C)(middle (gray (x), gray (y)))
281291
282- (/ )(n:: Number , c:: AbstractGray ) = base_color_type (c)(n / gray (c))
292+ (/ )(n:: Number , c:: AbstractGray ) = base_color_type (c)(_div ( real (n), gray (c) ))
283293(+ )(a:: AbstractGray , b:: AbstractGray ) = rettype (+ , a, b)(gray (a)+ gray (b))
284294(+ )(a:: TransparentGray , b:: TransparentGray ) = rettype (+ , a, b)(gray (a)+ gray (b), alpha (a)+ alpha (b))
285295(- )(a:: AbstractGray , b:: AbstractGray ) = rettype (- , a, b)(gray (a)- gray (b))
286296(- )(a:: TransparentGray , b:: TransparentGray ) = rettype (- , a, b)(gray (a)- gray (b), alpha (a)- alpha (b))
287297(* )(a:: AbstractGray , b:: AbstractGray ) = a ⊙ b
288298(^ )(a:: AbstractGray , b:: Integer ) = rettype (^ , a, b)(gray (a)^ convert (Int,b))
289299(^ )(a:: AbstractGray , b:: Real ) = rettype (^ , a, b)(gray (a)^ b)
290- (/ )(a:: C , b:: C ) where C<: AbstractGray = base_color_type (C)(gray (a)/ gray (b))
291- (/ )(a:: AbstractGray , b:: AbstractGray ) = / (promote (a, b)... )
300+ (/ )(a:: AbstractGray , b:: AbstractGray ) = rettype (/ , a, b)(_div (gray (a), gray (b)))
292301(+ )(a:: AbstractGray , b:: Number ) = base_color_type (a)(gray (a)+ b)
293302(+ )(a:: Number , b:: AbstractGray ) = b+ a
294303(- )(a:: AbstractGray , b:: Number ) = base_color_type (a)(gray (a)- b)
0 commit comments