Lut.js 3.5 KB

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