Lut.js 3.7 KB

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