1 line
7.3 KiB
Plaintext
1 line
7.3 KiB
Plaintext
|
|
{"version":3,"file":"Lut.cjs","sources":["../../src/math/Lut.js"],"sourcesContent":["import { Color, MathUtils } from 'three'\n\nclass Lut {\n constructor(colormap, count = 32) {\n this.isLut = true\n\n this.lut = []\n this.map = []\n this.n = 0\n this.minV = 0\n this.maxV = 1\n\n this.setColorMap(colormap, count)\n }\n\n set(value) {\n if (value.isLut === true) {\n this.copy(value)\n }\n\n return this\n }\n\n setMin(min) {\n this.minV = min\n\n return this\n }\n\n setMax(max) {\n this.maxV = max\n\n return this\n }\n\n setColorMap(colormap, count = 32) {\n this.map = ColorMapKeywords[colormap] || ColorMapKeywords.rainbow\n this.n = count\n\n const step = 1.0 / this.n\n const minColor = new Color()\n const maxColor = new Color()\n\n this.lut.length = 0\n\n // sample at 0\n\n this.lut.push(new Color(this.map[0][1]))\n\n // sample at 1/n, ..., (n-1)/n\n\n for (let i = 1; i < count; i++) {\n const alpha = i * step\n\n for (let j = 0; j < this.map.length - 1; j++) {\n if (alpha > this.map[j][0] && alpha <= this.map[j + 1][0]) {\n const min = this.map[j][0]\n const max = this.map[j + 1][0]\n\n minColor.setHex(this.map[j][1], 'srgb-linear')\n maxColor.setHex(this.map[j + 1][1], 'srgb-linear')\n\n const color = new Color().lerpColors(minColor, maxColor, (alpha - min) / (max - min))\n\n this.lut.push(color)\n }\n }\n }\n\n // sample at 1\n\n this.lut.push(new Color(this.map[this.map.length - 1][1]))\n\n return this\n }\n\n copy(lut) {\n this.lut = lut.lut\n this.map = lut.map\n this.n = lut.n\n this.minV = lut.minV\n this.maxV = lut.maxV\n\n return this\n }\n\n getColor(alpha) {\n alpha = MathUtils.clamp(alpha, this.minV, this.maxV)\n\n alpha = (alpha - this.minV) / (this.maxV - this.minV)\n\n const colorPosition = Math.round(alpha * this.n)\n\n return this.lut[colorPosition]\n }\n\n addColorMap(name, arrayOfColors) {\n ColorMapKeywords[name] = arrayOfColors\n\n return this\n }\n\n createCanvas() {\n const canvas = document.createElement('canvas')\n canvas.width = 1\n canvas.height = this.n\n\n this.updateCanvas(canvas)\n\n return canvas\n }\n\n updateCanvas(canvas) {\n const ctx = canvas.getContext('2d', { alpha: false })\n\n const imageData = ctx.getImageData(0, 0, 1, this.n)\n\n const data = imageData.data\n\n let k = 0\n\n const step = 1.0 / this.n\n\n const minColor = new Color()\n const maxColor = new Color()\n const finalColor = new Color()\n\n for (let i = 1; i >= 0; i -= step) {\n for (let j = this.map.length - 1; j >= 0; j--) {\n if (i < this.map[j][0] && i >= this.map[j - 1][0]) {\n const min = this.map[j - 1][0]\n const max = this.map[j][0]\n\n minColor.setHex(this.map[j - 1][1], 'srgb-linear')\n maxColor.setHex(this.map[j][1], 'srgb-linear')\n\n finalColor.lerpColors(minColor, maxColor, (i - min) / (max - min))\n\n data[k * 4] = Math.round(finalColor.r * 255)\n data[k * 4 + 1] = Math.round(finalColor.g * 255)\n data[k * 4 + 2] = Math.round(finalColor.b * 255)\n data[k * 4 + 3] = 255\n\n k += 1\n }\n }\n }\n\n ctx.putImageData(imageData, 0, 0)\n\n return canvas\n }\n}\n\nconst ColorMapKeywords = {\n rainbow: [\n [0.0, 0x0000ff],\n [0.2, 0x00ffff],\n [0.5, 0x00ff00],\n [0.8, 0xffff00],\n [1.0, 0xff0000],\n ],\n cooltowarm: [\n [0.0, 0x3c4ec2],\n [0.2, 0x9bbcff],\n [0.5, 0xdcdcdc],\n [0.8, 0xf6a385],\n [1.0, 0xb40426],\n ],\n blackbody: [\n [0.0, 0x000000],\n [0.2, 0x780000],\n [0.5, 0xe63200],\n [0.8, 0xffff00],\n [1.0, 0xffffff],\n ],\n grayscale: [\n [0.0, 0x000000],\n [0.2, 0x404040],\n [0.5, 0x7f7f80],\n [0.8, 0xbfbfbf],\n [1.0, 0xffffff],\n ],\n}\n\nexport { Lut, ColorMapKeywords }\n"],"names":["Color","MathUtils"],"mappings":";;;AAEA,MAAM,IAAI;AAAA,EACR,YAAY,
|