2
0

Lut.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. THREE.Lut = function ( colormap, numberofcolors ) {
  2. this.lut = [];
  3. this.setColorMap( colormap, numberofcolors );
  4. return this;
  5. };
  6. THREE.Lut.prototype = {
  7. constructor: THREE.Lut,
  8. lut: [], map: [], n: 256, minV: 0, maxV: 1,
  9. set: function ( value ) {
  10. if ( value instanceof THREE.Lut ) {
  11. this.copy( value );
  12. }
  13. return this;
  14. },
  15. setMin: function ( min ) {
  16. this.minV = min;
  17. return this;
  18. },
  19. setMax: function ( max ) {
  20. this.maxV = max;
  21. return this;
  22. },
  23. setColorMap: function ( colormap, numberofcolors ) {
  24. this.map = THREE.ColorMapKeywords[ colormap ] || THREE.ColorMapKeywords.rainbow;
  25. this.n = numberofcolors || 32;
  26. var step = 1.0 / this.n;
  27. this.lut.length = 0;
  28. for ( var i = 0; i <= 1; i += step ) {
  29. for ( var j = 0; j < this.map.length - 1; j ++ ) {
  30. if ( i >= this.map[ j ][ 0 ] && i < this.map[ j + 1 ][ 0 ] ) {
  31. var min = this.map[ j ][ 0 ];
  32. var max = this.map[ j + 1 ][ 0 ];
  33. var minColor = new THREE.Color( this.map[ j ][ 1 ] );
  34. var maxColor = new THREE.Color( this.map[ j + 1 ][ 1 ] );
  35. var color = minColor.lerp( maxColor, ( i - min ) / ( max - min ) );
  36. this.lut.push( color );
  37. }
  38. }
  39. }
  40. return this;
  41. },
  42. copy: function ( 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: function ( 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. var colorPosition = Math.round( alpha * this.n );
  58. colorPosition == this.n ? colorPosition -= 1 : colorPosition;
  59. return this.lut[ colorPosition ];
  60. },
  61. addColorMap: function ( colormapName, arrayOfColors ) {
  62. THREE.ColorMapKeywords[ colormapName ] = arrayOfColors;
  63. },
  64. createCanvas: function () {
  65. var canvas = document.createElement( 'canvas' );
  66. canvas.width = 1;
  67. canvas.height = this.n;
  68. this.updateCanvas( canvas );
  69. return canvas;
  70. },
  71. updateCanvas: function ( canvas ) {
  72. var ctx = canvas.getContext( '2d', { alpha: false } );
  73. var imageData = ctx.getImageData( 0, 0, 1, this.n );
  74. var data = imageData.data;
  75. var k = 0;
  76. var step = 1.0 / this.n;
  77. for ( var i = 1; i >= 0; i -= step ) {
  78. for ( var j = this.map.length - 1; j >= 0; j -- ) {
  79. if ( i < this.map[ j ][ 0 ] && i >= this.map[ j - 1 ][ 0 ] ) {
  80. var min = this.map[ j - 1 ][ 0 ];
  81. var max = this.map[ j ][ 0 ];
  82. var minColor = new THREE.Color( this.map[ j - 1 ][ 1 ] );
  83. var maxColor = new THREE.Color( this.map[ j ][ 1 ] );
  84. var 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. THREE.ColorMapKeywords = {
  98. 'rainbow': [[ 0.0, 0x0000FF ], [ 0.2, 0x00FFFF ], [ 0.5, 0x00FF00 ], [ 0.8, 0xFFFF00 ], [ 1.0, 0xFF0000 ]],
  99. 'cooltowarm': [[ 0.0, 0x3C4EC2 ], [ 0.2, 0x9BBCFF ], [ 0.5, 0xDCDCDC ], [ 0.8, 0xF6A385 ], [ 1.0, 0xB40426 ]],
  100. 'blackbody': [[ 0.0, 0x000000 ], [ 0.2, 0x780000 ], [ 0.5, 0xE63200 ], [ 0.8, 0xFFFF00 ], [ 1.0, 0xFFFFFF ]],
  101. 'grayscale': [[ 0.0, 0x000000 ], [ 0.2, 0x404040 ], [ 0.5, 0x7F7F80 ], [ 0.8, 0xBFBFBF ], [ 1.0, 0xFFFFFF ]]
  102. };