Lut.js 3.7 KB

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