Lut.js 3.5 KB

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