Lut.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. import {
  2. Color,
  3. LinearSRGBColorSpace,
  4. MathUtils
  5. } from 'three';
  6. /**
  7. * Represents a lookup table for colormaps. It is used to determine the color
  8. * values from a range of data values.
  9. *
  10. * ```js
  11. * const lut = new Lut( 'rainbow', 512 );
  12. * const color = lut.getColor( 0.5 );
  13. * ```
  14. *
  15. * @three_import import { Lut } from 'three/addons/math/Lut.js';
  16. */
  17. class Lut {
  18. /**
  19. * Constructs a new Lut.
  20. *
  21. * @param {('rainbow'|'cooltowarm'|'blackbody'|'grayscale')} [colormap='rainbow'] - Sets a colormap from predefined list of colormaps.
  22. * @param {number} [count=32] - Sets the number of colors used to represent the data array.
  23. */
  24. constructor( colormap, count = 32 ) {
  25. /**
  26. * This flag can be used for type testing.
  27. *
  28. * @type {boolean}
  29. * @readonly
  30. * @default true
  31. */
  32. this.isLut = true;
  33. /**
  34. * The lookup table for the selected color map
  35. *
  36. * @type {Array<Color>}
  37. */
  38. this.lut = [];
  39. /**
  40. * The currently selected color map.
  41. *
  42. * @type {Array}
  43. */
  44. this.map = [];
  45. /**
  46. * The number of colors of the current selected color map.
  47. *
  48. * @type {number}
  49. * @default 32
  50. */
  51. this.n = 0;
  52. /**
  53. * The minimum value to be represented with the lookup table.
  54. *
  55. * @type {number}
  56. * @default 0
  57. */
  58. this.minV = 0;
  59. /**
  60. * The maximum value to be represented with the lookup table.
  61. *
  62. * @type {number}
  63. * @default 1
  64. */
  65. this.maxV = 1;
  66. this.setColorMap( colormap, count );
  67. }
  68. /**
  69. * Sets the given LUT.
  70. *
  71. * @param {Lut} value - The LUT to set.
  72. * @return {Lut} A reference to this LUT.
  73. */
  74. set( value ) {
  75. if ( value.isLut === true ) {
  76. this.copy( value );
  77. }
  78. return this;
  79. }
  80. /**
  81. * Sets the minimum value to be represented with this LUT.
  82. *
  83. * @param {number} min - The minimum value to be represented with the lookup table.
  84. * @return {Lut} A reference to this LUT.
  85. */
  86. setMin( min ) {
  87. this.minV = min;
  88. return this;
  89. }
  90. /**
  91. * Sets the maximum value to be represented with this LUT.
  92. *
  93. * @param {number} max - The maximum value to be represented with the lookup table.
  94. * @return {Lut} A reference to this LUT.
  95. */
  96. setMax( max ) {
  97. this.maxV = max;
  98. return this;
  99. }
  100. /**
  101. * Configure the lookup table for the given color map and number of colors.
  102. *
  103. * @param {string} colormap - The name of the color map.
  104. * @param {number} [count=32] - The number of colors.
  105. * @return {Lut} A reference to this LUT.
  106. */
  107. setColorMap( colormap, count = 32 ) {
  108. this.map = ColorMapKeywords[ colormap ] || ColorMapKeywords.rainbow;
  109. this.n = count;
  110. const step = 1.0 / this.n;
  111. const minColor = new Color();
  112. const maxColor = new Color();
  113. this.lut.length = 0;
  114. // sample at 0
  115. this.lut.push( new Color( this.map[ 0 ][ 1 ] ) );
  116. // sample at 1/n, ..., (n-1)/n
  117. for ( let i = 1; i < count; i ++ ) {
  118. const alpha = i * step;
  119. for ( let j = 0; j < this.map.length - 1; j ++ ) {
  120. if ( alpha > this.map[ j ][ 0 ] && alpha <= this.map[ j + 1 ][ 0 ] ) {
  121. const min = this.map[ j ][ 0 ];
  122. const max = this.map[ j + 1 ][ 0 ];
  123. minColor.setHex( this.map[ j ][ 1 ], LinearSRGBColorSpace );
  124. maxColor.setHex( this.map[ j + 1 ][ 1 ], LinearSRGBColorSpace );
  125. const color = new Color().lerpColors( minColor, maxColor, ( alpha - min ) / ( max - min ) );
  126. this.lut.push( color );
  127. }
  128. }
  129. }
  130. // sample at 1
  131. this.lut.push( new Color( this.map[ this.map.length - 1 ][ 1 ] ) );
  132. return this;
  133. }
  134. /**
  135. * Copies the given lut.
  136. *
  137. * @param {Lut} lut - The LUT to copy.
  138. * @return {Lut} A reference to this LUT.
  139. */
  140. copy( lut ) {
  141. this.lut = lut.lut;
  142. this.map = lut.map;
  143. this.n = lut.n;
  144. this.minV = lut.minV;
  145. this.maxV = lut.maxV;
  146. return this;
  147. }
  148. /**
  149. * Returns an instance of Color for the given data value.
  150. *
  151. * @param {number} alpha - The value to lookup.
  152. * @return {Color} The color from the LUT.
  153. */
  154. getColor( alpha ) {
  155. alpha = MathUtils.clamp( alpha, this.minV, this.maxV );
  156. alpha = ( alpha - this.minV ) / ( this.maxV - this.minV );
  157. const colorPosition = Math.round( alpha * this.n );
  158. return this.lut[ colorPosition ];
  159. }
  160. /**
  161. * Adds a color map to this Lut instance.
  162. *
  163. * @param {string} name - The name of the color map.
  164. * @param {Array} arrayOfColors - An array of color values. Each value is an array
  165. * holding a threshold and the actual color value as a hexadecimal number.
  166. * @return {Lut} A reference to this LUT.
  167. */
  168. addColorMap( name, arrayOfColors ) {
  169. ColorMapKeywords[ name ] = arrayOfColors;
  170. return this;
  171. }
  172. /**
  173. * Creates a canvas in order to visualize the lookup table as a texture.
  174. *
  175. * @return {HTMLCanvasElement} The created canvas.
  176. */
  177. createCanvas() {
  178. const canvas = document.createElement( 'canvas' );
  179. canvas.width = 1;
  180. canvas.height = this.n;
  181. this.updateCanvas( canvas );
  182. return canvas;
  183. }
  184. /**
  185. * Updates the given canvas with the Lut's data.
  186. *
  187. * @param {HTMLCanvasElement} canvas - The canvas to update.
  188. * @return {HTMLCanvasElement} The updated canvas.
  189. */
  190. updateCanvas( canvas ) {
  191. const ctx = canvas.getContext( '2d', { alpha: false } );
  192. const imageData = ctx.getImageData( 0, 0, 1, this.n );
  193. const data = imageData.data;
  194. let k = 0;
  195. const step = 1.0 / this.n;
  196. const minColor = new Color();
  197. const maxColor = new Color();
  198. const finalColor = new Color();
  199. for ( let i = 1; i >= 0; i -= step ) {
  200. for ( let j = this.map.length - 1; j >= 0; j -- ) {
  201. if ( i < this.map[ j ][ 0 ] && i >= this.map[ j - 1 ][ 0 ] ) {
  202. const min = this.map[ j - 1 ][ 0 ];
  203. const max = this.map[ j ][ 0 ];
  204. minColor.setHex( this.map[ j - 1 ][ 1 ], LinearSRGBColorSpace );
  205. maxColor.setHex( this.map[ j ][ 1 ], LinearSRGBColorSpace );
  206. finalColor.lerpColors( minColor, maxColor, ( i - min ) / ( max - min ) );
  207. data[ k * 4 ] = Math.round( finalColor.r * 255 );
  208. data[ k * 4 + 1 ] = Math.round( finalColor.g * 255 );
  209. data[ k * 4 + 2 ] = Math.round( finalColor.b * 255 );
  210. data[ k * 4 + 3 ] = 255;
  211. k += 1;
  212. }
  213. }
  214. }
  215. ctx.putImageData( imageData, 0, 0 );
  216. return canvas;
  217. }
  218. }
  219. const ColorMapKeywords = {
  220. 'rainbow': [[ 0.0, 0x0000FF ], [ 0.2, 0x00FFFF ], [ 0.5, 0x00FF00 ], [ 0.8, 0xFFFF00 ], [ 1.0, 0xFF0000 ]],
  221. 'cooltowarm': [[ 0.0, 0x3C4EC2 ], [ 0.2, 0x9BBCFF ], [ 0.5, 0xDCDCDC ], [ 0.8, 0xF6A385 ], [ 1.0, 0xB40426 ]],
  222. 'blackbody': [[ 0.0, 0x000000 ], [ 0.2, 0x780000 ], [ 0.5, 0xE63200 ], [ 0.8, 0xFFFF00 ], [ 1.0, 0xFFFFFF ]],
  223. 'grayscale': [[ 0.0, 0x000000 ], [ 0.2, 0x404040 ], [ 0.5, 0x7F7F80 ], [ 0.8, 0xBFBFBF ], [ 1.0, 0xFFFFFF ]]
  224. };
  225. export { Lut, ColorMapKeywords };