@@ -108,6 +108,13 @@ _color_rettype{C<:Colorant}(::Type{C}, ::Type{C}) = C
108108
109109color_rettype (c1:: Colorant , c2:: Colorant ) = color_rettype (typeof (c1), typeof (c2))
110110
111+ arith_colorant_type (:: C ) where {C<: Colorant } = arith_colorant_type (C)
112+ arith_colorant_type (:: Type{C} ) where {C<: Colorant } = base_colorant_type (C)
113+ arith_colorant_type (:: Type{Gray24} ) = Gray
114+ arith_colorant_type (:: Type{AGray32} ) = AGray
115+ arith_colorant_type (:: Type{RGB24} ) = RGB
116+ arith_colorant_type (:: Type{ARGB32} ) = ARGB
117+
111118# # Math on Colors. These implementations encourage inlining and,
112119# # for the case of Normed types, nearly halve the number of multiplications (for RGB)
113120
@@ -117,23 +124,23 @@ copy(c::AbstractRGB) = c
117124(+ )(c:: TransparentRGB ) = mapc (+ , c)
118125(- )(c:: AbstractRGB ) = mapc (- , c)
119126(- )(c:: TransparentRGB ) = mapc (- , c)
120- (* )(f:: Real , c:: AbstractRGB ) = base_colorant_type (c){multype (typeof (f),eltype (c))}(f* red (c), f* green (c), f* blue (c))
121- (* )(f:: Real , c:: TransparentRGB ) = base_colorant_type (c){multype (typeof (f),eltype (c))}(f* red (c), f* green (c), f* blue (c), f* alpha (c))
127+ (* )(f:: Real , c:: AbstractRGB ) = arith_colorant_type (c){multype (typeof (f),eltype (c))}(f* red (c), f* green (c), f* blue (c))
128+ (* )(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))
122129function (* ){T<: Normed }(f:: Real , c:: AbstractRGB{T} )
123130 fs = f* (1 / reinterpret (oneunit (T)))
124- base_colorant_type (c){multype (typeof (f),T)}(fs* reinterpret (red (c)), fs* reinterpret (green (c)), fs* reinterpret (blue (c)))
131+ arith_colorant_type (c){multype (typeof (f),T)}(fs* reinterpret (red (c)), fs* reinterpret (green (c)), fs* reinterpret (blue (c)))
125132end
126133function (* ){T<: Normed }(f:: Normed , c:: AbstractRGB{T} )
127134 fs = reinterpret (f)* (1 / widen (reinterpret (oneunit (T)))^ 2 )
128- base_colorant_type (c){multype (typeof (f),T)}(fs* reinterpret (red (c)), fs* reinterpret (green (c)), fs* reinterpret (blue (c)))
135+ arith_colorant_type (c){multype (typeof (f),T)}(fs* reinterpret (red (c)), fs* reinterpret (green (c)), fs* reinterpret (blue (c)))
129136end
130137function (/ ){T<: Normed }(c:: AbstractRGB{T} , f:: Real )
131138 fs = (one (f)/ reinterpret (oneunit (T)))/ f
132- base_colorant_type (c){divtype (typeof (f),T)}(fs* reinterpret (red (c)), fs* reinterpret (green (c)), fs* reinterpret (blue (c)))
139+ arith_colorant_type (c){divtype (typeof (f),T)}(fs* reinterpret (red (c)), fs* reinterpret (green (c)), fs* reinterpret (blue (c)))
133140end
134141function (/ ){T<: Normed }(c:: AbstractRGB{T} , f:: Integer )
135142 fs = (1 / reinterpret (oneunit (T)))/ f
136- base_colorant_type (c){divtype (typeof (f),T)}(fs* reinterpret (red (c)), fs* reinterpret (green (c)), fs* reinterpret (blue (c)))
143+ arith_colorant_type (c){divtype (typeof (f),T)}(fs* reinterpret (red (c)), fs* reinterpret (green (c)), fs* reinterpret (blue (c)))
137144end
138145(+ ){S,T}(a:: AbstractRGB{S} , b:: AbstractRGB{T} ) = color_rettype (a, b){sumtype (S,T)}(red (a)+ red (b), green (a)+ green (b), blue (a)+ blue (b))
139146(- ){S,T}(a:: AbstractRGB{S} , b:: AbstractRGB{T} ) = color_rettype (a, b){sumtype (S,T)}(red (a)- red (b), green (a)- green (b), blue (a)- blue (b))
@@ -202,11 +209,11 @@ for op in unaryOps
202209 @eval ($ op)(c:: AbstractGray ) = $ op (gray (c))
203210end
204211
205- middle (c:: AbstractGray ) = base_colorant_type (c)(middle (gray (c)))
206- middle (x:: C , y:: C ) where {C<: AbstractGray } = C (middle (gray (x), gray (y)))
212+ middle (c:: AbstractGray ) = arith_colorant_type (c)(middle (gray (c)))
213+ middle (x:: C , y:: C ) where {C<: AbstractGray } = arith_colorant_type (C) (middle (gray (x), gray (y)))
207214
208- (* )(f:: Real , c:: AbstractGray ) = base_colorant_type (c){multype (typeof (f),eltype (c))}(f* gray (c))
209- (* )(f:: Real , c:: TransparentGray ) = base_colorant_type (c){multype (typeof (f),eltype (c))}(f* gray (c), f* alpha (c))
215+ (* )(f:: Real , c:: AbstractGray ) = arith_colorant_type (c){multype (typeof (f),eltype (c))}(f* gray (c))
216+ (* )(f:: Real , c:: TransparentGray ) = arith_colorant_type (c){multype (typeof (f),eltype (c))}(f* gray (c), f* alpha (c))
210217(* )(c:: AbstractGray , f:: Real ) = (* )(f, c)
211218(* )(c:: TransparentGray , f:: Real ) = (* )(f, c)
212219(/ )(c:: AbstractGray , f:: Real ) = (one (f)/ f)* c
@@ -219,8 +226,8 @@ middle(x::C, y::C) where {C<:AbstractGray} = C(middle(gray(x), gray(y)))
219226(- ){S,T}(a:: AbstractGray{S} , b:: AbstractGray{T} ) = color_rettype (a,b){sumtype (S,T)}(gray (a)- gray (b))
220227(- )(a:: TransparentGray , b:: TransparentGray ) = color_rettype (a,b){sumtype (eltype (a),eltype (b))}(gray (a)- gray (b),alpha (a)- alpha (b))
221228(* ){S,T}(a:: AbstractGray{S} , b:: AbstractGray{T} ) = color_rettype (a,b){multype (S,T)}(gray (a)* gray (b))
222- (^ ){S}(a:: AbstractGray{S} , b:: Integer ) = base_colorant_type (a){powtype (S,Int)}(gray (a)^ convert (Int,b))
223- (^ ){S}(a:: AbstractGray{S} , b:: Real ) = base_colorant_type (a){powtype (S,typeof (b))}(gray (a)^ b)
229+ (^ ){S}(a:: AbstractGray{S} , b:: Integer ) = arith_colorant_type (a){powtype (S,Int)}(gray (a)^ convert (Int,b))
230+ (^ ){S}(a:: AbstractGray{S} , b:: Real ) = arith_colorant_type (a){powtype (S,typeof (b))}(gray (a)^ b)
224231(+ )(c:: AbstractGray ) = c
225232(+ )(c:: TransparentGray ) = c
226233(- )(c:: AbstractGray ) = typeof (c)(- gray (c))
0 commit comments