Lut.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. console.warn( "THREE.Lut: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/#manual/en/introduction/Installation." );
  2. THREE.Lut = function ( colormap, numberofcolors ) {
  3. this.lut = [];
  4. this.setColorMap( colormap, numberofcolors );
  5. return this;
  6. };
  7. THREE.Lut.prototype = {
  8. constructor: THREE.Lut,
  9. lut: [], map: [], n: 256, minV: 0, maxV: 1,
  10. set: function ( value ) {
  11. if ( value instanceof THREE.Lut ) {
  12. this.copy( value );
  13. }
  14. return this;
  15. },
  16. setMin: function ( min ) {
  17. this.minV = min;
  18. return this;
  19. },
  20. setMax: function ( max ) {
  21. this.maxV = max;
  22. return this;
  23. },
  24. setColorMap: function ( colormap, numberofcolors ) {
  25. this.map = THREE.ColorMapKeywords[ colormap ] || THREE.ColorMapKeywords.rainbow;
  26. this.n = numberofcolors || 32;
  27. var step = 1.0 / this.n;
  28. this.lut.length = 0;
  29. for ( var i = 0; i <= 1; i += step ) {
  30. for ( var j = 0; j < this.map.length - 1; j ++ ) {
  31. if ( i >= this.map[ j ][ 0 ] && i < this.map[ j + 1 ][ 0 ] ) {
  32. var min = this.map[ j ][ 0 ];
  33. var max = this.map[ j + 1 ][ 0 ];
  34. var minColor = new THREE.Color( this.map[ j ][ 1 ] );
  35. var maxColor = new THREE.Color( this.map[ j + 1 ][ 1 ] );
  36. var color = minColor.lerp( maxColor, ( i - min ) / ( max - min ) );
  37. this.lut.push( color );
  38. }
  39. }
  40. }
  41. return this;
  42. },
  43. copy: function ( lut ) {
  44. this.lut = lut.lut;
  45. this.map = lut.map;
  46. this.n = lut.n;
  47. this.minV = lut.minV;
  48. this.maxV = lut.maxV;
  49. return this;
  50. },
  51. getColor: function ( alpha ) {
  52. if ( alpha <= this.minV ) {
  53. alpha = this.minV;
  54. } else if ( alpha >= this.maxV ) {
  55. alpha = this.maxV;
  56. }
  57. alpha = ( alpha - this.minV ) / ( this.maxV - this.minV );
  58. var colorPosition = Math.round( alpha * this.n );
  59. colorPosition == this.n ? colorPosition -= 1 : colorPosition;
  60. return this.lut[ colorPosition ];
  61. },
  62. addColorMap: function ( colormapName, arrayOfColors ) {
  63. THREE.ColorMapKeywords[ colormapName ] = arrayOfColors;
  64. },
  65. createCanvas: function () {
  66. var canvas = document.createElement( 'canvas' );
  67. canvas.width = 1;
  68. canvas.height = this.n;
  69. this.updateCanvas( canvas );
  70. return canvas;
  71. },
  72. updateCanvas: function ( canvas ) {
  73. var ctx = canvas.getContext( '2d', { alpha: false } );
  74. var imageData = ctx.getImageData( 0, 0, 1, this.n );
  75. var data = imageData.data;
  76. var k = 0;
  77. var step = 1.0 / this.n;
  78. for ( var i = 1; i >= 0; i -= step ) {
  79. for ( var j = this.map.length - 1; j >= 0; j -- ) {
  80. if ( i < this.map[ j ][ 0 ] && i >= this.map[ j - 1 ][ 0 ] ) {
  81. var min = this.map[ j - 1 ][ 0 ];
  82. var max = this.map[ j ][ 0 ];
  83. var minColor = new THREE.Color( this.map[ j - 1 ][ 1 ] );
  84. var maxColor = new THREE.Color( this.map[ j ][ 1 ] );
  85. var color = minColor.lerp( maxColor, ( i - min ) / ( max - min ) );
  86. data[ k * 4 ] = Math.round( color.r * 255 );
  87. data[ k * 4 + 1 ] = Math.round( color.g * 255 );
  88. data[ k * 4 + 2 ] = Math.round( color.b * 255 );
  89. data[ k * 4 + 3 ] = 255;
  90. k += 1;
  91. }
  92. }
  93. }
  94. ctx.putImageData( imageData, 0, 0 );
  95. return canvas;
  96. }
  97. };
  98. THREE.ColorMapKeywords = {
  99. "rainbow": [[ 0.0, 0x0000FF ], [ 0.2, 0x00FFFF ], [ 0.5, 0x00FF00 ], [ 0.8, 0xFFFF00 ], [ 1.0, 0xFF0000 ]],
  100. "cooltowarm": [[ 0.0, 0x3C4EC2 ], [ 0.2, 0x9BBCFF ], [ 0.5, 0xDCDCDC ], [ 0.8, 0xF6A385 ], [ 1.0, 0xB40426 ]],
  101. "blackbody": [[ 0.0, 0x000000 ], [ 0.2, 0x780000 ], [ 0.5, 0xE63200 ], [ 0.8, 0xFFFF00 ], [ 1.0, 0xFFFFFF ]],
  102. "grayscale": [[ 0.0, 0x000000 ], [ 0.2, 0x404040 ], [ 0.5, 0x7F7F80 ], [ 0.8, 0xBFBFBF ], [ 1.0, 0xFFFFFF ]]
  103. };