Skip to content

Commit

Permalink
caching colors in scales to improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
gka committed Nov 6, 2013
1 parent 8764b07 commit 98b90f7
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 36 deletions.
52 changes: 33 additions & 19 deletions chroma.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion chroma.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name" : "chroma-js",
"description" : "JavaScript library for color conversions",
"version" : "0.5.3",
"version" : "0.5.4",
"author" : "Gregor Aisch",
"homepage" : "https://github.com/gka/chroma.js",
"keywords" : ["color"],
Expand Down
45 changes: 30 additions & 15 deletions src/scale.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ chroma.scale = (colors, positions) ->
_max = 1
_correctLightness = false
_numClasses = 0
_colorCache = {}

# private methods

Expand All @@ -70,6 +71,7 @@ chroma.scale = (colors, positions) ->
_pos = []
for c in [0..colors.length-1]
_pos.push c/(colors.length-1)
resetCache()
_colors = colors

setDomain = (domain = []) ->
Expand All @@ -80,6 +82,7 @@ chroma.scale = (colors, positions) ->
_domain = domain
_min = domain[0]
_max = domain[domain.length-1]
resetCache()
if domain.length == 2
_numClasses = 0
else
Expand Down Expand Up @@ -121,23 +124,32 @@ chroma.scale = (colors, positions) ->
if not bypassMap
t = tmap t # lightness correction

if type(_colors) == 'array'
for i in [0.._pos.length-1]
p = _pos[i]
if t <= p
col = _colors[i]
break
if t >= p and i == _pos.length-1
col = _colors[i]
break
if t > p and t < _pos[i+1]
t = (t-p)/(_pos[i+1]-p)
col = chroma.interpolate _colors[i], _colors[i+1], t, _mode
break
else if type(_colors) == 'function'
col = _colors t
k = Math.floor(t * 10000)

if _colorCache[k]
col = _colorCache[k]
else
if type(_colors) == 'array'
for i in [0.._pos.length-1]
p = _pos[i]
if t <= p
col = _colors[i]
break
if t >= p and i == _pos.length-1
col = _colors[i]
break
if t > p and t < _pos[i+1]
t = (t-p)/(_pos[i+1]-p)
col = chroma.interpolate _colors[i], _colors[i+1], t, _mode
break
else if type(_colors) == 'function'
col = _colors t
_colorCache[k] = col
col

resetCache = () ->
_colorCache = {}

setColors colors, positions

# public interface
Expand All @@ -162,6 +174,7 @@ chroma.scale = (colors, positions) ->
if not arguments.length
return _mode
_mode = _m
resetCache()
f

f.range = (colors, _pos) ->
Expand All @@ -182,6 +195,7 @@ chroma.scale = (colors, positions) ->
if not arguments.length
return _correctLightness
_correctLightness = v
resetCache()
if _correctLightness
tmap = (t) ->
L0 = getColor(0, true).lab()[0]
Expand All @@ -208,6 +222,7 @@ chroma.scale = (colors, positions) ->
else
tmap = (t) -> t
f

f

# some pre-defined color scales:
Expand Down
19 changes: 19 additions & 0 deletions test/cache-performance.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

vows = require 'vows'
assert = require 'assert'
chroma = require '../chroma'



s = chroma.scale('RdYlGn')
.mode('lab')
.domain([0,100000], 10)

t0 = new Date().getTime()

for i in [1..100000]
s(i).hex()

t1 = new Date().getTime()

console.log (t1 - t0) + "ms"

0 comments on commit 98b90f7

Please sign in to comment.