Skip to content

Commit ab3dab2

Browse files
authored
Support length on Gray StepRanges (#177)
1 parent 00236e0 commit ab3dab2

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/ColorVectorSpace.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ end
216216

217217
for f in (:mod, :rem, :mod1)
218218
@eval $f(x::Gray, m::Gray) = Gray($f(gray(x), gray(m)))
219+
@eval $f(x::Gray, m::Real) = Gray($f(gray(x), m))
219220
end
220221

221222
dotc(x::T, y::T) where {T<:Real} = acc(x)*acc(y)
@@ -477,6 +478,9 @@ function stdmult(op, itr; kwargs...)
477478
return isa(result, Union{Real,Colorant,RGBRGB}) ? _sqrt(result) : _sqrt.(result)
478479
end
479480

481+
Base.length(r::StepRange{<:AbstractGray,<:AbstractGray}) = length(StepRange(gray(r.start), gray(r.step), gray(r.stop)))
482+
Base.length(r::StepRange{<:AbstractGray}) = length(StepRange(gray(r.start), r.step, gray(r.stop)))
483+
480484
function __init__()
481485
if isdefined(Base, :Experimental) && isdefined(Base.Experimental, :register_error_hint)
482486
Base.Experimental.register_error_hint(MethodError) do io, exc, argtypes, kwargs

test/runtests.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,4 +825,22 @@ ColorTypes.comp2(c::RGBA32) = alpha(c)
825825
@test copy(c) === c
826826
end
827827

828+
@testset "ranges" begin
829+
T = Gray{N0f8}
830+
for r in (zero(T):eps(T):oneunit(T),
831+
zero(T):eps(N0f8):oneunit(T),
832+
zero(N0f8):eps(T):oneunit(N0f8),
833+
StepRangeLen(zero(T), eps(T), 256),
834+
StepRangeLen(zero(T), eps(N0f8), 256),
835+
LinRange(zero(T), oneunit(T), 256))
836+
@test length(r) == 256
837+
@test step(r) == eps(T)
838+
end
839+
840+
r = LinRange(RGB(1, 0, 0), RGB(0, 0, 1), 256)
841+
@test length(r) == 256
842+
@test step(r) == RGB(-1.0f0/255, 0, eps(N0f8))
843+
@test r[2] == RGB(254.0f0/255, 0, 1.0f0/255)
844+
end
845+
828846
end

0 commit comments

Comments
 (0)