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