2
0

Lut.js 3.7 KB

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