@@ -61,6 +61,19 @@ divtype(::Type{A}, ::Type{B}) where {A,B} = coltype(typeof(zero(A)/oneunit(B)))
6161powtype (:: Type{A} , :: Type{B} ) where {A,B} = coltype (typeof (zero (A)^ zero (B)))
6262sumtype (a:: Colorant , b:: Colorant ) = coltype (sumtype (eltype (a),eltype (b)))
6363
64+ multype (:: Type{Bool} , :: Type{B} ) where {B} = B
65+ multype (:: Type{A} , :: Type{Bool} ) where {A} = A <: Integer ? multype (A, N0f8) : A
66+ multype (:: Type{Bool} , :: Type{Bool} ) = Bool
67+ sumtype (:: Type{Bool} , :: Type{B} ) where {B} = sumtype (B <: Integer ? N0f8 : UInt8, B)
68+ sumtype (:: Type{A} , :: Type{Bool} ) where {A} = sumtype (Bool, A)
69+ sumtype (:: Type{Bool} , :: Type{Bool} ) = Bool
70+ divtype (:: Type{Bool} , :: Type{B} ) where {B} = divtype (B <: Integer ? N0f8 : UInt8, B)
71+ divtype (:: Type{A} , :: Type{Bool} ) where {A} = divtype (A, N0f8)
72+ divtype (:: Type{Bool} , :: Type{Bool} ) = divtype (Bool, N0f8)
73+ powtype (:: Type{Bool} , :: Type{B} ) where {B} = B <: Integer ? Bool : B
74+ powtype (:: Type{A} , :: Type{Bool} ) where {A} = A
75+ powtype (:: Type{Bool} , :: Type{Bool} ) = Bool
76+
6477coltype (:: Type{T} ) where {T<: Fractional } = T
6578coltype (:: Type{T} ) where {T<: Number } = floattype (T)
6679
@@ -140,12 +153,15 @@ dotc(x::Real, y::Real) = dotc(promote(x, y)...)
140153# # Math on Colors. These implementations encourage inlining and,
141154# # for the case of Normed types, nearly halve the number of multiplications (for RGB)
142155
156+ _neg (x) = - x
157+ _neg (x:: Bool ) = x # GF(2)
158+
143159# Scalar RGB
144160copy (c:: AbstractRGB ) = c
145161(+ )(c:: AbstractRGB ) = mapc (+ , c)
146162(+ )(c:: TransparentRGB ) = mapc (+ , c)
147- (- )(c:: AbstractRGB ) = mapc (- , c)
148- (- )(c:: TransparentRGB ) = mapc (- , c)
163+ (- )(c:: AbstractRGB ) = mapc (_neg , c)
164+ (- )(c:: TransparentRGB ) = mapc (_neg , c)
149165(* )(f:: Real , c:: AbstractRGB ) = arith_colorant_type (c){multype (typeof (f),eltype (c))}(f* red (c), f* green (c), f* blue (c))
150166(* )(f:: Real , c:: TransparentRGB ) = arith_colorant_type (c){multype (typeof (f),eltype (c))}(f* red (c), f* green (c), f* blue (c), f* alpha (c))
151167function (* )(f:: Real , c:: AbstractRGB{T} ) where T<: Normed
@@ -250,28 +266,42 @@ end
250266(* )(f:: Real , c:: TransparentGray ) = arith_colorant_type (c){multype (typeof (f),eltype (c))}(f* gray (c), f* alpha (c))
251267(* )(c:: AbstractGray , f:: Real ) = (* )(f, c)
252268(* )(c:: TransparentGray , f:: Real ) = (* )(f, c)
253- (/ )(c:: AbstractGray , f:: Real ) = (one (f )/ f)* c
254- (/ )(n :: Number , c :: AbstractGray ) = base_color_type (c)(n / gray (c))
255- (/ )(c :: TransparentGray , f :: Real ) = ( one (f) / f) * c
256- (/ )(c:: AbstractGray , f:: Integer ) = (one ( eltype (c))/ f)* c
269+ (/ )(c:: AbstractGray , f:: Real ) = (oneunit ( divtype ( eltype (c), typeof (f)) )/ f)* c
270+ (/ )(c :: AbstractGray{Bool} , f :: Real ) = arith_colorant_type (c){ divtype (Bool, typeof (f))}( gray (c))/ f
271+ (/ )(n :: Number , c :: AbstractGray ) = arith_colorant_type (c){ divtype ( typeof ( real (n)), eltype (c))}(n / gray (c))
272+ (/ )(c:: TransparentGray , f:: Real ) = (oneunit ( divtype ( eltype (c), typeof (f) ))/ f)* c
257273(/ )(c:: TransparentGray , f:: Integer ) = (one (eltype (c))/ f)* c
258274(+ )(a:: AbstractGray{S} , b:: AbstractGray{T} ) where {S,T} = parametric (color_rettype (a,b), sumtype (S,T))(gray (a)+ gray (b))
275+ (+ )(a:: AbstractGray{Bool} , b:: AbstractGray{T} ) where {T} = parametric (color_rettype (a,b), sumtype (Bool,T))(gray (a)) + b
276+ (+ )(a:: AbstractGray{S} , b:: AbstractGray{Bool} ) where {S} = b + a
277+ (+ )(a:: AbstractGray{Bool} , b:: AbstractGray{Bool} ) = parametric (color_rettype (a,b), Bool)(gray (a) ⊻ gray (b))
259278(+ )(a:: TransparentGray , b:: TransparentGray ) = parametric (color_rettype (a,b), sumtype (eltype (a),eltype (b)))(gray (a)+ gray (b),alpha (a)+ alpha (b))
260279(- )(a:: AbstractGray{S} , b:: AbstractGray{T} ) where {S,T} = parametric (color_rettype (a,b), sumtype (S,T))(gray (a)- gray (b))
280+ (- )(a:: AbstractGray{Bool} , b:: AbstractGray{T} ) where {T} = parametric (color_rettype (a,b), sumtype (Bool,T))(gray (a)) - b
281+ (- )(a:: AbstractGray{S} , b:: AbstractGray{Bool} ) where {S} = a - parametric (color_rettype (a,b), sumtype (S,Bool))(gray (b))
282+ (- )(a:: AbstractGray{Bool} , b:: AbstractGray{Bool} ) = a + b # GF(2)
261283(- )(a:: TransparentGray , b:: TransparentGray ) = parametric (color_rettype (a,b), sumtype (eltype (a),eltype (b)))(gray (a)- gray (b),alpha (a)- alpha (b))
262284(* )(a:: AbstractGray{S} , b:: AbstractGray{T} ) where {S,T} = parametric (color_rettype (a,b), multype (S,T))(gray (a)* gray (b))
263285(^ )(a:: AbstractGray{S} , b:: Integer ) where {S} = arith_colorant_type (a){powtype (S,Int)}(gray (a)^ convert (Int,b))
264286(^ )(a:: AbstractGray{S} , b:: Real ) where {S} = arith_colorant_type (a){powtype (S,typeof (b))}(gray (a)^ b)
265287(+ )(c:: AbstractGray ) = c
266288(+ )(c:: TransparentGray ) = c
267- (- )(c:: AbstractGray ) = typeof (c)(- gray (c))
268- (- )(c:: TransparentGray ) = typeof (c)(- gray (c), - alpha (c))
289+ (- )(c:: AbstractGray ) = typeof (c)(_neg ( gray (c) ))
290+ (- )(c:: TransparentGray ) = typeof (c)(_neg ( gray (c)), _neg ( alpha (c) ))
269291(/ )(a:: C , b:: C ) where C<: AbstractGray = base_color_type (C)(gray (a)/ gray (b))
270292(/ )(a:: AbstractGray , b:: AbstractGray ) = / (promote (a, b)... )
293+ (/ )(a:: AbstractGray{Bool} , b:: AbstractGray ) = / (UInt8 (gray (a)), b)
294+ (/ )(a:: AbstractGray{Bool} , b:: AbstractGray{Bool} ) = gray (b) ? a : throw (DivideError ())
271295(+ )(a:: AbstractGray , b:: Number ) = base_color_type (a)(gray (a)+ b)
296+ (+ )(a:: AbstractGray{Bool} , b:: Number ) = arith_colorant_type (a){sumtype (Bool, typeof (b))}(gray (a)) + b
297+ (+ )(a:: AbstractGray{Bool} , b:: Bool ) = a + UInt8 (b)
272298(+ )(a:: Number , b:: AbstractGray ) = b+ a
273299(- )(a:: AbstractGray , b:: Number ) = base_color_type (a)(gray (a)- b)
274300(- )(a:: Number , b:: AbstractGray ) = base_color_type (b)(a- gray (b))
301+ (- )(a:: AbstractGray{Bool} , b:: Number ) = arith_colorant_type (a){sumtype (Bool, typeof (b))}(gray (a)) - b
302+ (- )(a:: Number , b:: AbstractGray{Bool} ) = a - arith_colorant_type (b){sumtype (typeof (a), Bool)}(gray (b))
303+ (- )(a:: AbstractGray{Bool} , b:: Bool ) = a - UInt8 (b)
304+ (- )(a:: Bool , b:: AbstractGray{Bool} ) = UInt8 (a) - b
275305
276306(⋅ )(x:: AbstractGray , y:: AbstractGray ) = gray (x)* gray (y)
277307(⊙ )(x:: C , y:: C ) where C<: AbstractGray = base_color_type (C)(gray (x)* gray (y))
0 commit comments