Lut.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * @author daron1337 / http://daron1337.github.io/
  3. */
  4. THREE.Lut = function ( colormap, numberofcolors ) {
  5. this.lut = new Array();
  6. this.map = THREE.ColorMapKeywords[ colormap ];
  7. this.n = numberofcolors;
  8. var step = 1. / this.n;
  9. for ( var i = 0; i <= 1; i+=step ) {
  10. for ( var j = 0; j < this.map.length - 1; j++ ) {
  11. if ( i >= this.map[ j ][ 0 ] && i < this.map[ j+1 ][ 0 ] ) {
  12. var min = this.map[ j ][ 0 ];
  13. var max = this.map[ j+1 ][ 0 ];
  14. var color = new THREE.Color( 0xffffff );
  15. var minColor = new THREE.Color( 0xffffff ).setHex( this.map[ j ][ 1 ] );
  16. var maxColor = new THREE.Color( 0xffffff ).setHex( this.map[ j+1 ][ 1 ] );
  17. color = minColor.lerp( maxColor, ( i - min ) / ( max - min ) );
  18. this.lut.push(color);
  19. }
  20. }
  21. }
  22. return this.set( this );
  23. };
  24. THREE.Lut.prototype = {
  25. constructor: THREE.Lut,
  26. lut: [], map: [], mapname: 'rainbow' , n: 256, minV: 0, maxV: 1,
  27. set: function ( value ) {
  28. if ( value instanceof THREE.Lut ) {
  29. this.copy( value );
  30. }
  31. return this;
  32. },
  33. setMin: function ( min ) {
  34. this.minV = min;
  35. return this;
  36. },
  37. setMax: function ( max ) {
  38. this.maxV = max;
  39. return this;
  40. },
  41. changeNumberOfColors: function ( numberofcolors ) {
  42. this.n = numberofcolors;
  43. return new THREE.Lut( this.mapname, this.n );
  44. },
  45. changeColorMap: function ( colormap ) {
  46. this.mapname = colormap;
  47. return new THREE.Lut( this.mapname, this.n );
  48. },
  49. copy: function ( lut ) {
  50. this.lut = lut.lut;
  51. this.mapname = lut.mapname;
  52. this.map = lut.map;
  53. this.n = lut.n;
  54. this.minV = lut.minV;
  55. this.maxV = lut.maxV;
  56. return this;
  57. },
  58. getColor: function ( alpha ) {
  59. if ( alpha <= this.minV ) {
  60. alpha = this.minV;
  61. }
  62. else if ( alpha >= this.maxV ) {
  63. alpha = this.maxV;
  64. }
  65. alpha = ( alpha - this.minV ) / ( this.maxV - this.minV );
  66. var colorPosition = Math.round ( alpha * this.n );
  67. colorPosition == this.n ? colorPosition -= 1 : colorPosition;
  68. return this.lut[ colorPosition ];
  69. },
  70. addColorMap: function ( colormapName, arrayOfColors ) {
  71. THREE.ColorMapKeywords[ colormapName ] = arrayOfColors;
  72. },
  73. };
  74. THREE.ColorMapKeywords = {
  75. "rainbow": [ [ 0.0, '0x0000FF' ], [ 0.2, '0x00FFFF' ], [ 0.5, '0x00FF00' ], [ 0.8, '0xFFFF00'], [1.0, '0xFF0000' ] ],
  76. "cooltowarm": [ [ 0.0, '0x3C4EC2' ], [ 0.2, '0x9BBCFF' ], [ 0.5, '0xDCDCDC' ], [ 0.8, '0xF6A385'], [1.0, '0xB40426' ] ],
  77. "blackbody" : [ [ 0.0, '0x000000' ], [ 0.2, '0x780000' ], [ 0.5, '0xE63200' ], [ 0.8, '0xFFFF00'], [1.0, '0xFFFFFF' ] ]
  78. }