| 1 |
Rickshaw.namespace("Rickshaw.Color.Palette"); |
| 2 |
|
| 3 |
Rickshaw.Color.Palette = function(args) { |
| 4 |
|
| 5 |
var color = new Rickshaw.Fixtures.Color(); |
| 6 |
|
| 7 |
args = args || {}; |
| 8 |
this.schemes = {}; |
| 9 |
|
| 10 |
this.scheme = color.schemes[args.scheme] || args.scheme || color.schemes.colorwheel; |
| 11 |
this.runningIndex = 0; |
| 12 |
this.generatorIndex = 0; |
| 13 |
|
| 14 |
if (args.interpolatedStopCount) { |
| 15 |
var schemeCount = this.scheme.length - 1; |
| 16 |
var i, j, scheme = []; |
| 17 |
for (i = 0; i < schemeCount; i++) { |
| 18 |
scheme.push(this.scheme[i]); |
| 19 |
var generator = d3.interpolateHsl(this.scheme[i], this.scheme[i + 1]); |
| 20 |
for (j = 1; j < args.interpolatedStopCount; j++) { |
| 21 |
scheme.push(generator((1 / args.interpolatedStopCount) * j)); |
| 22 |
} |
| 23 |
} |
| 24 |
scheme.push(this.scheme[this.scheme.length - 1]); |
| 25 |
this.scheme = scheme; |
| 26 |
} |
| 27 |
this.rotateCount = this.scheme.length; |
| 28 |
|
| 29 |
this.color = function(key) { |
| 30 |
return this.scheme[key] || this.scheme[this.runningIndex++] || this.interpolateColor() || '#808080'; |
| 31 |
}; |
| 32 |
|
| 33 |
this.interpolateColor = function() { |
| 34 |
if (!Array.isArray(this.scheme)) return; |
| 35 |
var color; |
| 36 |
if (this.generatorIndex == this.rotateCount * 2 - 1) { |
| 37 |
color = d3.interpolateHsl(this.scheme[this.generatorIndex], this.scheme[0])(0.5); |
| 38 |
this.generatorIndex = 0; |
| 39 |
this.rotateCount *= 2; |
| 40 |
} else { |
| 41 |
color = d3.interpolateHsl(this.scheme[this.generatorIndex], this.scheme[this.generatorIndex + 1])(0.5); |
| 42 |
this.generatorIndex++; |
| 43 |
} |
| 44 |
this.scheme.push(color); |
| 45 |
return color; |
| 46 |
}; |
| 47 |
|
| 48 |
}; |