Lut.js 3.8 KB

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