Skip to content

Commit

Permalink
Merge pull request #304 from fredrikekre/fe/07
Browse files Browse the repository at this point in the history
fixes for julia 0.7
  • Loading branch information
timholy authored Jun 21, 2018
2 parents 3d53e10 + bef069c commit 6ec22e2
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 71 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ os:
- osx
- linux
julia:
- 0.6
- 0.7
- nightly
notifications:
email: false
Expand Down
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,13 @@ A seed color or array of seed colors may be provided to `distinguishable_colors`
```julia
distinguishable_colors{T<:Color}(n::Integer, seed::AbstractVector{T};
transform::Function = identity,
lchoices::AbstractVector = linspace(0, 100, 15),
cchoices::AbstractVector = linspace(0, 100, 15),
hchoices::AbstractVector = linspace(0, 340, 20)
lchoices::AbstractVector = range(0, stop=100, length=15),
cchoices::AbstractVector = range(0, stop=100, length=15),
hchoices::AbstractVector = range(0, stop=340, length=20)
)
```

By default, `distinguishable_colors` chooses maximally distinguishable colors from the outer product of lightness, chroma and hue values specified by `lchoices = linspace(0, 100, 15)`, `cchoices = linspace(0, 100, 15)`, and `hchoices = linspace(0, 340, 20)`. The set of colors that `distinguishable_colors` chooses from may be specified by passing different choices as keyword arguments.
By default, `distinguishable_colors` chooses maximally distinguishable colors from the outer product of lightness, chroma and hue values specified by `lchoices = range(0, stop=100, length=15)`, `cchoices = range(0, stop=100, length=15)`, and `hchoices = range(0, stop=340, length=20)`. The set of colors that `distinguishable_colors` chooses from may be specified by passing different choices as keyword arguments.

Distinguishability is maximized with respect to the CIEDE2000 color difference formula (see `colordiff`). If a `transform` function is specified, color difference is instead maximized between colors `a` and `b` according to
`colordiff(transform(a), transform(b))`.
Expand All @@ -198,18 +198,18 @@ Color arrays generated by `distinguishable_colors` are particularly useful for i
```
vars = 1:10
cols = distinguishable_colors(length(vars)+1,[RGB(1,1,1)])[2:end]
pcols = map(col->(red(col),green(col),blue(col)),cols)
pcols = map(col->(red(col),green(col),blue(col)),cols)
for i = 1:length(vars)
plot(1:10,rand(10),c = pcols[i])
end
```

#### `linspace`
#### `range`

`linspace(c1::Color, c2::Color, n=100)`
`range(start::Color; stop::Color, length=100)`

Generates `n` colors in a linearly interpolated ramp from `c1` to
`c2`, inclusive, returning an `Array` of colors
Generates `n` colors in a linearly interpolated ramp from `start` to
`stop`, inclusive, returning an `Array` of colors

#### `weighted_color_mean`

Expand Down
7 changes: 3 additions & 4 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
julia 0.6
ColorTypes 0.6.3
FixedPointNumbers 0.3.0
julia 0.7-alpha
ColorTypes 0.7.0
FixedPointNumbers 0.5.0
Reexport
Compat 0.32
12 changes: 8 additions & 4 deletions src/Colors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ __precompile__()

module Colors

using FixedPointNumbers, ColorTypes, Reexport, Compat
using FixedPointNumbers, ColorTypes, Reexport, Printf
@reexport using ColorTypes
# deprecated exports
export U8, U16

AbstractGray{T} = Color{T,1}
using ColorTypes: TransparentGray
Expand All @@ -15,7 +13,7 @@ Color3{T} = Color{T,3}
Transparent4{C<:Color3,T} = TransparentColor{C,T,4}

import Base: ==, +, -, *, /
import Base: convert, eltype, hex, isless, linspace, show, typemin, typemax
import Base: convert, eltype, hex, isless, range, show, typemin, typemax

# Additional exports, not exported by ColorTypes
export weighted_color_mean,
Expand All @@ -26,6 +24,12 @@ export weighted_color_mean,
MSC, sequential_palette, diverging_palette, colormap,
colormatch, CIE1931_CMF, CIE1964_CMF, CIE1931J_CMF, CIE1931JV_CMF

# atan(x,y) was introduced after 0.7.0-alpha
@static if VERSION < v"0.7.0-alpha.44"
atan(x, y) = atan2(x, y)
atan(x) = Base.atan(x)
end

# Early utilities
include("utilities.jl")

Expand Down
4 changes: 0 additions & 4 deletions src/algorithms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,6 @@ protanopic(c::Color) = protanopic(c, 1.0)
deuteranopic(c::Color) = deuteranopic(c, 1.0)
tritanopic(c::Color) = tritanopic(c, 1.0)

Compat.@dep_vectorize_1arg Color protanopic
Compat.@dep_vectorize_1arg Color deuteranopic
Compat.@dep_vectorize_1arg Color tritanopic

# MSC - Most Saturated Colorant for given hue h
# ---------------------

Expand Down
26 changes: 13 additions & 13 deletions src/colormaps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,41 +23,41 @@ Args:
Keyword arguments:
- `transform`: Transform applied to colors before measuring distance. Default is the identity; other choices include `deuteranopic` to simulate color-blindness.
- `lchoices`: Possible lightness values (default `linspace(0,100,15)`)
- `cchoices`: Possible chroma values (default `linspace(0,100,15)`)
- `hchoices`: Possible hue values (default `linspace(0,340,20)`)
- `lchoices`: Possible lightness values (default `range(0,stop=100,start=15)`)
- `cchoices`: Possible chroma values (default `range(0,stop=100,start=15)`)
- `hchoices`: Possible hue values (default `range(0,stop=340,start=20)`)
Returns:
A `Vector` of colors of length `n`, of the type specified in `seed`.
"""
function distinguishable_colors(n::Integer,
seed::AbstractVector{T};
transform::Function = identity,
lchoices::AbstractVector = linspace(0, 100, 15),
cchoices::AbstractVector = linspace(0, 100, 15),
hchoices::AbstractVector = linspace(0, 340, 20)) where T<:Color
lchoices::AbstractVector = range(0, stop=100, length=15),
cchoices::AbstractVector = range(0, stop=100, length=15),
hchoices::AbstractVector = range(0, stop=340, length=20)) where T<:Color
if n <= length(seed)
return seed[1:n]
end

# Candidate colors
N = length(lchoices)*length(cchoices)*length(hchoices)
candidate = Vector{Lab{Float64}}(N)
candidate = Vector{Lab{Float64}}(undef, N)
j = 0
for h in hchoices, c in cchoices, l in lchoices
rgb = convert(RGB, LCHab(l, c, h))
candidate[j+=1] = convert(LCHab, rgb)
end

# Transformed colors
candidate_t = Vector{Lab{Float64}}(N)
candidate_t = Vector{Lab{Float64}}(undef, N)
for i = 1:N
candidate_t[i] = transform(candidate[i])
end

# Start with the seed colors
colors = Vector{T}(n)
copy!(colors, seed)
colors = Vector{T}(undef, n)
copyto!(colors, seed)

# Minimum distances of the current color to each previously selected color.
ds = fill(Inf, N)
Expand All @@ -69,7 +69,7 @@ function distinguishable_colors(n::Integer,
end

for i in length(seed)+1:n
j = indmax(ds)
j = argmax(ds)
colors[i] = candidate[j]
tc = candidate_t[j]
for k = 1:N
Expand All @@ -83,7 +83,7 @@ end


distinguishable_colors(n::Integer, seed::Color; kwargs...) = distinguishable_colors(n, [seed]; kwargs...)
distinguishable_colors(n::Integer; kwargs...) = distinguishable_colors(n, Vector{RGB{N0f8}}(0); kwargs...)
distinguishable_colors(n::Integer; kwargs...) = distinguishable_colors(n, Vector{RGB{N0f8}}(); kwargs...)

@deprecate distinguishable_colors(n::Integer,
transform::Function,
Expand Down Expand Up @@ -159,7 +159,7 @@ function sequential_palette(h,
if logscale
absc = logspace(-2.,0.,N)
else
absc = linspace(0.,1.,N)
absc = range(0.,stop=1.,length=N)
end

for t in absc
Expand Down
16 changes: 8 additions & 8 deletions src/conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,8 @@ cnvt(::Type{XYZ{T}}, c::LCHuv) where {T} = cnvt(XYZ{T}, convert(Luv{T}, c))
function cnvt(::Type{XYZ{T}}, c::DIN99d) where T

# Go back to C-h space
# FIXME: Clean this up (why is there no atan2d?)
h = rad2deg(atan2(c.b,c.a)) + 50
# FIXME: Clean this up (why is there no atand?)
h = rad2deg(atan(c.b,c.a)) + 50
while h > 360; h -= 360; end
while h < 0; h += 360; end

Expand Down Expand Up @@ -492,7 +492,7 @@ function cnvt(::Type{Lab{T}}, c::DIN99o) where T
co = sqrt(c.a^2 + c.b^2)

# hue angle h99o
h = atan2(c.b, c.a)
h = atan(c.b, c.a)

# revert rotation by 26°
ho= rad2deg(h)-26
Expand Down Expand Up @@ -559,7 +559,7 @@ cnvt(::Type{Luv{T}}, c::Color3) where {T} = cnvt(Luv{T}, convert(XYZ{T}, c))
# -------------------

function cnvt(::Type{LCHuv{T}}, c::Luv) where T
h = rad2deg(atan2(c.v, c.u))
h = rad2deg(atan(c.v, c.u))
while h > 360; h -= 360; end
while h < 0; h += 360; end
LCHuv{T}(c.l, sqrt(c.u^2 + c.v^2), h)
Expand All @@ -573,7 +573,7 @@ cnvt(::Type{LCHuv{T}}, c::Color3) where {T} = cnvt(LCHuv{T}, convert(Luv{T}, c))
# -------------------

function cnvt(::Type{LCHab{T}}, c::Lab) where T
h = rad2deg(atan2(c.b, c.a))
h = rad2deg(atan(c.b, c.a))
while h > 360; h -= 360; end
while h < 0; h += 360; end
LCHab{T}(c.l, sqrt(c.a^2 + c.b^2), h)
Expand Down Expand Up @@ -656,8 +656,8 @@ function cnvt(::Type{DIN99d{T}}, c::XYZ{T}) where T

# Calculate hue/chroma
C = 22.5*log(1+0.06*G)
# FIXME: Clean this up (why is there no atan2d?)
h = rad2deg(atan2(f,ee)) + 50
# FIXME: Clean this up (why is there no atand?)
h = rad2deg(atan(f,ee)) + 50
while h > 360; h -= 360; end
while h < 0; h += 360; end

Expand Down Expand Up @@ -692,7 +692,7 @@ function cnvt(::Type{DIN99o{T}}, c::Lab) where T

# Temporary value for chroma
go = sqrt(eo^2 + fo^2)
ho = atan2(fo,eo)
ho = atan(fo,eo)
# rotation of the color space by 26°
h = rad2deg(ho) + 26

Expand Down
22 changes: 11 additions & 11 deletions src/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@ const col_pat_hsla = r"hsla\((\d+%?),(\d+%?),(\d+%?),(\d+(?:\.\d*)?%?)\)"
# Parse a number used in the "rgb()" or "hsl()" color.
function parse_rgb(num::AbstractString)
if num[end] == '%'
return clamp(parse(Int, num[1:end-1], 10) / 100, 0, 1)
return clamp(parse(Int, num[1:end-1], base=10) / 100, 0, 1)
else
return clamp(parse(Int, num, 10) / 255, 0, 1)
return clamp(parse(Int, num, base=10) / 255, 0, 1)
end
end

function parse_hsl_hue(num::AbstractString)
if num[end] == '%'
error("hue cannot end in %")
else
return parse(Int, num, 10)
return parse(Int, num, base=10)
end
end

function parse_hsl_sl(num::AbstractString)
if num[end] != '%'
error("saturation and lightness must end in %")
else
return parse(Int, num[1:end-1], 10) / 100
return parse(Int, num[1:end-1], base=10) / 100
end
end

Expand All @@ -48,19 +48,19 @@ end


function _parse_colorant(desc::AbstractString)
desc_ = replace(desc, " ", "")
desc_ = replace(desc, " " => "")
mat = match(col_pat_hex2, desc_)
if mat != nothing
return RGB{N0f8}(parse(Int, mat.captures[2], 16) / 255,
parse(Int, mat.captures[3], 16) / 255,
parse(Int, mat.captures[4], 16) / 255)
return RGB{N0f8}(parse(Int, mat.captures[2], base=16) / 255,
parse(Int, mat.captures[3], base=16) / 255,
parse(Int, mat.captures[4], base=16) / 255)
end

mat = match(col_pat_hex1, desc_)
if mat != nothing
return RGB{N0f8}(parse(Int, mat.captures[2], 16) / 15,
parse(Int, mat.captures[3], 16) / 15,
parse(Int, mat.captures[4], 16) / 15)
return RGB{N0f8}(parse(Int, mat.captures[2], base=16) / 15,
parse(Int, mat.captures[3], base=16) / 15,
parse(Int, mat.captures[4], base=16) / 15)
end

mat = match(col_pat_rgb, desc_)
Expand Down
13 changes: 8 additions & 5 deletions src/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ hex(c::Colorant) = hex(convert(ARGB, c))
"""
weighted_color_mean(w1, c1, c2)
Returns the color `w1*c1 + (1-w1)*c2` that is the weighted mean of `c1` and
Returns the color `w1*c1 + (1-w1)*c2` that is the weighted mean of `c1` and
`c2`, where `c1` has a weight 0 ≤ `w1` ≤ 1.
"""
function weighted_color_mean(w1::Real, c1::Colorant, c2::Colorant)
Expand All @@ -49,15 +49,18 @@ function weighted_color_mean(w1::Real, c1::Gray{Bool}, c2::Gray{Bool})
end

"""
linspace(c1::Color, c2::Color, n=100)
range(start::Color; stop::Color, length=100)
Generates `n`>2 colors in a linearly interpolated ramp from `c1` to`c2`,
Generates `n`>2 colors in a linearly interpolated ramp from `start` to`stop`,
inclusive, returning an `Array` of colors.
"""
function linspace(c1::T, c2::T, n::Integer=100) where T<:Colorant
return T[weighted_color_mean(w1, c1, c2) for w1 in linspace(1.0,0.0,n)]
function range(start::T; stop::T, length::Integer=100) where T<:Colorant
return T[weighted_color_mean(w1, start, stop) for w1 in range(1.0,stop=0.0,length=length)]
end

import Base: linspace
Base.@deprecate linspace(start::Colorant, stop::Colorant, n::Integer=100) range(start, stop=stop, length=n)

#Double quadratic Bezier curve
function Bezier(t::T, p0::T, p2::T, q0::T, q1::T, q2::T) where T<:Real
B(t,a,b,c)=a*(1.0-t)^2.0+2.0b*(1.0-t)*t+c*t^2.0
Expand Down
8 changes: 4 additions & 4 deletions test/algorithms.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using Base.Test, Colors
using Test, Colors

@test isconcrete(eltype(colormap("Grays")))
@test isconcretetype(eltype(colormap("Grays")))

@test_throws ArgumentError colormap("Grays", N=10)

col = distinguishable_colors(10)
@test isconcrete(eltype(col))
@test isconcretetype(eltype(col))
mindiff = Inf
for i = 1:10
for j = i+1:10
mindiff = min(mindiff, colordiff(col[i], col[j]))
global mindiff = min(mindiff, colordiff(col[i], col[j]))
end
end
@test mindiff > 8
2 changes: 1 addition & 1 deletion test/conversion.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Colors, FixedPointNumbers, JLD2
using Base.Test
using Test
using ColorTypes: eltype_default

r8(x) = reinterpret(N0f8, x)
Expand Down
2 changes: 1 addition & 1 deletion test/din99.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const testdata = [
# results due to the exponentiation.
const conveps = 0.05
const diffeps = 0.01
let a, b
let a, b, metric
for (i, (a, b)) in enumerate(testdata)
converted = convert(DIN99, Lab(a...))
test = DIN99(b...)
Expand Down
3 changes: 1 addition & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Colors, Base.Test
using Compat
using Colors, Test
@test isempty(detect_ambiguities(Colors, Base, Core))

include("algorithms.jl")
Expand Down
8 changes: 4 additions & 4 deletions test/utilities.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Colors, FixedPointNumbers, Base.Test
using Colors, FixedPointNumbers, Test, InteractiveUtils

# test utility function weighted_color_mean
parametric2 = [GrayA,AGray32,AGray]
Expand Down Expand Up @@ -38,13 +38,13 @@ for C in colortypes
end
end

# test utility function linspace
# linspace uses weighted_color_mean which is extensively tested.
# test utility function range
# range uses weighted_color_mean which is extensively tested.
# Therefore it suffices to test the function using gray colors.
for T in colorElementTypes
c1 = Gray(T(1))
c2 = Gray(T(0))
linc1c2 = linspace(c1,c2,43)
linc1c2 = range(c1,stop=c2,length=43)
@test length(linc1c2) == 43
@test linc1c2[1] == c1
@test linc1c2[end] == c2
Expand Down

0 comments on commit 6ec22e2

Please sign in to comment.