@@ -7,7 +7,7 @@ import StatsBase: histrange
77
88import Base: == , + , - , * , / , ^ , < , ~
99import Base: abs, abs2, clamp, convert, copy, div, eps, isfinite, isinf,
10- isnan, isless, length, mapreduce, norm, one , promote_array_type,
10+ isnan, isless, length, mapreduce, norm, oneunit , promote_array_type,
1111 promote_op, promote_rule, zero, trunc, floor, round, ceil, bswap,
1212 mod, rem, atan2, hypot, max, min, varm, real, typemin, typemax
1313
@@ -28,7 +28,7 @@ import Base: conj, sin, cos, tan, sinh, cosh, tanh,
2828 gamma, lfact, frexp, modf, airy, airyai,
2929 airyprime, airyaiprime, airybi, airybiprime,
3030 besselj0, besselj1, bessely0, bessely1,
31- eta, zeta, digamma, float
31+ eta, zeta, digamma, float, middle
3232
3333export dotc
3434
@@ -82,7 +82,7 @@ dotc(x::Real, y::Real) = dotc(promote(x, y)...)
8282# Return types for arithmetic operations
8383multype {A,B} (:: Type{A} , :: Type{B} ) = coltype (typeof (zero (A)* zero (B)))
8484sumtype {A,B} (:: Type{A} , :: Type{B} ) = coltype (typeof (zero (A)+ zero (B)))
85- divtype {A,B} (:: Type{A} , :: Type{B} ) = coltype (typeof (zero (A)/ one (B)))
85+ divtype {A,B} (:: Type{A} , :: Type{B} ) = coltype (typeof (zero (A)/ oneunit (B)))
8686powtype {A,B} (:: Type{A} , :: Type{B} ) = coltype (typeof (zero (A)^ zero (B)))
8787multype (a:: Colorant , b:: Colorant ) = multype (eltype (a),eltype (b))
8888sumtype (a:: Colorant , b:: Colorant ) = sumtype (eltype (a),eltype (b))
@@ -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} )
123- fs = f* (1 / reinterpret (one (T)))
124- base_colorant_type (c){multype (typeof (f),T)}(fs* reinterpret (red (c)), fs* reinterpret (green (c)), fs* reinterpret (blue (c)))
130+ fs = f* (1 / reinterpret (oneunit (T)))
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} )
127- fs = reinterpret (f)* (1 / widen (reinterpret (one (T)))^ 2 )
128- base_colorant_type (c){multype (typeof (f),T)}(fs* reinterpret (red (c)), fs* reinterpret (green (c)), fs* reinterpret (blue (c)))
134+ fs = reinterpret (f)* (1 / widen (reinterpret (oneunit (T)))^ 2 )
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 )
131- fs = (one (f)/ reinterpret (one (T)))/ f
132- base_colorant_type (c){divtype (typeof (f),T)}(fs* reinterpret (red (c)), fs* reinterpret (green (c)), fs* reinterpret (blue (c)))
138+ fs = (one (f)/ reinterpret (oneunit (T)))/ f
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 )
135- fs = (1 / reinterpret (one (T)))/ f
136- base_colorant_type (c){divtype (typeof (f),T)}(fs* reinterpret (red (c)), fs* reinterpret (green (c)), fs* reinterpret (blue (c)))
142+ fs = (1 / reinterpret (oneunit (T)))/ f
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))
@@ -164,13 +171,15 @@ abs2(c::TransparentRGB) = (ret = abs2(color(c)); ret + convert(typeof(ret), alph
164171norm (c:: AbstractRGB ) = sqrt (abs2 (c))
165172norm (c:: TransparentRGB ) = sqrt (abs2 (c))
166173
167- one {C<:AbstractRGB} (:: Type{C} ) = C (1 ,1 ,1 )
168- one {C<:TransparentRGB} (:: Type{C} ) = C (1 ,1 ,1 ,1 )
174+ oneunit {C<:AbstractRGB} (:: Type{C} ) = C (1 ,1 ,1 )
175+ oneunit {C<:TransparentRGB} (:: Type{C} ) = C (1 ,1 ,1 ,1 )
176+
169177zero {C<:AbstractRGB} (:: Type{C} ) = C (0 ,0 ,0 )
170178zero {C<:TransparentRGB} (:: Type{C} ) = C (0 ,0 ,0 ,0 )
171179zero {C<:YCbCr} (:: Type{C} ) = C (0 ,0 ,0 )
172180zero {C<:HSV} (:: Type{C} ) = C (0 ,0 ,0 )
173- one (p:: Colorant ) = one (typeof (p))
181+ oneunit (p:: Colorant ) = oneunit (typeof (p))
182+ Base. one (c:: Colorant ) = Base. one (typeof (c))
174183zero (p:: Colorant ) = zero (typeof (p))
175184
176185# These constants come from squaring the conversion to grayscale
@@ -200,8 +209,11 @@ for op in unaryOps
200209 @eval ($ op)(c:: AbstractGray ) = $ op (gray (c))
201210end
202211
203- (* )(f:: Real , c:: AbstractGray ) = base_colorant_type (c){multype (typeof (f),eltype (c))}(f* gray (c))
204- (* )(f:: Real , c:: TransparentGray ) = base_colorant_type (c){multype (typeof (f),eltype (c))}(f* gray (c), f* alpha (c))
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)))
214+
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))
205217(* )(c:: AbstractGray , f:: Real ) = (* )(f, c)
206218(* )(c:: TransparentGray , f:: Real ) = (* )(f, c)
207219(/ )(c:: AbstractGray , f:: Real ) = (one (f)/ f)* c
214226(- ){S,T}(a:: AbstractGray{S} , b:: AbstractGray{T} ) = color_rettype (a,b){sumtype (S,T)}(gray (a)- gray (b))
215227(- )(a:: TransparentGray , b:: TransparentGray ) = color_rettype (a,b){sumtype (eltype (a),eltype (b))}(gray (a)- gray (b),alpha (a)- alpha (b))
216228(* ){S,T}(a:: AbstractGray{S} , b:: AbstractGray{T} ) = color_rettype (a,b){multype (S,T)}(gray (a)* gray (b))
217- (^ ){S}(a:: AbstractGray{S} , b:: Integer ) = base_colorant_type (a){powtype (S,Int)}(gray (a)^ convert (Int,b))
218- (^ ){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)
219231(+ )(c:: AbstractGray ) = c
220232(+ )(c:: TransparentGray ) = c
221233(- )(c:: AbstractGray ) = typeof (c)(- gray (c))
@@ -272,38 +284,19 @@ function Base.isapprox{Cx<:MathTypes,Cy<:MathTypes}(x::AbstractArray{Cx},
272284end
273285
274286zero {C<:TransparentGray} (:: Type{C} ) = C (0 ,0 )
275- one {C<:TransparentGray} (:: Type{C} ) = C (1 ,1 )
287+ oneunit {C<:TransparentGray} (:: Type{C} ) = C (1 ,1 )
276288
277289dotc {T<:AbstractGray} (x:: T , y:: T ) = acc (gray (x))* acc (gray (y))
278290dotc (x:: AbstractGray , y:: AbstractGray ) = dotc (promote (x, y)... )
279291
280292float {T<:Gray} (:: Type{T} ) = typeof (float (zero (T)))
281293
282294# Mixed types
283- if VERSION < v " 0.6.0-dev.2009"
284- (+ )(a:: MathTypes , b:: MathTypes ) = (+ )(promote (a, b)... )
285- (- )(a:: MathTypes , b:: MathTypes ) = (- )(promote (a, b)... )
286- else
287- (+ )(a:: MathTypes , b:: MathTypes ) = (+ )(Base. promote_noncircular (a, b)... )
288- (- )(a:: MathTypes , b:: MathTypes ) = (- )(Base. promote_noncircular (a, b)... )
289- end
290-
291- if VERSION < v " 0.6.0-dev.1839"
292- include (" dots.jl" )
293- elseif VERSION < v " 0.6.0-pre.beta.101"
294- Base. Broadcast. eltypestuple (c:: Colorant ) = Tuple{typeof (c)}
295- end
295+ (+ )(a:: MathTypes , b:: MathTypes ) = (+ )(Base. promote_noncircular (a, b)... )
296+ (- )(a:: MathTypes , b:: MathTypes ) = (- )(Base. promote_noncircular (a, b)... )
296297
297298Compat. @dep_vectorize_2arg Gray max
298299Compat. @dep_vectorize_2arg Gray min
299- for f in (:min , :max )
300- @eval begin
301- @deprecate ($ f {T<:Gray} (x:: Number , y:: AbstractArray{T} ),
302- @compat $ f .(x, y))
303- @deprecate ($ f {T<:Gray} (x:: AbstractArray{T} , y:: Number ),
304- @compat $ f .(x, y))
305- end
306- end
307300
308301# Arrays
309302+ {C<: MathTypes }(A:: AbstractArray{C} ) = A
@@ -354,4 +347,18 @@ typemax{T<:ColorTypes.AbstractGray}(::Type{T}) = T(typemax(eltype(T)))
354347typemin {T<:ColorTypes.AbstractGray} (:: T ) = T (typemin (eltype (T)))
355348typemax {T<:ColorTypes.AbstractGray} (:: T ) = T (typemax (eltype (T)))
356349
350+ # deprecations
351+ function Base. one (:: Type{C} ) where {C<: Union{TransparentGray,AbstractRGB,TransparentRGB} }
352+ Base. depwarn (" one($C ) will soon switch to returning 1; you might need to switch to `oneunit`" , :one )
353+ C (_onetuple (C)... )
354+ end
355+ _onetuple (:: Type{C} ) where {C<: Colorant{T,N} } where {T,N} = ntuple (d-> 1 , Val (N))
356+
357+ for f in (:min , :max )
358+ @eval begin
359+ @deprecate ($ f {T<:Gray} (x:: Number , y:: AbstractArray{T} ), $ f .(x, y))
360+ @deprecate ($ f {T<:Gray} (x:: AbstractArray{T} , y:: Number ), $ f .(x, y))
361+ end
362+ end
363+
357364end
0 commit comments