Lut.js 3.4 KB

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