Skip to content

Commit

Permalink
Add method to add new colormap pattern dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
mshubin committed Dec 16, 2014
1 parent 8147780 commit 5699bb9
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion src/ColorMap.js
Expand Up @@ -18,7 +18,7 @@
* along with GlobWeb. If not, see <http://www.gnu.org/licenses/>.
***************************************/

define([], function() {
define(["./Numeric"], function(Numeric) {

/**************************************************************************************************************/
var transferFonctions = {
Expand Down Expand Up @@ -335,6 +335,48 @@ function _textureFromPixelArray(gl, dataArray, format, dataType, width, height)

return {

/**
* Create custom colormap with equidistant intervals
*
* @param name Colormap name
* @param colors The array of colors defining the colormap(must have length at least >=2)
*/
addCustomColormap : function(name, colors)
{
if ( colors.length < 2 )
{
console.error("Colors length must be >= 2");
return null;
}

var colormapSize = 256;
var colormap = {
red: [],
green: [],
blue: []
};

var nbIntervals = colors.length-1;
for ( var i=0; i<nbIntervals; i++ )
{
var c1 = colors[i];
var c2 = colors[i+1];

var intervalLength = colormapSize / (colors.length - 1);
var start = colormap.red.length;
var end = Math.floor((i+1)*intervalLength);
for ( var j = start; j < end; j++ )
{
colormap.red.push( Numeric.lerp( j/end, c1[0], c2[0] ));
colormap.green.push( Numeric.lerp( j/end, c1[1], c2[1] ));
colormap.blue.push( Numeric.lerp( j/end, c1[2], c2[2] ));
}
}

// Add to colormaps object
colormaps[name] = colormap;
},

/**
* Generate colormap
*/
Expand Down

0 comments on commit 5699bb9

Please sign in to comment.