Lut.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. this.lut.length = 0;
  30. for ( let i = 0; i <= 1; i += step ) {
  31. for ( let j = 0; j < this.map.length - 1; j ++ ) {
  32. if ( i >= this.map[ j ][ 0 ] && i < this.map[ j + 1 ][ 0 ] ) {
  33. const min = this.map[ j ][ 0 ];
  34. const max = this.map[ j + 1 ][ 0 ];
  35. const minColor = new THREE.Color( this.map[ j ][ 1 ] );
  36. const maxColor = new THREE.Color( this.map[ j + 1 ][ 1 ] );
  37. const color = minColor.lerp( maxColor, ( i - min ) / ( max - min ) );
  38. this.lut.push( color );
  39. }
  40. }
  41. }
  42. return this;
  43. }
  44. copy( lut ) {
  45. this.lut = lut.lut;
  46. this.map = lut.map;
  47. this.n = lut.n;
  48. this.minV = lut.minV;
  49. this.maxV = lut.maxV;
  50. return this;
  51. }
  52. getColor( alpha ) {
  53. if ( alpha <= this.minV ) {
  54. alpha = this.minV;
  55. } else if ( alpha >= this.maxV ) {
  56. alpha = this.maxV;
  57. }
  58. alpha = ( alpha - this.minV ) / ( this.maxV - this.minV );
  59. let colorPosition = Math.round( alpha * this.n );
  60. colorPosition == this.n ? colorPosition -= 1 : colorPosition;
  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. for ( let i = 1; i >= 0; i -= step ) {
  83. for ( let j = this.map.length - 1; j >= 0; j -- ) {
  84. if ( i < this.map[ j ][ 0 ] && i >= this.map[ j - 1 ][ 0 ] ) {
  85. const min = this.map[ j - 1 ][ 0 ];
  86. const max = this.map[ j ][ 0 ];
  87. const minColor = new THREE.Color( this.map[ j - 1 ][ 1 ] );
  88. const maxColor = new THREE.Color( this.map[ j ][ 1 ] );
  89. const color = minColor.lerp( maxColor, ( i - min ) / ( max - min ) );
  90. data[ k * 4 ] = Math.round( color.r * 255 );
  91. data[ k * 4 + 1 ] = Math.round( color.g * 255 );
  92. data[ k * 4 + 2 ] = Math.round( color.b * 255 );
  93. data[ k * 4 + 3 ] = 255;
  94. k += 1;
  95. }
  96. }
  97. }
  98. ctx.putImageData( imageData, 0, 0 );
  99. return canvas;
  100. }
  101. }
  102. Lut.prototype.isLut = true;
  103. const ColorMapKeywords = {
  104. 'rainbow': [[ 0.0, 0x0000FF ], [ 0.2, 0x00FFFF ], [ 0.5, 0x00FF00 ], [ 0.8, 0xFFFF00 ], [ 1.0, 0xFF0000 ]],
  105. 'cooltowarm': [[ 0.0, 0x3C4EC2 ], [ 0.2, 0x9BBCFF ], [ 0.5, 0xDCDCDC ], [ 0.8, 0xF6A385 ], [ 1.0, 0xB40426 ]],
  106. 'blackbody': [[ 0.0, 0x000000 ], [ 0.2, 0x780000 ], [ 0.5, 0xE63200 ], [ 0.8, 0xFFFF00 ], [ 1.0, 0xFFFFFF ]],
  107. 'grayscale': [[ 0.0, 0x000000 ], [ 0.2, 0x404040 ], [ 0.5, 0x7F7F80 ], [ 0.8, 0xBFBFBF ], [ 1.0, 0xFFFFFF ]]
  108. };
  109. THREE.ColorMapKeywords = ColorMapKeywords;
  110. THREE.Lut = Lut;
  111. } )();