Skip to content

Commit 39cd626

Browse files
authored
Merge pull request #80 from JuliaGraphics/teh/middle
Switch to `oneunit` (and other smaller changes)
2 parents 101823d + c68b925 commit 39cd626

File tree

6 files changed

+96
-193
lines changed

6 files changed

+96
-193
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ os:
44
- linux
55
- osx
66
julia:
7-
- 0.5
7+
- 0.6
88
- nightly
99
notifications:
1010
email: false

REQUIRE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
julia 0.5
1+
julia 0.6
22
Colors 0.7.1
33
ColorTypes 0.4
44
FixedPointNumbers 0.3.0

appveyor.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
environment:
22
matrix:
3-
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.5/julia-0.5-latest-win32.exe"
4-
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.5/julia-0.5-latest-win64.exe"
3+
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.6/julia-0.6-latest-win32.exe"
4+
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.6/julia-0.6-latest-win64.exe"
55
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x86/julia-latest-win32.exe"
66
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x64/julia-latest-win64.exe"
77

src/ColorVectorSpace.jl

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import StatsBase: histrange
77

88
import Base: ==, +, -, *, /, ^, <, ~
99
import 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

3333
export dotc
3434

@@ -82,7 +82,7 @@ dotc(x::Real, y::Real) = dotc(promote(x, y)...)
8282
# Return types for arithmetic operations
8383
multype{A,B}(::Type{A}, ::Type{B}) = coltype(typeof(zero(A)*zero(B)))
8484
sumtype{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)))
8686
powtype{A,B}(::Type{A}, ::Type{B}) = coltype(typeof(zero(A)^zero(B)))
8787
multype(a::Colorant, b::Colorant) = multype(eltype(a),eltype(b))
8888
sumtype(a::Colorant, b::Colorant) = sumtype(eltype(a),eltype(b))
@@ -108,6 +108,13 @@ _color_rettype{C<:Colorant}(::Type{C}, ::Type{C}) = C
108108

109109
color_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))
122129
function (*){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)))
125132
end
126133
function (*){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)))
129136
end
130137
function (/){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)))
133140
end
134141
function (/){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)))
137144
end
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
164171
norm(c::AbstractRGB) = sqrt(abs2(c))
165172
norm(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+
169177
zero{C<:AbstractRGB}(::Type{C}) = C(0,0,0)
170178
zero{C<:TransparentRGB}(::Type{C}) = C(0,0,0,0)
171179
zero{C<:YCbCr}(::Type{C}) = C(0,0,0)
172180
zero{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))
174183
zero(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))
201210
end
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
@@ -214,8 +226,8 @@ end
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},
272284
end
273285

274286
zero{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

277289
dotc{T<:AbstractGray}(x::T, y::T) = acc(gray(x))*acc(gray(y))
278290
dotc(x::AbstractGray, y::AbstractGray) = dotc(promote(x, y)...)
279291

280292
float{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

297298
Compat.@dep_vectorize_2arg Gray max
298299
Compat.@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)))
354347
typemin{T<:ColorTypes.AbstractGray}(::T) = T(typemin(eltype(T)))
355348
typemax{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+
357364
end

src/dots.jl

Lines changed: 0 additions & 112 deletions
This file was deleted.

0 commit comments

Comments
 (0)