Lut.js 3.6 KB

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