Lut.js 3.7 KB

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