Lut.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**
  2. * @author daron1337 / http://daron1337.github.io/
  3. */
  4. THREE.Lut = function ( colormap, numberofcolors ) {
  5. this.lut = [];
  6. this.setColorMap( colormap, numberofcolors );
  7. return this;
  8. };
  9. var defaultLabelParameters = {
  10. fontsize: 24,
  11. fontface: 'Arial',
  12. title: '',
  13. um: '',
  14. ticks: 0,
  15. decimal: 2,
  16. notation: 'standard'
  17. };
  18. var defaultBackgroundColor = { r: 255, g: 100, b: 100, a: 0.8 };
  19. var defaultBorderColor = { r: 255, g: 0, b: 0, a: 1.0 };
  20. var defaultBorderThickness = 4;
  21. THREE.Lut.prototype = {
  22. constructor: THREE.Lut,
  23. lut: [], map: [], n: 256, minV: 0, maxV: 1,
  24. set: function ( value ) {
  25. if ( value instanceof THREE.Lut ) {
  26. this.copy( value );
  27. }
  28. return this;
  29. },
  30. setMin: function ( min ) {
  31. this.minV = min;
  32. return this;
  33. },
  34. setMax: function ( max ) {
  35. this.maxV = max;
  36. return this;
  37. },
  38. setColorMap: function ( colormap, numberofcolors ) {
  39. this.map = THREE.ColorMapKeywords[ colormap ] || THREE.ColorMapKeywords.rainbow;
  40. this.n = numberofcolors || 32;
  41. var step = 1.0 / this.n;
  42. this.lut.length = 0;
  43. for ( var i = 0; i <= 1; i += step ) {
  44. for ( var j = 0; j < this.map.length - 1; j ++ ) {
  45. if ( i >= this.map[ j ][ 0 ] && i < this.map[ j + 1 ][ 0 ] ) {
  46. var min = this.map[ j ][ 0 ];
  47. var max = this.map[ j + 1 ][ 0 ];
  48. var minColor = new THREE.Color( this.map[ j ][ 1 ] );
  49. var maxColor = new THREE.Color( this.map[ j + 1 ][ 1 ] );
  50. var color = minColor.lerp( maxColor, ( i - min ) / ( max - min ) );
  51. this.lut.push( color );
  52. }
  53. }
  54. }
  55. return this;
  56. },
  57. copy: function ( lut ) {
  58. this.lut = lut.lut;
  59. this.map = lut.map;
  60. this.n = lut.n;
  61. this.minV = lut.minV;
  62. this.maxV = lut.maxV;
  63. return this;
  64. },
  65. getColor: function ( alpha ) {
  66. if ( alpha <= this.minV ) {
  67. alpha = this.minV;
  68. } else if ( alpha >= this.maxV ) {
  69. alpha = this.maxV;
  70. }
  71. alpha = ( alpha - this.minV ) / ( this.maxV - this.minV );
  72. var colorPosition = Math.round( alpha * this.n );
  73. colorPosition == this.n ? colorPosition -= 1 : colorPosition;
  74. return this.lut[ colorPosition ];
  75. },
  76. addColorMap: function ( colormapName, arrayOfColors ) {
  77. THREE.ColorMapKeywords[ colormapName ] = arrayOfColors;
  78. },
  79. createCanvas: function () {
  80. var canvas = document.createElement( 'canvas' );
  81. canvas.width = 1;
  82. canvas.height = this.n;
  83. this.updateCanvas( canvas );
  84. return canvas;
  85. },
  86. updateCanvas: function ( canvas ) {
  87. var ctx = canvas.getContext( '2d', { alpha: false } );
  88. var imageData = ctx.getImageData( 0, 0, 1, this.n );
  89. var data = imageData.data;
  90. var k = 0;
  91. var step = 1.0 / this.n;
  92. for ( var i = 1; i >= 0; i -= step ) {
  93. for ( var j = this.map.length - 1; j >= 0; j -- ) {
  94. if ( i < this.map[ j ][ 0 ] && i >= this.map[ j - 1 ][ 0 ] ) {
  95. var min = this.map[ j - 1 ][ 0 ];
  96. var max = this.map[ j ][ 0 ];
  97. var minColor = new THREE.Color( this.map[ j - 1 ][ 1 ] );
  98. var maxColor = new THREE.Color( this.map[ j ][ 1 ] );
  99. var color = minColor.lerp( maxColor, ( i - min ) / ( max - min ) );
  100. data[ k * 4 ] = Math.round( color.r * 255 );
  101. data[ k * 4 + 1 ] = Math.round( color.g * 255 );
  102. data[ k * 4 + 2 ] = Math.round( color.b * 255 );
  103. data[ k * 4 + 3 ] = 255;
  104. k += 1;
  105. }
  106. }
  107. }
  108. ctx.putImageData( imageData, 0, 0 );
  109. return canvas;
  110. }
  111. };
  112. THREE.ColorMapKeywords = {
  113. "rainbow": [[ 0.0, 0x0000FF ], [ 0.2, 0x00FFFF ], [ 0.5, 0x00FF00 ], [ 0.8, 0xFFFF00 ], [ 1.0, 0xFF0000 ]],
  114. "cooltowarm": [[ 0.0, 0x3C4EC2 ], [ 0.2, 0x9BBCFF ], [ 0.5, 0xDCDCDC ], [ 0.8, 0xF6A385 ], [ 1.0, 0xB40426 ]],
  115. "blackbody": [[ 0.0, 0x000000 ], [ 0.2, 0x780000 ], [ 0.5, 0xE63200 ], [ 0.8, 0xFFFF00 ], [ 1.0, 0xFFFFFF ]],
  116. "grayscale": [[ 0.0, 0x000000 ], [ 0.2, 0x404040 ], [ 0.5, 0x7F7F80 ], [ 0.8, 0xBFBFBF ], [ 1.0, 0xFFFFFF ]]
  117. };