Skip to content

Commit 0d853b0

Browse files
committed
Deprecate old implementation of one in favor of oneunit
1 parent adafb1a commit 0d853b0

File tree

2 files changed

+34
-18
lines changed

2 files changed

+34
-18
lines changed

src/ColorVectorSpace.jl

Lines changed: 26 additions & 18 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

@@ -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))
@@ -120,19 +120,19 @@ copy(c::AbstractRGB) = c
120120
(*)(f::Real, c::AbstractRGB) = base_colorant_type(c){multype(typeof(f),eltype(c))}(f*red(c), f*green(c), f*blue(c))
121121
(*)(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))
122122
function (*){T<:Normed}(f::Real, c::AbstractRGB{T})
123-
fs = f*(1/reinterpret(one(T)))
123+
fs = f*(1/reinterpret(oneunit(T)))
124124
base_colorant_type(c){multype(typeof(f),T)}(fs*reinterpret(red(c)), fs*reinterpret(green(c)), fs*reinterpret(blue(c)))
125125
end
126126
function (*){T<:Normed}(f::Normed, c::AbstractRGB{T})
127-
fs = reinterpret(f)*(1/widen(reinterpret(one(T)))^2)
127+
fs = reinterpret(f)*(1/widen(reinterpret(oneunit(T)))^2)
128128
base_colorant_type(c){multype(typeof(f),T)}(fs*reinterpret(red(c)), fs*reinterpret(green(c)), fs*reinterpret(blue(c)))
129129
end
130130
function (/){T<:Normed}(c::AbstractRGB{T}, f::Real)
131-
fs = (one(f)/reinterpret(one(T)))/f
131+
fs = (one(f)/reinterpret(oneunit(T)))/f
132132
base_colorant_type(c){divtype(typeof(f),T)}(fs*reinterpret(red(c)), fs*reinterpret(green(c)), fs*reinterpret(blue(c)))
133133
end
134134
function (/){T<:Normed}(c::AbstractRGB{T}, f::Integer)
135-
fs = (1/reinterpret(one(T)))/f
135+
fs = (1/reinterpret(oneunit(T)))/f
136136
base_colorant_type(c){divtype(typeof(f),T)}(fs*reinterpret(red(c)), fs*reinterpret(green(c)), fs*reinterpret(blue(c)))
137137
end
138138
(+){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 +164,15 @@ abs2(c::TransparentRGB) = (ret = abs2(color(c)); ret + convert(typeof(ret), alph
164164
norm(c::AbstractRGB) = sqrt(abs2(c))
165165
norm(c::TransparentRGB) = sqrt(abs2(c))
166166

167-
one{C<:AbstractRGB}(::Type{C}) = C(1,1,1)
168-
one{C<:TransparentRGB}(::Type{C}) = C(1,1,1,1)
167+
oneunit{C<:AbstractRGB}(::Type{C}) = C(1,1,1)
168+
oneunit{C<:TransparentRGB}(::Type{C}) = C(1,1,1,1)
169+
169170
zero{C<:AbstractRGB}(::Type{C}) = C(0,0,0)
170171
zero{C<:TransparentRGB}(::Type{C}) = C(0,0,0,0)
171172
zero{C<:YCbCr}(::Type{C}) = C(0,0,0)
172173
zero{C<:HSV}(::Type{C}) = C(0,0,0)
173-
one(p::Colorant) = one(typeof(p))
174+
oneunit(p::Colorant) = oneunit(typeof(p))
175+
Base.one(c::Colorant) = Base.one(typeof(c))
174176
zero(p::Colorant) = zero(typeof(p))
175177

176178
# These constants come from squaring the conversion to grayscale
@@ -272,7 +274,7 @@ function Base.isapprox{Cx<:MathTypes,Cy<:MathTypes}(x::AbstractArray{Cx},
272274
end
273275

274276
zero{C<:TransparentGray}(::Type{C}) = C(0,0)
275-
one{C<:TransparentGray}(::Type{C}) = C(1,1)
277+
oneunit{C<:TransparentGray}(::Type{C}) = C(1,1)
276278

277279
dotc{T<:AbstractGray}(x::T, y::T) = acc(gray(x))*acc(gray(y))
278280
dotc(x::AbstractGray, y::AbstractGray) = dotc(promote(x, y)...)
@@ -285,14 +287,6 @@ float{T<:Gray}(::Type{T}) = typeof(float(zero(T)))
285287

286288
Compat.@dep_vectorize_2arg Gray max
287289
Compat.@dep_vectorize_2arg Gray min
288-
for f in (:min, :max)
289-
@eval begin
290-
@deprecate($f{T<:Gray}(x::Number, y::AbstractArray{T}),
291-
@compat $f.(x, y))
292-
@deprecate($f{T<:Gray}(x::AbstractArray{T}, y::Number),
293-
@compat $f.(x, y))
294-
end
295-
end
296290

297291
# Arrays
298292
+{C<:MathTypes}(A::AbstractArray{C}) = A
@@ -343,4 +337,18 @@ typemax{T<:ColorTypes.AbstractGray}(::Type{T}) = T(typemax(eltype(T)))
343337
typemin{T<:ColorTypes.AbstractGray}(::T) = T(typemin(eltype(T)))
344338
typemax{T<:ColorTypes.AbstractGray}(::T) = T(typemax(eltype(T)))
345339

340+
# deprecations
341+
function Base.one(::Type{C}) where {C<:Union{TransparentGray,AbstractRGB,TransparentRGB}}
342+
Base.depwarn("one($C) will soon switch to returning 1; you might need to switch to `oneunit`", :one)
343+
C(_onetuple(C)...)
344+
end
345+
_onetuple(::Type{C}) where {C<:Colorant{T,N}} where {T,N} = ntuple(d->1, Val(N))
346+
347+
for f in (:min, :max)
348+
@eval begin
349+
@deprecate($f{T<:Gray}(x::Number, y::AbstractArray{T}), $f.(x, y))
350+
@deprecate($f{T<:Gray}(x::AbstractArray{T}, y::Number), $f.(x, y))
351+
end
352+
end
353+
346354
end

test/runtests.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ end
146146

147147
@testset "Unary operations with Gray" begin
148148
for g in (Gray(0.4), Gray{N0f8}(0.4))
149+
@test @inferred(zero(g)) === typeof(g)(0)
150+
@test @inferred(oneunit(g)) === typeof(g)(1)
149151
for op in ColorVectorSpace.unaryOps
150152
try
151153
v = @eval $op(gray(g)) # if this fails, don't bother
@@ -161,6 +163,8 @@ end
161163

162164
@testset "Arithmetic with GrayA" begin
163165
p1 = GrayA{Float32}(Gray(0.8), 0.2)
166+
@test @inferred(zero(p1)) === GrayA{Float32}(0,0)
167+
@test @inferred(oneunit(p1)) === GrayA{Float32}(1,1)
164168
@test +p1 == p1
165169
@test -p1 == GrayA(-0.8f0, -0.2f0)
166170
p2 = GrayA{Float32}(Gray(0.6), 0.3)
@@ -193,6 +197,8 @@ end
193197

194198
@testset "Arithemtic with RGB" begin
195199
cf = RGB{Float32}(0.1,0.2,0.3)
200+
@test @inferred(zero(cf)) === RGB{Float32}(0,0,0)
201+
@test @inferred(oneunit(cf)) === RGB{Float32}(1,1,1)
196202
@test +cf == cf
197203
@test -cf == RGB(-0.1f0, -0.2f0, -0.3f0)
198204
ccmp = RGB{Float32}(0.2,0.4,0.6)
@@ -261,6 +267,8 @@ end
261267

262268
@testset "Arithemtic with RGBA" begin
263269
cf = RGBA{Float32}(0.1,0.2,0.3,0.4)
270+
@test @inferred(zero(cf)) === RGBA{Float32}(0,0,0,0)
271+
@test @inferred(oneunit(cf)) === RGBA{Float32}(1,1,1,1)
264272
@test +cf == cf
265273
@test -cf == RGBA(-0.1f0, -0.2f0, -0.3f0, -0.4f0)
266274
ccmp = RGBA{Float32}(0.2,0.4,0.6,0.8)

0 commit comments

Comments
 (0)