@@ -108,6 +108,13 @@ _color_rettype{C<:Colorant}(::Type{C}, ::Type{C}) = C
108
108
109
109
color_rettype (c1:: Colorant , c2:: Colorant ) = color_rettype (typeof (c1), typeof (c2))
110
110
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
+
111
118
# # Math on Colors. These implementations encourage inlining and,
112
119
# # for the case of Normed types, nearly halve the number of multiplications (for RGB)
113
120
@@ -117,23 +124,23 @@ copy(c::AbstractRGB) = c
117
124
(+ )(c:: TransparentRGB ) = mapc (+ , c)
118
125
(- )(c:: AbstractRGB ) = mapc (- , c)
119
126
(- )(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))
122
129
function (* ){T<: Normed }(f:: Real , c:: AbstractRGB{T} )
123
130
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)))
125
132
end
126
133
function (* ){T<: Normed }(f:: Normed , c:: AbstractRGB{T} )
127
134
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)))
129
136
end
130
137
function (/ ){T<: Normed }(c:: AbstractRGB{T} , f:: Real )
131
138
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)))
133
140
end
134
141
function (/ ){T<: Normed }(c:: AbstractRGB{T} , f:: Integer )
135
142
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)))
137
144
end
138
145
(+ ){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))
139
146
(- ){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
202
209
@eval ($ op)(c:: AbstractGray ) = $ op (gray (c))
203
210
end
204
211
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)))
207
214
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))
210
217
(* )(c:: AbstractGray , f:: Real ) = (* )(f, c)
211
218
(* )(c:: TransparentGray , f:: Real ) = (* )(f, c)
212
219
(/ )(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)))
219
226
(- ){S,T}(a:: AbstractGray{S} , b:: AbstractGray{T} ) = color_rettype (a,b){sumtype (S,T)}(gray (a)- gray (b))
220
227
(- )(a:: TransparentGray , b:: TransparentGray ) = color_rettype (a,b){sumtype (eltype (a),eltype (b))}(gray (a)- gray (b),alpha (a)- alpha (b))
221
228
(* ){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)
224
231
(+ )(c:: AbstractGray ) = c
225
232
(+ )(c:: TransparentGray ) = c
226
233
(- )(c:: AbstractGray ) = typeof (c)(- gray (c))
0 commit comments