Lut.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. ( function () {
  2. class Lut {
  3. constructor( colormap, numberofcolors ) {
  4. this.lut = [];
  5. this.setColorMap( colormap, numberofcolors );
  6. }
  7. set( value ) {
  8. if ( value instanceof Lut ) {
  9. this.copy( value );
  10. }
  11. return this;
  12. }
  13. setMin( min ) {
  14. this.minV = min;
  15. return this;
  16. }
  17. setMax( max ) {
  18. this.maxV = max;
  19. return this;
  20. }
  21. setColorMap( colormap, numberofcolors = 32 ) {
  22. this.map = ColorMapKeywords[ colormap ] || ColorMapKeywords.rainbow;
  23. this.n = numberofcolors;
  24. const step = 1.0 / this.n;
  25. this.lut.length = 0;
  26. for ( let i = 0; i <= 1; i += step ) {
  27. for ( let j = 0; j < this.map.length - 1; j ++ ) {
  28. if ( i >= this.map[ j ][ 0 ] && i < this.map[ j + 1 ][ 0 ] ) {
  29. const min = this.map[ j ][ 0 ];
  30. const max = this.map[ j + 1 ][ 0 ];
  31. const minColor = new THREE.Color( this.map[ j ][ 1 ] );
  32. const maxColor = new THREE.Color( this.map[ j + 1 ][ 1 ] );
  33. const color = minColor.lerp( maxColor, ( i - min ) / ( max - min ) );
  34. this.lut.push( color );
  35. }
  36. }
  37. }
  38. return this;
  39. }
  40. copy( lut ) {
  41. this.lut = lut.lut;
  42. this.map = lut.map;
  43. this.n = lut.n;
  44. this.minV = lut.minV;
  45. this.maxV = lut.maxV;
  46. return this;
  47. }
  48. getColor( alpha ) {
  49. if ( alpha <= this.minV ) {
  50. alpha = this.minV;
  51. } else if ( alpha >= this.maxV ) {
  52. alpha = this.maxV;
  53. }
  54. alpha = ( alpha - this.minV ) / ( this.maxV - this.minV );
  55. let colorPosition = Math.round( alpha * this.n );
  56. colorPosition == this.n ? colorPosition -= 1 : colorPosition;
  57. return this.lut[ colorPosition ];
  58. }
  59. addColorMap( colormapName, arrayOfColors ) {
  60. ColorMapKeywords[ colormapName ] = arrayOfColors;
  61. }
  62. createCanvas() {
  63. const canvas = document.createElement( 'canvas' );
  64. canvas.width = 1;
  65. canvas.height = this.n;
  66. this.updateCanvas( canvas );
  67. return canvas;
  68. }
  69. updateCanvas( canvas ) {
  70. const ctx = canvas.getContext( '2d', {
  71. alpha: false
  72. } );
  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 THREE.Color( this.map[ j - 1 ][ 1 ] );
  83. const maxColor = new THREE.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. THREE.ColorMapKeywords = ColorMapKeywords;
  109. THREE.Lut = Lut;
  110. } )();