Lut.js 3.4 KB

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