Skip to content

Commit

Permalink
more typestable get_indexed..() and misc doc/test changes
Browse files Browse the repository at this point in the history
  • Loading branch information
cormullion committed Jan 30, 2019
1 parent 2884595 commit 3a9b44d
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 94 deletions.
40 changes: 28 additions & 12 deletions src/ColorSchemeTools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,17 @@ function extract(imfile, n=10, i=10, tolerance=0.01; kwargs...)
return ewc
end

"""/
"""
extract_weighted_colors(imfile, n=10, i=10, tolerance=0.01; shrink = 2)
Extract colors and weights of the clusters of colors in an image file. Returns a
ColorScheme and weights.
Example:
pal, wts = extract_weighted_colors(imfile, n, i, tolerance; shrink = 2)
```
pal, wts = extract_weighted_colors(imfile, n, i, tolerance; shrink = 2)
```
"""
function extract_weighted_colors(imfile, n=10, i=10, tolerance=0.01; shrink = 2.0)
img = load(imfile)
Expand Down Expand Up @@ -300,6 +302,9 @@ end
length=100,
category="",
notes="")
Make a new ColorScheme from a dictionary of linear-segment information. Calls
`get_linear_segment_color(dict, n)` with `n` for every `length` value between 0 and 1.
"""
function make_colorscheme(dict::Dict;
length=100,
Expand All @@ -311,9 +316,21 @@ function make_colorscheme(dict::Dict;
return cs
end

function lerp(x, from_min, from_max, to_min=0.0, newmax=1.0)
"""
lerp((x, from_min, from_max, to_min=0.0, to_max=1.0)
Linear interpolation of `x` between `from_min` and `from_max`.
Example
```
ColorSchemeTools.lerp(128, 0, 256)
0.5
```
"""
function lerp(x, from_min, from_max, to_min=0.0, to_max=1.0)
if !isapprox(from_max, from_min)
return ((x - from_min) / (from_max - from_min)) * (newmax - to_min) + to_min
return ((x - from_min) / (from_max - from_min)) * (to_max - to_min) + to_min
else
return from_max
end
Expand All @@ -337,26 +354,25 @@ gist_rainbow = (
)
```
To make a colorscheme, use...
To make a colorscheme, use:
```
make_indexed_list_colorscheme(gist_rainbow, :gist_rainbow)
make_colorscheme(gist_rainbow)
```
"""
function get_indexed_list_color(indexedlist, n)
n = clamp(n, 0.0, 1.0)
upper = max(2, findfirst(f -> n <= first(first(f)), indexedlist))
m = clamp(n, 0.0, 1.0)
upper = max(2, findfirst(f -> m <= first(first(f)), indexedlist))
lower = max(1, upper - 1)
lowercolorvalues = last(indexedlist[lower])
uppercolorvalues = last(indexedlist[upper])
lowerv = first(indexedlist[lower])
upperv = first(indexedlist[upper])
lr, lg, lb = lowercolorvalues
ur, ug, ub = uppercolorvalues
r = lerp(n, lowerv, upperv, lr, ur)
g = lerp(n, lowerv, upperv, lg, ug)
b = lerp(n, lowerv, upperv, lb, ub)
r = lerp(m, lowerv, upperv, lr, ur)
g = lerp(m, lowerv, upperv, lg, ug)
b = lerp(m, lowerv, upperv, lb, ub)
return round.((r, g, b), digits=6)
end

Expand Down
29 changes: 21 additions & 8 deletions src/wip/matplotlib-gnu.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using ColorSchemeTools

# translating colorschemes from https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/_cm.py
# code in here is for translating colorschemes from
# https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/_cm.py
# into ColorSchemeTools.jl compatible code

# # Gnuplot palette functions
# these are simply translated from Python
_g0(x) = 0
_g1(x) = 0.5
_g2(x) = 1
_g3(x) = x # really?
_g3(x) = x # really
_g4(x) = x^2
_g5(x) = x^3
_g6(x) = x^4
Expand Down Expand Up @@ -62,6 +64,9 @@ _g34(x) = 2x
_g35(x) = 2x - 0.5
_g36(x) = 2x - 1

# Didn't bother with building these functions into gfunc()s
# just pass the functions directly

#
# _gnuplot_data = Dict(
# :red => gfunc(7),
Expand All @@ -76,7 +81,7 @@ gnuplot = make_colorscheme(_g7, _g5, _g15)
# :green => gfunc(31),
# :blue => gfunc(32))
# =>
# this one is weird, so I fudged it thus
# this one is the only weird one, so I fudged it thus
wackyblues = _g32(range(0, stop=1, length=100))
gnuplot2 = make_colorscheme(_g30, _g31, (n) -> wackyblues[convert(Int, floor(ColorSchemeTools.lerp(n, 0, 1, 1, 100)))])

Expand Down Expand Up @@ -110,10 +115,13 @@ rainbow = make_colorscheme(_g33, _g13, _g10)
# 'green': gfunc[3],
# 'blue': gfunc[3],
# }
# don't really get this next bit, but it's easy to avoid
# def _gist_yarg(x): return 1 - x
#_gist_yarg_data = {'red': _gist_yarg, 'green': _gist_yarg, 'blue': _gist_yarg} ??
# =>

gist_gray = make_colorscheme(identity, identity, identity)
# tada!
gist_yarg = ColorScheme(reverse(gist_gray.colors))

#
Expand Down Expand Up @@ -231,11 +239,11 @@ coolwarm_dict = Dict(
(1.0, 0.150232812, 0.150232812))
)

coolwarm = make_colorscheme(coolwarm_dict)
coolwarm = make_colorscheme(coolwarm_dict, notes="blue to white to red")


# _bwr_data = ((0.0, 0.0, 1.0), (1.0, 1.0, 1.0), (1.0, 0.0, 0.0))
# this is blue to white to red
# this is another blue to white to red
# _brg_data = ((0.0, 0.0, 1.0), (1.0, 0.0, 0.0), (0.0, 1.0, 0.0))
# this is blue to red to green

Expand All @@ -260,33 +268,38 @@ brg = make_colorscheme(brg_data)
# def _flag_green(x): return np.sin( x * 31.5 * np.pi)
# def _flag_blue(x): return 0.75 * np.sin((x * 31.5 - 0.25) * np.pi) + 0.5
# _flag_data = {'red': _flag_red, 'green': _flag_green, 'blue': _flag_blue}
#is apparently what they want
# they apparently want red->white->blue->black 16 times
# =>

flag = make_colorscheme(
(n) -> 0.75 * sin((31.5n + 0.25) * π) + 0.5,
(n) -> sin(31.5n * π),
(n) -> 0.75 * sin((31.5n - 0.25) * π) + 0.5)
(n) -> 0.75 * sin((31.5n - 0.25) * π) + 0.5,
# length=300 # looks better with more samples, but probably it will be used for continuous sampling
)

#
# def _prism_red(x): return 0.75 * np.sin((x * 20.9 + 0.25) * np.pi) + 0.67
# def _prism_green(x): return 0.75 * np.sin((x * 20.9 - 0.25) * np.pi) + 0.33
# def _prism_blue(x): return -1.1 * np.sin((x * 20.9) * np.pi)
# _prism_data = {'red': _prism_red, 'green': _prism_green, 'blue': _prism_blue}
# The (9) colors of the spectrum repeating 11 times...
# =>

prism = make_colorscheme(
(n) -> 0.75 * sin((20.9n + 0.25) * π) + 0.67,
(n) -> 0.75 * sin((20.9n - 0.25) * π) + 0.33,
(n) -> -1.1 * sin(20.9n * π))

#=
# output to files
# code to output these colorschemes to files
for cs in (:rainbow, :afmhot, :ocean, :gnuplot, :gnuplot2, :gist_gray, :gist_yarg, :gist_heat, :coolwarm, :bwr, :brg, :flag, :prism)
colorscheme_to_text(Base.eval(Main, cs), String(cs), "/tmp/$(cs).jl")
end
# assemble
# who needs Unix tools...
open("/tmp/out.jl", "w") do file
for f in ("/tmp/rainbow.jl", "/tmp/prism.jl", "/tmp/flag.jl", "/tmp/brg.jl", "/tmp/bwr.jl", "/tmp/coolwarm.jl", "/tmp/gist_heat.jl", "/tmp/gist_yarg.jl", "/tmp/gist_gray.jl", "/tmp/gnuplot2.jl", "/tmp/gnuplot.jl" , "/tmp/ocean.jl", "/tmp/afmhot.jl")
write(file, read(f, String))
Expand Down
Loading

0 comments on commit 3a9b44d

Please sign in to comment.