Unlock custom brushes, natural fill effects and intuitive hatching in p5.js
Brush.min
Alejandroad8286d030e0
diff --git a/examples.js b/examples.jsindex 02e80fd..6d3bbfc 100644--- a/examples.js+++ b/examples.js@@ -41,7 +41,7 @@ brush.config({// YOU CAN CREATE YOU OWN BRUSHES-brush.addBrush("watercolor", {+brush.add("watercolor", {type: "image", // this is the TIP TYPE: choose standard / spray / marker / custom / imageweight: 15, // Base weight of the brush tipvibration: 2, // Vibration of the lines, spread@@ -81,12 +81,7 @@ function setup () {C.createCanvas()background("#e2e7dc")-- angleMode(DEGREES) // Because I'm an architect-- // USE BRUSHBOX LIBRARY- // Load BrushBox: load(BUFFER) -> Leave empty if you want to draw to the main p5 canvas- //brush.load();+ angleMode(DEGREES)translate(-width/2,-height/2)@@ -137,7 +132,7 @@ function setup () {//polygon.hatch(2,45,0.05)// You can also hatch arrays of polygons brush.hatch(ARRAY WITH POLS, distance_between_lines, angle_of_lines, 0-1.0 for % precission)brush.stroke("#080f15")- */+let pol_array = []for (let i = 0; i < 5; i++) {pol_array.push(new brush.Polygon([@@ -215,7 +210,32 @@ function setup () {brush.circle(100,100,50)brush.fill("#002185",120)brush.circle(150,100,50)++ brush.set("2B","#9c2128",1)++ */+ let num_cols = 20+ let num_rows = 10+ let col_size = (width - 40) / num_cols+ let row_size = (height - 40) / num_rows+ brush.noStroke()+ brush.field("seabed")+ for (let i = 0; i < num_rows; i++) {+ for (let j = 0; j < num_cols; j++) {+ brush.fill(random(palette),random(75,130))+ brush.bleed(random(0.05,0.4))++ brush.beginShape(0)+ brush.vertex(20 + col_size * j, 20 + row_size * i)+ brush.vertex(20 + col_size * j + col_size, 20 + row_size * i)+ brush.vertex(20 + col_size * j + col_size, 20 + row_size * i + row_size)+ brush.vertex(20 + col_size * j, 20 + row_size * i + row_size)+ brush.endShape()++ //brush.rect(20 + col_size * j, 20 + row_size * i, col_size, row_size)+ }+ }}diff --git a/index.html b/index.htmlindex bcbdca6..3f39750 100644--- a/index.html+++ b/index.html@@ -3,7 +3,7 @@<head><title>Brushbox - Examples</title><script src="./p5.min.js"></script>- <script src="./spectral.js"></script>+ <script src="./spectral.min.js"></script><script src="./p5.brush.js"></script><link rel="stylesheet" href="./style.css"></head>diff --git a/p5.brush.js b/p5.brush.jsindex 48020a2..f65854c 100644--- a/p5.brush.js+++ b/p5.brush.js@@ -416,33 +416,9 @@vert: `precision highp float;attribute vec3 aPosition;attribute vec2 aTexCoord;uniform mat4 uModelViewMatrix,uProjectionMatrix;varying vec2 vVertTexCoord;void main(){gl_Position=uProjectionMatrix*uModelViewMatrix*vec4(aPosition,1);vVertTexCoord=aTexCoord;}`,// Fragment shader source code with blending operations- frag: `- precision highp float;varying vec2 vVertTexCoord;- uniform sampler2D source;- uniform sampler2D mask;- uniform vec4 addColor;+ frag: `precision highp float;varying vec2 vVertTexCoord;uniform sampler2D source,mask;uniform vec4 addColor;#include "spectral.glsl"- float rand(vec2 co, float a, float b, float c){return fract(sin(dot(co, vec2(a, b))) * c);}- void main() {- vec4 maskColor = texture2D(mask, vVertTexCoord);- if (maskColor.r > 0.0) {- vec2 randParams1 = vec2(12.9898, 78.233);- vec2 randParams2 = vec2(7.9898, 58.233);- vec2 randParams3 = vec2(17.9898, 3.233);- float r1 = rand(vVertTexCoord, randParams1.x, randParams1.y, 43358.5453) * 2.0 - 1.0;- float r2 = rand(vVertTexCoord, randParams2.x, randParams2.y, 43213.5453) * 2.0 - 1.0;- float r3 = rand(vVertTexCoord, randParams3.x, randParams3.y, 33358.5453) * 2.0 - 1.0;- vec4 canvasColor = texture2D(source, vVertTexCoord);- vec3 mixedColor = spectral_mix(canvasColor.rgb, addColor.rgb, maskColor.a * 0.8);- if (maskColor.a > 0.8) {- mixedColor = spectral_mix(mixedColor, vec3(0.0), (maskColor.a - 0.8) / 0.6);- }- gl_FragColor = vec4(mixedColor + 0.02 * vec3(r1, r2, r3), 1.0);- } else {- gl_FragColor = vec4(0.0);- }- }- `+ float v(vec2 v,float d,float f,float s){return fract(sin(dot(v,vec2(d,f)))*s);}void main(){vec4 f=texture2D(mask,vVertTexCoord);if(f.x>0.){vec2 r=vec2(12.9898,78.233),a=vec2(7.9898,58.233),l=vec2(17.9898,3.233);float d=v(vVertTexCoord,r.x,r.y,43358.5453)*2.-1.,x=v(vVertTexCoord,a.x,a.y,43213.5453)*2.-1.,s=v(vVertTexCoord,l.x,l.y,33358.5453)*2.-1.;vec3 y=spectral_mix(texture2D(source,vVertTexCoord).xyz,addColor.xyz,f.w*.8);if(f.w>.8)y=spectral_mix(y,vec3(0),(f.w-.8)/.6);gl_FragColor=vec4(y+.02*vec3(d,x,s),1);}else gl_FragColor=vec4(0);}`}// =============================================================================@@ -916,6 +892,7 @@* @returns {number} The calculated spacing value.*/spacing() {+ this.p = this.list.get(this.name).paramreturn this.p.spacing * this.w;},@@ -956,40 +933,59 @@this.popState();},+ /**+ * Executes the drawing operation for a single tip.+ * @param {number} pressure - The desired pressure value.+ */+ drawTip(pressure) {+ this.pushState(true);+ this.tip(pressure)+ this.popState(true);+ },+/*** Sets up the environment for a brush stroke.*/- pushState() {+ pushState(isTip = false) {this.p = this.list.get(this.name).param// Pressure values for the stroke+ if (!isTip) {this.a = this.p.pressure.type !== "custom" ? R.random(-1, 1) : 0;this.b = this.p.pressure.type !== "custom" ? R.random(1, 1.5) : 0;this.cp = this.p.pressure.type !== "custom" ? R.random(3, 3.5) : R.random(-0.2, 0.2);const [min, max] = this.p.pressure.min_max;this.min = min;this.max = max;+ }// Blend Modethis.c = _r.color(this.c);if (this.p.blend) _trans(), Mix.mask.begin();_r.push();_r.noStroke();- if (this.p.blend) _r.translate(_matrix[0],_matrix[1]), this.markerTip();+ if (this.p.blend) {+ _r.translate(_matrix[0],_matrix[1]);+ if (!isTip) this.markerTip()+ };},/*** Restores the drawing state after a brush stroke is completed.*/- popState() {- if (this.p.blend) this.markerTip();+ popState(isTip = false) {+ if (this.p.blend && !isTip) this.markerTip();_r.pop();- if (this.p.blend) Mix.mask.end(), Mix.blend(this.c);+ if (this.p.blend) {+ Mix.mask.end();+ Mix.blend(this.c);+ }},/*** Draws the tip of the brush based on the current pressure and position.+ * @param {number} pressure - The desired pressure value.*/- tip() {- let pressure = this.calculatePressure(); // Calculate Pressure+ tip(custom_pressure = false) {+ let pressure = custom_pressure ? custom_pressure : this.calculatePressure(); // Calculate Pressurelet alpha = this.calculateAlpha(pressure); // Calcula Alphathis.applyColor(alpha); // Apply Colorif (this.isInsideClippingArea()) { // Check if it's inside clipping area@@ -1167,6 +1163,19 @@},}+ /**+ * Draws the tip of the selected brush+ * @param {number} x - x coordinate for the tip.+ * @param {number} y - y coordinate for the tip.+ * @param {number} pressure - The desired pressure value.+ * EXPORTED+ */+ function drawTip(x, y, pressure) {+ _ensureReady();+ B.position = new Position(x, y)+ B.drawTip(pressure)+ }+// =============================================================================// Section: Hatching// =============================================================================@@ -1700,7 +1709,7 @@}let sp = new Spline(_strokeArray, _strokeOption); // Create a new Spline with the recorded vertices and curvature optionif (F.isActive) {- sp.p.genPol(sp.origin[0], sp.origin[1]).fill(); // Fill the shape if the fill state is active+ sp.p.genPol(_strokeArray[0][0], _strokeArray[0][1]).fill(); // Fill the shape if the fill state is active}if (B.isActive) {sp.draw(); // Draw the shape if the brush state is active@@ -1818,7 +1827,9 @@/*** Draws the spline curve on the canvas.*/- draw () {this.p.draw()}+ draw () {+ if (B.isActive) this.p.draw();+ }}/*** Creates and draws a spline curve with the given points and curvature.@@ -2185,6 +2196,7 @@ exports.box = listOfBrushes; // Retrieves an array with existiexports.set = B.set; // Sets values for all properties of a brush.exports.pick = B.setBrush; // Selects a brush to use.exports.clip = B.clip; // Clips brushes with a rectangle.+exports.tip = drawTip; // Clips brushes with a rectangle.// STROKE Propertiesexports.stroke = B.setColor; // Sets the stroke color.diff --git a/p5.brush.min.js b/p5.brush.min.jsnew file mode 100644index 0000000..5975c83--- /dev/null+++ b/p5.brush.min.js@@ -0,0 +1,3 @@+glsl() {+ return `float spectral_uncompand(float s){return s<.04045?s/12.92:pow((s+.055)/1.055,2.4);}float spectral_compand(float s){return s<.0031308?s*12.92:1.055*pow(s,1./2.4)-.055;}vec3 spectral_srgb_to_linear(vec3 s){return vec3(spectral_uncompand(s[0]),spectral_uncompand(s[1]),spectral_uncompand(s[2]));}vec3 spectral_linear_to_srgb(vec3 s){return clamp(vec3(spectral_compand(s[0]),spectral_compand(s[1]),spectral_compand(s[2])),0.,1.);}void spectral_upsampling(vec3 e,out float m,out float s,out float f,out float r,out float x,out float v,out float z){m=min(e.x,min(e.y,e.z));e-=m;s=min(e.y,e.z);f=min(e.x,e.z);r=min(e.x,e.y);x=min(max(0.,e.x-e.z),max(0.,e.x-e.y));v=min(max(0.,e.y-e.z),max(0.,e.y-e.x));z=min(max(0.,e.z-e.y),max(0.,e.z-e.x));}void spectral_linear_to_reflectance(vec3 s,inout float f[38]){float e,o,m,x,v,w,z;spectral_upsampling(s,e,o,m,x,v,w,z);f[0]=max(1e-4,e+o*.96853629+m*.51567122+x*.02055257+v*.03147571+w*.49108579+z*.97901834);f[1]=max(1e-4,e+o*.96855103+m*.5401552+x*.02059936+v*.03146636+w*.46944057+z*.97901649);f[2]=max(1e-4,e+o*.96859338+m*.62645502+x*.02062723+v*.03140624+w*.4016578+z*.97901118);f[3]=max(1e-4,e+o*.96877345+m*.75595012+x*.02073387+v*.03119611+w*.2449042+z*.97892146);f[4]=max(1e-4,e+o*.96942204+m*.92826996+x*.02114202+v*.03053888+w*.0682688+z*.97858555);f[5]=max(1e-4,e+o*.97143709+m*.97223624+x*.02233154+v*.02856855+w*.02732883+z*.97743705);f[6]=max(1e-4,e+o*.97541862+m*.98616174+x*.02556857+v*.02459485+w*.013606+z*.97428075);f[7]=max(1e-4,e+o*.98074186+m*.98955255+x*.03330189+v*.0192952+w*.01000187+z*.96663223);f[8]=max(1e-4,e+o*.98580992+m*.98676237+x*.05185294+v*.01423112+w*.01284127+z*.94822893);f[9]=max(1e-4,e+o*.98971194+m*.97312575+x*.10087639+v*.01033111+w*.02636635+z*.89937713);f[10]=max(1e-4,e+o*.99238027+m*.91944277+x*.24000413+v*.00765876+w*.07058713+z*.76070164);f[11]=max(1e-4,e+o*.99409844+m*.32564851+x*.53589066+v*.00593693+w*.70421692+z*.4642044);f[12]=max(1e-4,e+o*.995172+m*.13820628+x*.79874659+v*.00485616+w*.85473994+z*.20123039);f[13]=max(1e-4,e+o*.99576545+m*.05015143+x*.91186529+v*.00426186+w*.95081565+z*.08808402);f[14]=max(1e-4,e+o*.99593552+m*.02912336+x*.95399623+v*.00409039+w*.9717037+z*.04592894);f[15]=max(1e-4,e+o*.99564041+m*.02421691+x*.97137099+v*.00438375+w*.97651888+z*.02860373);f[16]=max(1e-4,e+o*.99464769+m*.02660696+x*.97939505+v*.00537525+w*.97429245+z*.02060067);f[17]=max(1e-4,e+o*.99229579+m*.03407586+x*.98345207+v*.00772962+w*.97012917+z*.01656701);f[18]=max(1e-4,e+o*.98638762+m*.04835936+x*.98553736+v*.0136612+w*.9425863+z*.01451549);f[19]=max(1e-4,e+o*.96829712+m*.0001172+x*.98648905+v*.03181352+w*.99989207+z*.01357964);f[20]=max(1e-4,e+o*.89228016+m*8.554e-5+x*.98674535+v*.10791525+w*.99989891+z*.01331243);f[21]=max(1e-4,e+o*.53740239+m*.85267882+x*.98657555+v*.46249516+w*.13823139+z*.01347661);f[22]=max(1e-4,e+o*.15360445+m*.93188793+x*.98611877+v*.84604333+w*.06968113+z*.01387181);f[23]=max(1e-4,e+o*.05705719+m*.94810268+x*.98559942+v*.94275572+w*.05628787+z*.01435472);f[24]=max(1e-4,e+o*.03126539+m*.94200977+x*.98507063+v*.96860996+w*.06111561+z*.01479836);f[25]=max(1e-4,e+o*.02205445+m*.91478045+x*.98460039+v*.97783966+w*.08987709+z*.0151525);f[26]=max(1e-4,e+o*.01802271+m*.87065445+x*.98425301+v*.98187757+w*.13656016+z*.01540513);f[27]=max(1e-4,e+o*.0161346+m*.78827548+x*.98403909+v*.98377315+w*.22169624+z*.01557233);f[28]=max(1e-4,e+o*.01520947+m*.65738359+x*.98388535+v*.98470202+w*.32176956+z*.0156571);f[29]=max(1e-4,e+o*.01475977+m*.59909403+x*.98376116+v*.98515481+w*.36157329+z*.01571025);f[30]=max(1e-4,e+o*.01454263+m*.56817268+x*.98368246+v*.98537114+w*.4836192+z*.01571916);f[31]=max(1e-4,e+o*.01444459+m*.54031997+x*.98365023+v*.98546685+w*.46488579+z*.01572133);f[32]=max(1e-4,e+o*.01439897+m*.52110241+x*.98361309+v*.98550011+w*.47440306+z*.01572502);f[33]=max(1e-4,e+o*.0143762+m*.51041094+x*.98357259+v*.98551031+w*.4857699+z*.01571717);f[34]=max(1e-4,e+o*.01436343+m*.50526577+x*.98353856+v*.98550741+w*.49267971+z*.01571905);f[35]=max(1e-4,e+o*.01435687+m*.5025508+x*.98351247+v*.98551323+w*.49625685+z*.01571059);f[36]=max(1e-4,e+o*.0143537+m*.50126452+x*.98350101+v*.98551563+w*.49807754+z*.01569728);f[37]=max(1e-4,e+o*.01435408+m*.50083021+x*.98350852+v*.98551547+w*.49889859+z*.0157002);}vec3 spectral_xyz_to_srgb(vec3 e){mat3 f;f[0]=vec3(3.24306333,-1.53837619,-.49893282);f[1]=vec3(-.96896309,1.87542451,.04154303);f[2]=vec3(.05568392,-.20417438,1.05799454);float s=dot(f[0],e),m=dot(f[1],e),z=dot(f[2],e);return spectral_linear_to_srgb(vec3(s,m,z));}vec3 spectral_reflectance_to_xyz(float f[38]){vec3 e=vec3(0);e+=f[0]*vec3(6.469e-5,1.84e-6,.00030502);e+=f[1]*vec3(.00021941,6.21e-6,.00103681);e+=f[2]*vec3(.00112057,3.101e-5,.00531314);e+=f[3]*vec3(.00376661,.00010475,.01795439);e+=f[4]*vec3(.01188055,.00035364,.05707758);e+=f[5]*vec3(.02328644,.00095147,.11365162);e+=f[6]*vec3(.03455942,.00228226,.17335873);e+=f[7]*vec3(.03722379,.00420733,.19620658);e+=f[8]*vec3(.03241838,.0066888,.18608237);e+=f[9]*vec3(.02123321,.0098884,.13995048);e+=f[10]*vec3(.01049099,.01524945,.08917453);e+=f[11]*vec3(.00329584,.02141831,.04789621);e+=f[12]*vec3(.00050704,.03342293,.02814563);e+=f[13]*vec3(.00094867,.05131001,.01613766);e+=f[14]*vec3(.00627372,.07040208,.0077591);e+=f[15]*vec3(.01686462,.08783871,.00429615);e+=f[16]*vec3(.02868965,.09424905,.00200551);e+=f[17]*vec3(.04267481,.09795667,.00086147);e+=f[18]*vec3(.05625475,.09415219,.00036904);e+=f[19]*vec3(.0694704,.08678102,.00019143);e+=f[20]*vec3(.08305315,.07885653,.00014956);e+=f[21]*vec3(.0861261,.0635267,9.231e-5);e+=f[22]*vec3(.09046614,.05374142,6.813e-5);e+=f[23]*vec3(.08500387,.04264606,2.883e-5);e+=f[24]*vec3(.07090667,.03161735,1.577e-5);e+=f[25]*vec3(.05062889,.02088521,3.94e-6);e+=f[26]*vec3(.03547396,.01386011,1.58e-6);e+=f[27]*vec3(.02146821,.00810264,0);e+=f[28]*vec3(.01251646,.0046301,0);e+=f[29]*vec3(.00680458,.00249138,0);e+=f[30]*vec3(.00346457,.0012593,0);e+=f[31]*vec3(.00149761,.00054165,0);e+=f[32]*vec3(.0007697,.00027795,0);e+=f[33]*vec3(.00040737,.00014711,0);e+=f[34]*vec3(.00016901,6.103e-5,0);e+=f[35]*vec3(9.522e-5,3.439e-5,0);e+=f[36]*vec3(4.903e-5,1.771e-5,0);e+=f[37]*vec3(2e-5,7.22e-6,0);return e;}float spectral_linear_to_concentration(float v,float m,float s){float z=m*pow(s,2.);return z/(v*pow(1.-s,2.)+z);}vec3 spectral_mix(vec3 s,vec3 f,float e){vec3 v=spectral_srgb_to_linear(s),m=spectral_srgb_to_linear(f);float z[38],o[38];spectral_linear_to_reflectance(v,z);spectral_linear_to_reflectance(m,o);float x=spectral_reflectance_to_xyz(z)[1],w=spectral_reflectance_to_xyz(o)[1];e=spectral_linear_to_concentration(x,w,e);float r[38];for(int c=0;c<38;c++){float y=(1.-e)*(pow(1.-z[c],2.)/(2.*z[c]))+e*(pow(1.-o[c],2.)/(2.*o[c]));r[c]=1.+y-sqrt(pow(y,2.)+2.*y);}return spectral_xyz_to_srgb(spectral_reflectance_to_xyz(r));}vec4 spectral_mix(vec4 s,vec4 e,float z){return vec4(spectral_mix(s.xyz,e.xyz,z),mix(s.w,e.w,z));}`;+}\ No newline at end of filediff --git a/spectral.js b/spectral.jsdeleted file mode 100644index 5da9e3e..0000000--- a/spectral.js+++ /dev/null@@ -1,517 +0,0 @@-// MIT License-//-// Copyright (c) 2023 Ronald van Wijnen-//-// Permission is hereby granted, free of charge, to any person obtaining a-// copy of this software and associated documentation files (the "Software"),-// to deal in the Software without restriction, including without limitation-// the rights to use, copy, modify, merge, publish, distribute, sublicense,-// and/or sell copies of the Software, and to permit persons to whom the-// Software is furnished to do so, subject to the following conditions:-//-// The above copyright notice and this permission notice shall be included in-// all copies or substantial portions of the Software.-//-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR-// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,-// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE-// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER-// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING-// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER-// DEALINGS IN THE SOFTWARE.--(function (global, factory) {- typeof exports === 'object' && typeof module !== 'undefined'- ? factory(exports)- : typeof define === 'function' && define.amd- ? define(['exports'], factory)- : ((global = global || self), factory((global.spectral = {})));-})(this, function (exports) {- ('use strict');-- const RGB = 0;- const RGBA = 1;- const HEX = 2;- const HEXA = 3;-- const SIZE = 38;- const GAMMA = 2.4;- const EPSILON = 0.00000001;-- function linear_to_concentration(l1, l2, t) {- let t1 = l1 * (1 - t) ** 2;- let t2 = l2 * t ** 2;-- return t2 / (t1 + t2);- }-- function mix(color1, color2, t, returnFormat) {- color1 = unpack(color1);- color2 = unpack(color2);-- let lrgb1 = srgb_to_linear(color1);- let lrgb2 = srgb_to_linear(color2);-- let R1 = linear_to_reflectance(lrgb1);- let R2 = linear_to_reflectance(lrgb2);-- let l1 = dotproduct(R1, CIE_CMF_Y);- let l2 = dotproduct(R2, CIE_CMF_Y);-- t = linear_to_concentration(l1, l2, t);-- const R = new Array(SIZE);-- for (let i = 0; i < SIZE; i++) {- let KS = (1 - t) * ((1 - R1[i]) ** 2 / (2 * R1[i])) + t * ((1 - R2[i]) ** 2 / (2 * R2[i]));- let KM = 1 + KS - Math.sqrt(KS ** 2 + 2 * KS);-- //Saunderson correction- // let S = ((1.0 - K1) * (1.0 - K2) * KM) / (1.0 - K2 * KM);-- R[i] = KM;- }-- let rgb = xyz_to_srgb(reflectance_to_xyz(R));-- rgb.push(lerp(color1[3], color2[3], t));-- return pack(rgb, returnFormat);- }-- function palette(color1, color2, size, returnFormat) {- let g = [];-- for (let i = 0; i < size; i++) {- g.push(mix(color1, color2, i / (size - 1), returnFormat));- }-- return g;- }-- function uncompand(x) {- return x < 0.04045 ? x / 12.92 : ((x + 0.055) / 1.055) ** GAMMA;- }-- function compand(x) {- return x < 0.0031308 ? x * 12.92 : 1.055 * x ** (1.0 / GAMMA) - 0.055;- }-- function srgb_to_linear(srgb) {- let r = uncompand(srgb[0] / 255.0);- let g = uncompand(srgb[1] / 255.0);- let b = uncompand(srgb[2] / 255.0);-- return [r, g, b];- }-- function linear_to_srgb(lrgb) {- let r = compand(lrgb[0]);- let g = compand(lrgb[1]);- let b = compand(lrgb[2]);-- return [Math.round(clamp(r, 0, 1) * 255), Math.round(clamp(g, 0, 1) * 255), Math.round(clamp(b, 0, 1) * 255)];- }-- function xyz_to_srgb(xyz) {- let r = dotproduct(XYZ_RGB[0], xyz);- let g = dotproduct(XYZ_RGB[1], xyz);- let b = dotproduct(XYZ_RGB[2], xyz);-- return linear_to_srgb([r, g, b]);- }-- function reflectance_to_xyz(R) {- let x = dotproduct(R, CIE_CMF_X);- let y = dotproduct(R, CIE_CMF_Y);- let z = dotproduct(R, CIE_CMF_Z);-- return [x, y, z];- }-- function spectral_upsampling(lrgb) {- let w = Math.min(Math.min(lrgb[0], lrgb[1]), lrgb[2]);-- lrgb = [lrgb[0] - w, lrgb[1] - w, lrgb[2] - w];-- let c = Math.min(lrgb[1], lrgb[2]);- let m = Math.min(lrgb[0], lrgb[2]);- let y = Math.min(lrgb[0], lrgb[1]);- let r = Math.max(0, Math.min(lrgb[0] - lrgb[2], lrgb[0] - lrgb[1]));- let g = Math.max(0, Math.min(lrgb[1] - lrgb[2], lrgb[1] - lrgb[0]));- let b = Math.max(0, Math.min(lrgb[2] - lrgb[1], lrgb[2] - lrgb[0]));-- return [w, c, m, y, r, g, b];- }-- function linear_to_reflectance(lrgb) {- let weights = spectral_upsampling(lrgb);-- const R = new Array(SIZE);-- for (let i = 0; i < SIZE; i++) {- R[i] = Math.max(- EPSILON,- weights[0] + weights[1] * SPD_C[i] + weights[2] * SPD_M[i] + weights[3] * SPD_Y[i] + weights[4] * SPD_R[i] + weights[5] * SPD_G[i] + weights[6] * SPD_B[i]- );- }-- return R;- }-- function lerp(a, b, alpha) {- return a + alpha * (b - a);- }-- function clamp(value, min, max) {- return Math.min(Math.max(value, min), max);- }-- function dotproduct(a, b) {- return a.map((x, i) => a[i] * b[i]).reduce((m, n) => m + n);- }-- function glsl_color(c) {- let rgba = unpack(c);-- return [rgba[0] / 255, rgba[1] / 255, rgba[2] / 255, rgba[3] > 1 ? rgba[3] / 255 : rgba[3]];- }-- function unpack(color) {- if (Array.isArray(color)) {- const [r, g, b, a = 1] = color;-- return [r, g, b, a];- }-- if (color.startsWith('rgb')) {- color = color- .slice(color.indexOf('(') + 1, -1)- .split(',')- .map((value) => (value.trim().endsWith('%') ? Math.round(parseFloat(value.trim()) * 2.55) : Number(value.trim())));-- const [r, g, b, a = 1] = color;-- return [r, g, b, a];- }-- if (color.startsWith('#')) {- if (color.length === 4 || color.length === 5) {- color = color- .split('')- .slice(1)- .map((value) => value + value)- .join('');- } else {- color = color.slice(1);- }-- color = color.match(/.{1,2}/g).map((value) => parseInt(value, 16));-- const [r, g, b, a = 1] = color.length === 3 ? [...color, 1] : color;-- return [r, g, b, a];- }-- return [0, 0, 0, 1];- }-- function pack(srgb, returnFormat) {- let r = srgb[0];- let g = srgb[1];- let b = srgb[2];- let a = srgb[3];-- if (returnFormat === RGB || returnFormat === RGBA) {- return `rgb${returnFormat === RGBA ? 'a' : ''}(${r}, ${g}, ${b}${returnFormat === RGBA ? ', ' + a : ''})`;- }-- r = r.toString(16);- g = g.toString(16);- b = b.toString(16);- a = (a > 1 ? a : Math.round(clamp(a, 0, 1) * 255)).toString(16);-- if (r.length == 1) r = '0' + r;- if (g.length == 1) g = '0' + g;- if (b.length == 1) b = '0' + b;- if (a.length == 1) a = '0' + a;-- return `#${r}${g}${b}${returnFormat === HEXA ? a : ''}`;- }-- // K1 and K2 are used for the Saunderson correction which is used for how the color looks behind a refractive- // surface like glass- //- // 1 is used for a vacuum, no surface- //- // Refractive Index- //- // https://en.wikipedia.org/wiki/Refractive_index- // https://en.wikipedia.org/wiki/List_of_refractive_indices- //- // Glass = 1.5- // Vacuum = 1-- const RI = 1.0;- const K1 = (RI - 1) ** 2 / (RI + 1) ** 2;-- // 0 = neutral, - = lighten, + = darken- const K2 = 0;-- const SPD_C = [- 0.96853629, 0.96855103, 0.96859338, 0.96877345, 0.96942204, 0.97143709, 0.97541862, 0.98074186, 0.98580992, 0.98971194, 0.99238027, 0.99409844, 0.995172, 0.99576545,- 0.99593552, 0.99564041, 0.99464769, 0.99229579, 0.98638762, 0.96829712, 0.89228016, 0.53740239, 0.15360445, 0.05705719, 0.03126539, 0.02205445, 0.01802271, 0.0161346,- 0.01520947, 0.01475977, 0.01454263, 0.01444459, 0.01439897, 0.0143762, 0.01436343, 0.01435687, 0.0143537, 0.01435408,- ];-- const SPD_M = [- 0.51567122, 0.5401552, 0.62645502, 0.75595012, 0.92826996, 0.97223624, 0.98616174, 0.98955255, 0.98676237, 0.97312575, 0.91944277, 0.32564851, 0.13820628, 0.05015143,- 0.02912336, 0.02421691, 0.02660696, 0.03407586, 0.04835936, 0.0001172, 0.00008554, 0.85267882, 0.93188793, 0.94810268, 0.94200977, 0.91478045, 0.87065445, 0.78827548,- 0.65738359, 0.59909403, 0.56817268, 0.54031997, 0.52110241, 0.51041094, 0.50526577, 0.5025508, 0.50126452, 0.50083021,- ];-- const SPD_Y = [- 0.02055257, 0.02059936, 0.02062723, 0.02073387, 0.02114202, 0.02233154, 0.02556857, 0.03330189, 0.05185294, 0.10087639, 0.24000413, 0.53589066, 0.79874659, 0.91186529,- 0.95399623, 0.97137099, 0.97939505, 0.98345207, 0.98553736, 0.98648905, 0.98674535, 0.98657555, 0.98611877, 0.98559942, 0.98507063, 0.98460039, 0.98425301, 0.98403909,- 0.98388535, 0.98376116, 0.98368246, 0.98365023, 0.98361309, 0.98357259, 0.98353856, 0.98351247, 0.98350101, 0.98350852,- ];-- const SPD_R = [- 0.03147571, 0.03146636, 0.03140624, 0.03119611, 0.03053888, 0.02856855, 0.02459485, 0.0192952, 0.01423112, 0.01033111, 0.00765876, 0.00593693, 0.00485616, 0.00426186,- 0.00409039, 0.00438375, 0.00537525, 0.00772962, 0.0136612, 0.03181352, 0.10791525, 0.46249516, 0.84604333, 0.94275572, 0.96860996, 0.97783966, 0.98187757, 0.98377315,- 0.98470202, 0.98515481, 0.98537114, 0.98546685, 0.98550011, 0.98551031, 0.98550741, 0.98551323, 0.98551563, 0.98551547,- ];-- const SPD_G = [- 0.49108579, 0.46944057, 0.4016578, 0.2449042, 0.0682688, 0.02732883, 0.013606, 0.01000187, 0.01284127, 0.02636635, 0.07058713, 0.70421692, 0.85473994, 0.95081565, 0.9717037,- 0.97651888, 0.97429245, 0.97012917, 0.9425863, 0.99989207, 0.99989891, 0.13823139, 0.06968113, 0.05628787, 0.06111561, 0.08987709, 0.13656016, 0.22169624, 0.32176956,- 0.36157329, 0.4836192, 0.46488579, 0.47440306, 0.4857699, 0.49267971, 0.49625685, 0.49807754, 0.49889859,- ];-- const SPD_B = [- 0.97901834, 0.97901649, 0.97901118, 0.97892146, 0.97858555, 0.97743705, 0.97428075, 0.96663223, 0.94822893, 0.89937713, 0.76070164, 0.4642044, 0.20123039, 0.08808402,- 0.04592894, 0.02860373, 0.02060067, 0.01656701, 0.01451549, 0.01357964, 0.01331243, 0.01347661, 0.01387181, 0.01435472, 0.01479836, 0.0151525, 0.01540513, 0.01557233,- 0.0156571, 0.01571025, 0.01571916, 0.01572133, 0.01572502, 0.01571717, 0.01571905, 0.01571059, 0.01569728, 0.0157002,- ];-- const CIE_CMF_X = [- 0.00006469, 0.00021941, 0.00112057, 0.00376661, 0.01188055, 0.02328644, 0.03455942, 0.03722379, 0.03241838, 0.02123321, 0.01049099, 0.00329584, 0.00050704, 0.00094867,- 0.00627372, 0.01686462, 0.02868965, 0.04267481, 0.05625475, 0.0694704, 0.08305315, 0.0861261, 0.09046614, 0.08500387, 0.07090667, 0.05062889, 0.03547396, 0.02146821,- 0.01251646, 0.00680458, 0.00346457, 0.00149761, 0.0007697, 0.00040737, 0.00016901, 0.00009522, 0.00004903, 0.00002,- ];-- const CIE_CMF_Y = [- 0.00000184, 0.00000621, 0.00003101, 0.00010475, 0.00035364, 0.00095147, 0.00228226, 0.00420733, 0.0066888, 0.0098884, 0.01524945, 0.02141831, 0.03342293, 0.05131001,- 0.07040208, 0.08783871, 0.09424905, 0.09795667, 0.09415219, 0.08678102, 0.07885653, 0.0635267, 0.05374142, 0.04264606, 0.03161735, 0.02088521, 0.01386011, 0.00810264,- 0.0046301, 0.00249138, 0.0012593, 0.00054165, 0.00027795, 0.00014711, 0.00006103, 0.00003439, 0.00001771, 0.00000722,- ];-- const CIE_CMF_Z = [- 0.00030502, 0.00103681, 0.00531314, 0.01795439, 0.05707758, 0.11365162, 0.17335873, 0.19620658, 0.18608237, 0.13995048, 0.08917453, 0.04789621, 0.02814563, 0.01613766,- 0.0077591, 0.00429615, 0.00200551, 0.00086147, 0.00036904, 0.00019143, 0.00014956, 0.00009231, 0.00006813, 0.00002883, 0.00001577, 0.00000394, 0.00000158, 0.0, 0.0, 0.0, 0.0,- 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,- ];-- const XYZ_RGB = [- [3.24306333, -1.53837619, -0.49893282],- [-0.96896309, 1.87542451, 0.04154303],- [0.05568392, -0.20417438, 1.05799454],- ];-- function glsl() {- return `-#ifndef SPECTRAL-#define SPECTRAL--const int SPECTRAL_SIZE = 38;-const float SPECTRAL_GAMMA = 2.4;-const float SPECTRAL_EPSILON = 0.0001;--float spectral_uncompand(float x) {- return (x < 0.04045) ? x / 12.92 : pow((x + 0.055) / 1.055, SPECTRAL_GAMMA);-}--float spectral_compand(float x) {- return (x < 0.0031308) ? x * 12.92 : 1.055 * pow(x, 1.0 / SPECTRAL_GAMMA) - 0.055;-}--vec3 spectral_srgb_to_linear(vec3 srgb) {- return vec3(spectral_uncompand(srgb[0]), spectral_uncompand(srgb[1]), spectral_uncompand(srgb[2]));-}--vec3 spectral_linear_to_srgb(vec3 lrgb) {- return clamp(vec3(spectral_compand(lrgb[0]), spectral_compand(lrgb[1]), spectral_compand(lrgb[2])), 0.0, 1.0);-}--void spectral_upsampling(vec3 lrgb, out float w, out float c, out float m, out float y, out float r, out float g, out float b) {- w = min(lrgb.r, min(lrgb.g, lrgb.b));-- lrgb -= w;-- c = min(lrgb.g, lrgb.b);- m = min(lrgb.r, lrgb.b);- y = min(lrgb.r, lrgb.g);- r = min(max(0., lrgb.r - lrgb.b), max(0., lrgb.r - lrgb.g));- g = min(max(0., lrgb.g - lrgb.b), max(0., lrgb.g - lrgb.r));- b = min(max(0., lrgb.b - lrgb.g), max(0., lrgb.b - lrgb.r));-}--void spectral_linear_to_reflectance(vec3 lrgb, inout float R[SPECTRAL_SIZE]) {- float w, c, m, y, r, g, b;-- spectral_upsampling(lrgb, w, c, m, y, r, g, b);-- R[0] = max(SPECTRAL_EPSILON, w + c * 0.96853629 + m * 0.51567122 + y * 0.02055257 + r * 0.03147571 + g * 0.49108579 + b * 0.97901834);- R[1] = max(SPECTRAL_EPSILON, w + c * 0.96855103 + m * 0.54015520 + y * 0.02059936 + r * 0.03146636 + g * 0.46944057 + b * 0.97901649);- R[2] = max(SPECTRAL_EPSILON, w + c * 0.96859338 + m * 0.62645502 + y * 0.02062723 + r * 0.03140624 + g * 0.40165780 + b * 0.97901118);- R[3] = max(SPECTRAL_EPSILON, w + c * 0.96877345 + m * 0.75595012 + y * 0.02073387 + r * 0.03119611 + g * 0.24490420 + b * 0.97892146);- R[4] = max(SPECTRAL_EPSILON, w + c * 0.96942204 + m * 0.92826996 + y * 0.02114202 + r * 0.03053888 + g * 0.06826880 + b * 0.97858555);- R[5] = max(SPECTRAL_EPSILON, w + c * 0.97143709 + m * 0.97223624 + y * 0.02233154 + r * 0.02856855 + g * 0.02732883 + b * 0.97743705);- R[6] = max(SPECTRAL_EPSILON, w + c * 0.97541862 + m * 0.98616174 + y * 0.02556857 + r * 0.02459485 + g * 0.01360600 + b * 0.97428075);- R[7] = max(SPECTRAL_EPSILON, w + c * 0.98074186 + m * 0.98955255 + y * 0.03330189 + r * 0.01929520 + g * 0.01000187 + b * 0.96663223);- R[8] = max(SPECTRAL_EPSILON, w + c * 0.98580992 + m * 0.98676237 + y * 0.05185294 + r * 0.01423112 + g * 0.01284127 + b * 0.94822893);- R[9] = max(SPECTRAL_EPSILON, w + c * 0.98971194 + m * 0.97312575 + y * 0.10087639 + r * 0.01033111 + g * 0.02636635 + b * 0.89937713);- R[10] = max(SPECTRAL_EPSILON, w + c * 0.99238027 + m * 0.91944277 + y * 0.24000413 + r * 0.00765876 + g * 0.07058713 + b * 0.76070164);- R[11] = max(SPECTRAL_EPSILON, w + c * 0.99409844 + m * 0.32564851 + y * 0.53589066 + r * 0.00593693 + g * 0.70421692 + b * 0.46420440);- R[12] = max(SPECTRAL_EPSILON, w + c * 0.99517200 + m * 0.13820628 + y * 0.79874659 + r * 0.00485616 + g * 0.85473994 + b * 0.20123039);- R[13] = max(SPECTRAL_EPSILON, w + c * 0.99576545 + m * 0.05015143 + y * 0.91186529 + r * 0.00426186 + g * 0.95081565 + b * 0.08808402);- R[14] = max(SPECTRAL_EPSILON, w + c * 0.99593552 + m * 0.02912336 + y * 0.95399623 + r * 0.00409039 + g * 0.97170370 + b * 0.04592894);- R[15] = max(SPECTRAL_EPSILON, w + c * 0.99564041 + m * 0.02421691 + y * 0.97137099 + r * 0.00438375 + g * 0.97651888 + b * 0.02860373);- R[16] = max(SPECTRAL_EPSILON, w + c * 0.99464769 + m * 0.02660696 + y * 0.97939505 + r * 0.00537525 + g * 0.97429245 + b * 0.02060067);- R[17] = max(SPECTRAL_EPSILON, w + c * 0.99229579 + m * 0.03407586 + y * 0.98345207 + r * 0.00772962 + g * 0.97012917 + b * 0.01656701);- R[18] = max(SPECTRAL_EPSILON, w + c * 0.98638762 + m * 0.04835936 + y * 0.98553736 + r * 0.01366120 + g * 0.94258630 + b * 0.01451549);- R[19] = max(SPECTRAL_EPSILON, w + c * 0.96829712 + m * 0.00011720 + y * 0.98648905 + r * 0.03181352 + g * 0.99989207 + b * 0.01357964);- R[20] = max(SPECTRAL_EPSILON, w + c * 0.89228016 + m * 0.00008554 + y * 0.98674535 + r * 0.10791525 + g * 0.99989891 + b * 0.01331243);- R[21] = max(SPECTRAL_EPSILON, w + c * 0.53740239 + m * 0.85267882 + y * 0.98657555 + r * 0.46249516 + g * 0.13823139 + b * 0.01347661);- R[22] = max(SPECTRAL_EPSILON, w + c * 0.15360445 + m * 0.93188793 + y * 0.98611877 + r * 0.84604333 + g * 0.06968113 + b * 0.01387181);- R[23] = max(SPECTRAL_EPSILON, w + c * 0.05705719 + m * 0.94810268 + y * 0.98559942 + r * 0.94275572 + g * 0.05628787 + b * 0.01435472);- R[24] = max(SPECTRAL_EPSILON, w + c * 0.03126539 + m * 0.94200977 + y * 0.98507063 + r * 0.96860996 + g * 0.06111561 + b * 0.01479836);- R[25] = max(SPECTRAL_EPSILON, w + c * 0.02205445 + m * 0.91478045 + y * 0.98460039 + r * 0.97783966 + g * 0.08987709 + b * 0.01515250);- R[26] = max(SPECTRAL_EPSILON, w + c * 0.01802271 + m * 0.87065445 + y * 0.98425301 + r * 0.98187757 + g * 0.13656016 + b * 0.01540513);- R[27] = max(SPECTRAL_EPSILON, w + c * 0.01613460 + m * 0.78827548 + y * 0.98403909 + r * 0.98377315 + g * 0.22169624 + b * 0.01557233);- R[28] = max(SPECTRAL_EPSILON, w + c * 0.01520947 + m * 0.65738359 + y * 0.98388535 + r * 0.98470202 + g * 0.32176956 + b * 0.01565710);- R[29] = max(SPECTRAL_EPSILON, w + c * 0.01475977 + m * 0.59909403 + y * 0.98376116 + r * 0.98515481 + g * 0.36157329 + b * 0.01571025);- R[30] = max(SPECTRAL_EPSILON, w + c * 0.01454263 + m * 0.56817268 + y * 0.98368246 + r * 0.98537114 + g * 0.48361920 + b * 0.01571916);- R[31] = max(SPECTRAL_EPSILON, w + c * 0.01444459 + m * 0.54031997 + y * 0.98365023 + r * 0.98546685 + g * 0.46488579 + b * 0.01572133);- R[32] = max(SPECTRAL_EPSILON, w + c * 0.01439897 + m * 0.52110241 + y * 0.98361309 + r * 0.98550011 + g * 0.47440306 + b * 0.01572502);- R[33] = max(SPECTRAL_EPSILON, w + c * 0.01437620 + m * 0.51041094 + y * 0.98357259 + r * 0.98551031 + g * 0.48576990 + b * 0.01571717);- R[34] = max(SPECTRAL_EPSILON, w + c * 0.01436343 + m * 0.50526577 + y * 0.98353856 + r * 0.98550741 + g * 0.49267971 + b * 0.01571905);- R[35] = max(SPECTRAL_EPSILON, w + c * 0.01435687 + m * 0.50255080 + y * 0.98351247 + r * 0.98551323 + g * 0.49625685 + b * 0.01571059);- R[36] = max(SPECTRAL_EPSILON, w + c * 0.01435370 + m * 0.50126452 + y * 0.98350101 + r * 0.98551563 + g * 0.49807754 + b * 0.01569728);- R[37] = max(SPECTRAL_EPSILON, w + c * 0.01435408 + m * 0.50083021 + y * 0.98350852 + r * 0.98551547 + g * 0.49889859 + b * 0.01570020);-}--vec3 spectral_xyz_to_srgb(vec3 xyz) {- mat3 XYZ_RGB;-- XYZ_RGB[0] = vec3( 3.24306333, -1.53837619, -0.49893282);- XYZ_RGB[1] = vec3(-0.96896309, 1.87542451, 0.04154303);- XYZ_RGB[2] = vec3( 0.05568392, -0.20417438, 1.05799454);-- float r = dot(XYZ_RGB[0], xyz);- float g = dot(XYZ_RGB[1], xyz);- float b = dot(XYZ_RGB[2], xyz);-- return spectral_linear_to_srgb(vec3(r, g, b));-}--vec3 spectral_reflectance_to_xyz(float R[SPECTRAL_SIZE]) {- vec3 xyz = vec3(0.0);-- xyz += R[0] * vec3(0.00006469, 0.00000184, 0.00030502);- xyz += R[1] * vec3(0.00021941, 0.00000621, 0.00103681);- xyz += R[2] * vec3(0.00112057, 0.00003101, 0.00531314);- xyz += R[3] * vec3(0.00376661, 0.00010475, 0.01795439);- xyz += R[4] * vec3(0.01188055, 0.00035364, 0.05707758);- xyz += R[5] * vec3(0.02328644, 0.00095147, 0.11365162);- xyz += R[6] * vec3(0.03455942, 0.00228226, 0.17335873);- xyz += R[7] * vec3(0.03722379, 0.00420733, 0.19620658);- xyz += R[8] * vec3(0.03241838, 0.00668880, 0.18608237);- xyz += R[9] * vec3(0.02123321, 0.00988840, 0.13995048);- xyz += R[10] * vec3(0.01049099, 0.01524945, 0.08917453);- xyz += R[11] * vec3(0.00329584, 0.02141831, 0.04789621);- xyz += R[12] * vec3(0.00050704, 0.03342293, 0.02814563);- xyz += R[13] * vec3(0.00094867, 0.05131001, 0.01613766);- xyz += R[14] * vec3(0.00627372, 0.07040208, 0.00775910);- xyz += R[15] * vec3(0.01686462, 0.08783871, 0.00429615);- xyz += R[16] * vec3(0.02868965, 0.09424905, 0.00200551);- xyz += R[17] * vec3(0.04267481, 0.09795667, 0.00086147);- xyz += R[18] * vec3(0.05625475, 0.09415219, 0.00036904);- xyz += R[19] * vec3(0.06947040, 0.08678102, 0.00019143);- xyz += R[20] * vec3(0.08305315, 0.07885653, 0.00014956);- xyz += R[21] * vec3(0.08612610, 0.06352670, 0.00009231);- xyz += R[22] * vec3(0.09046614, 0.05374142, 0.00006813);- xyz += R[23] * vec3(0.08500387, 0.04264606, 0.00002883);- xyz += R[24] * vec3(0.07090667, 0.03161735, 0.00001577);- xyz += R[25] * vec3(0.05062889, 0.02088521, 0.00000394);- xyz += R[26] * vec3(0.03547396, 0.01386011, 0.00000158);- xyz += R[27] * vec3(0.02146821, 0.00810264, 0.00000000);- xyz += R[28] * vec3(0.01251646, 0.00463010, 0.00000000);- xyz += R[29] * vec3(0.00680458, 0.00249138, 0.00000000);- xyz += R[30] * vec3(0.00346457, 0.00125930, 0.00000000);- xyz += R[31] * vec3(0.00149761, 0.00054165, 0.00000000);- xyz += R[32] * vec3(0.00076970, 0.00027795, 0.00000000);- xyz += R[33] * vec3(0.00040737, 0.00014711, 0.00000000);- xyz += R[34] * vec3(0.00016901, 0.00006103, 0.00000000);- xyz += R[35] * vec3(0.00009522, 0.00003439, 0.00000000);- xyz += R[36] * vec3(0.00004903, 0.00001771, 0.00000000);- xyz += R[37] * vec3(0.00002000, 0.00000722, 0.00000000);-- return xyz;-}--float spectral_linear_to_concentration(float l1, float l2, float t) {- float t1 = l1 * pow(1.0 - t, 2.0);- float t2 = l2 * pow(t, 2.0);-- return t2 / (t1 + t2);-}--vec3 spectral_mix(vec3 color1, vec3 color2, float t) {- vec3 lrgb1 = spectral_srgb_to_linear(color1);- vec3 lrgb2 = spectral_srgb_to_linear(color2);-- float R1[SPECTRAL_SIZE];- float R2[SPECTRAL_SIZE];-- spectral_linear_to_reflectance(lrgb1, R1);- spectral_linear_to_reflectance(lrgb2, R2);-- float l1 = spectral_reflectance_to_xyz(R1)[1];- float l2 = spectral_reflectance_to_xyz(R2)[1];-- t = spectral_linear_to_concentration(l1, l2, t);-- float R[SPECTRAL_SIZE];-- for (int i = 0; i < SPECTRAL_SIZE; i++) {- float KS = (1.0 - t) * (pow(1.0 - R1[i], 2.0) / (2.0 * R1[i])) + t * (pow(1.0 - R2[i], 2.0) / (2.0 * R2[i]));- float KM = 1.0 + KS - sqrt(pow(KS, 2.0) + 2.0 * KS);-- //Saunderson correction- // let S = ((1.0 - K1) * (1.0 - K2) * KM) / (1.0 - K2 * KM);-- R[i] = KM;- }-- return spectral_xyz_to_srgb(spectral_reflectance_to_xyz(R));-}--vec4 spectral_mix(vec4 color1, vec4 color2, float t) {- return vec4(spectral_mix(color1.rgb, color2.rgb, t), mix(color1.a, color2.a, t));-}--#endif- `;- }-- exports.RGB = RGB;- exports.RGBA = RGBA;- exports.HEX = HEX;- exports.HEXA = HEXA;-- exports.mix = mix;- exports.palette = palette;- exports.glsl_color = glsl_color;- exports.glsl = glsl;-});diff --git a/spectral.min.js b/spectral.min.jsnew file mode 100644index 0000000..ec37aed--- /dev/null+++ b/spectral.min.js@@ -0,0 +1 @@+var n,t;n=this,t=function(n){const t=0,r=1,e=3,c=38,a=2.4,l=1e-8;function o(n,a,l,o){n=b(n),a=b(a);let R=_(n),y=_(a),E=x(R),i=x(y);l=function(n,t,r){let e=t*r**2;return e/(n*(1-r)**2+e)}(S(E,u),S(i,u),l);const L=new Array(c);for(let n=0;n<c;n++){let t=(1-l)*((1-E[n])**2/(2*E[n]))+l*((1-i[n])**2/(2*i[n])),r=1+t-Math.sqrt(t**2+2*t);L[n]=r}let P=function(n){let t=S(p[0],n),r=S(p[1],n),e=S(p[2],n);return function(n){let t=m(n[0]),r=m(n[1]),e=m(n[2]);return[Math.round(255*g(t,0,1)),Math.round(255*g(r,0,1)),Math.round(255*g(e,0,1))]}([t,r,e])}(function(n){let t=S(n,s),r=S(n,u),e=S(n,v);return[t,r,e]}(L));var f,A;return P.push((f=n[3],A=a[3],f+l*(A-f))),function(n,c){let a=n[0],l=n[1],o=n[2],R=n[3];return c===t||c===r?`rgb${c===r?"a":""}(${a}, ${l}, ${o}${c===r?", "+R:""})`:(a=a.toString(16),l=l.toString(16),o=o.toString(16),R=(R>1?R:Math.round(255*g(R,0,1))).toString(16),1==a.length&&(a="0"+a),1==l.length&&(l="0"+l),1==o.length&&(o="0"+o),1==R.length&&(R="0"+R),`#${a}${l}${o}${c===e?R:""}`)}(P,o)}function R(n){return n<.04045?n/12.92:((n+.055)/1.055)**a}function m(n){return n<.0031308?12.92*n:1.055*n**(1/a)-.055}function _(n){return[R(n[0]/255),R(n[1]/255),R(n[2]/255)]}function x(n){let t=function(n){let t=Math.min(Math.min(n[0],n[1]),n[2]);return n=[n[0]-t,n[1]-t,n[2]-t],[t,Math.min(n[1],n[2]),Math.min(n[0],n[2]),Math.min(n[0],n[1]),Math.max(0,Math.min(n[0]-n[2],n[0]-n[1])),Math.max(0,Math.min(n[1]-n[2],n[1]-n[0])),Math.max(0,Math.min(n[2]-n[1],n[2]-n[0]))]}(n);const r=new Array(c);for(let n=0;n<c;n++)r[n]=Math.max(l,t[0]+t[1]*y[n]+t[2]*E[n]+t[3]*i[n]+t[4]*L[n]+t[5]*P[n]+t[6]*f[n]);return r}function g(n,t,r){return Math.min(Math.max(n,t),r)}function S(n,t){return n.map(((r,e)=>n[e]*t[e])).reduce(((n,t)=>n+t))}function b(n){if(Array.isArray(n)){const[t,r,e,c=1]=n;return[t,r,e,c]}if(n.startsWith("rgb")){n=n.slice(n.indexOf("(")+1,-1).split(",").map((n=>n.trim().endsWith("%")?Math.round(2.55*parseFloat(n.trim())):Number(n.trim())));const[t,r,e,c=1]=n;return[t,r,e,c]}if(n.startsWith("#")){n=(n=4===n.length||5===n.length?n.split("").slice(1).map((n=>n+n)).join(""):n.slice(1)).match(/.{1,2}/g).map((n=>parseInt(n,16)));const[t,r,e,c=1]=3===n.length?[...n,1]:n;return[t,r,e,c]}return[0,0,0,1]}const y=[.96853629,.96855103,.96859338,.96877345,.96942204,.97143709,.97541862,.98074186,.98580992,.98971194,.99238027,.99409844,.995172,.99576545,.99593552,.99564041,.99464769,.99229579,.98638762,.96829712,.89228016,.53740239,.15360445,.05705719,.03126539,.02205445,.01802271,.0161346,.01520947,.01475977,.01454263,.01444459,.01439897,.0143762,.01436343,.01435687,.0143537,.01435408],E=[.51567122,.5401552,.62645502,.75595012,.92826996,.97223624,.98616174,.98955255,.98676237,.97312575,.91944277,.32564851,.13820628,.05015143,.02912336,.02421691,.02660696,.03407586,.04835936,1172e-7,8554e-8,.85267882,.93188793,.94810268,.94200977,.91478045,.87065445,.78827548,.65738359,.59909403,.56817268,.54031997,.52110241,.51041094,.50526577,.5025508,.50126452,.50083021],i=[.02055257,.02059936,.02062723,.02073387,.02114202,.02233154,.02556857,.03330189,.05185294,.10087639,.24000413,.53589066,.79874659,.91186529,.95399623,.97137099,.97939505,.98345207,.98553736,.98648905,.98674535,.98657555,.98611877,.98559942,.98507063,.98460039,.98425301,.98403909,.98388535,.98376116,.98368246,.98365023,.98361309,.98357259,.98353856,.98351247,.98350101,.98350852],L=[.03147571,.03146636,.03140624,.03119611,.03053888,.02856855,.02459485,.0192952,.01423112,.01033111,.00765876,.00593693,.00485616,.00426186,.00409039,.00438375,.00537525,.00772962,.0136612,.03181352,.10791525,.46249516,.84604333,.94275572,.96860996,.97783966,.98187757,.98377315,.98470202,.98515481,.98537114,.98546685,.98550011,.98551031,.98550741,.98551323,.98551563,.98551547],P=[.49108579,.46944057,.4016578,.2449042,.0682688,.02732883,.013606,.01000187,.01284127,.02636635,.07058713,.70421692,.85473994,.95081565,.9717037,.97651888,.97429245,.97012917,.9425863,.99989207,.99989891,.13823139,.06968113,.05628787,.06111561,.08987709,.13656016,.22169624,.32176956,.36157329,.4836192,.46488579,.47440306,.4857699,.49267971,.49625685,.49807754,.49889859],f=[.97901834,.97901649,.97901118,.97892146,.97858555,.97743705,.97428075,.96663223,.94822893,.89937713,.76070164,.4642044,.20123039,.08808402,.04592894,.02860373,.02060067,.01656701,.01451549,.01357964,.01331243,.01347661,.01387181,.01435472,.01479836,.0151525,.01540513,.01557233,.0156571,.01571025,.01571916,.01572133,.01572502,.01571717,.01571905,.01571059,.01569728,.0157002],s=[6469e-8,21941e-8,.00112057,.00376661,.01188055,.02328644,.03455942,.03722379,.03241838,.02123321,.01049099,.00329584,50704e-8,94867e-8,.00627372,.01686462,.02868965,.04267481,.05625475,.0694704,.08305315,.0861261,.09046614,.08500387,.07090667,.05062889,.03547396,.02146821,.01251646,.00680458,.00346457,.00149761,7697e-7,40737e-8,16901e-8,9522e-8,4903e-8,2e-5],u=[184e-8,621e-8,3101e-8,10475e-8,35364e-8,95147e-8,.00228226,.00420733,.0066888,.0098884,.01524945,.02141831,.03342293,.05131001,.07040208,.08783871,.09424905,.09795667,.09415219,.08678102,.07885653,.0635267,.05374142,.04264606,.03161735,.02088521,.01386011,.00810264,.0046301,.00249138,.0012593,54165e-8,27795e-8,14711e-8,6103e-8,3439e-8,1771e-8,722e-8],v=[30502e-8,.00103681,.00531314,.01795439,.05707758,.11365162,.17335873,.19620658,.18608237,.13995048,.08917453,.04789621,.02814563,.01613766,.0077591,.00429615,.00200551,86147e-8,36904e-8,19143e-8,14956e-8,9231e-8,6813e-8,2883e-8,1577e-8,394e-8,158e-8,0,0,0,0,0,0,0,0,0,0,0],p=[[3.24306333,-1.53837619,-.49893282],[-.96896309,1.87542451,.04154303],[.05568392,-.20417438,1.05799454]];n.RGB=t,n.RGBA=r,n.HEX=2,n.HEXA=e,n.mix=o,n.palette=function(n,t,r,e){let c=[];for(let a=0;a<r;a++)c.push(o(n,t,a/(r-1),e));return c},n.glsl_color=function(n){let t=b(n);return[t[0]/255,t[1]/255,t[2]/255,t[3]>1?t[3]/255:t[3]]},n.glsl=function(){return"\n#ifndef SPECTRAL\n#define SPECTRAL\n\nconst int SPECTRAL_SIZE = 38;\nconst float SPECTRAL_GAMMA = 2.4;\nconst float SPECTRAL_EPSILON = 0.0001;\n\nfloat spectral_uncompand(float x) {\n return (x < 0.04045) ? x / 12.92 : pow((x + 0.055) / 1.055, SPECTRAL_GAMMA);\n}\n\nfloat spectral_compand(float x) {\n return (x < 0.0031308) ? x * 12.92 : 1.055 * pow(x, 1.0 / SPECTRAL_GAMMA) - 0.055;\n}\n\nvec3 spectral_srgb_to_linear(vec3 srgb) {\n return vec3(spectral_uncompand(srgb[0]), spectral_uncompand(srgb[1]), spectral_uncompand(srgb[2]));\n}\n\nvec3 spectral_linear_to_srgb(vec3 lrgb) {\n return clamp(vec3(spectral_compand(lrgb[0]), spectral_compand(lrgb[1]), spectral_compand(lrgb[2])), 0.0, 1.0);\n}\n\nvoid spectral_upsampling(vec3 lrgb, out float w, out float c, out float m, out float y, out float r, out float g, out float b) {\n w = min(lrgb.r, min(lrgb.g, lrgb.b));\n\n lrgb -= w;\n\n c = min(lrgb.g, lrgb.b);\n m = min(lrgb.r, lrgb.b);\n y = min(lrgb.r, lrgb.g);\n r = min(max(0., lrgb.r - lrgb.b), max(0., lrgb.r - lrgb.g));\n g = min(max(0., lrgb.g - lrgb.b), max(0., lrgb.g - lrgb.r));\n b = min(max(0., lrgb.b - lrgb.g), max(0., lrgb.b - lrgb.r));\n}\n\nvoid spectral_linear_to_reflectance(vec3 lrgb, inout float R[SPECTRAL_SIZE]) {\n float w, c, m, y, r, g, b;\n \n spectral_upsampling(lrgb, w, c, m, y, r, g, b);\n \n R[0] = max(SPECTRAL_EPSILON, w + c * 0.96853629 + m * 0.51567122 + y * 0.02055257 + r * 0.03147571 + g * 0.49108579 + b * 0.97901834);\n R[1] = max(SPECTRAL_EPSILON, w + c * 0.96855103 + m * 0.54015520 + y * 0.02059936 + r * 0.03146636 + g * 0.46944057 + b * 0.97901649);\n R[2] = max(SPECTRAL_EPSILON, w + c * 0.96859338 + m * 0.62645502 + y * 0.02062723 + r * 0.03140624 + g * 0.40165780 + b * 0.97901118);\n R[3] = max(SPECTRAL_EPSILON, w + c * 0.96877345 + m * 0.75595012 + y * 0.02073387 + r * 0.03119611 + g * 0.24490420 + b * 0.97892146);\n R[4] = max(SPECTRAL_EPSILON, w + c * 0.96942204 + m * 0.92826996 + y * 0.02114202 + r * 0.03053888 + g * 0.06826880 + b * 0.97858555);\n R[5] = max(SPECTRAL_EPSILON, w + c * 0.97143709 + m * 0.97223624 + y * 0.02233154 + r * 0.02856855 + g * 0.02732883 + b * 0.97743705);\n R[6] = max(SPECTRAL_EPSILON, w + c * 0.97541862 + m * 0.98616174 + y * 0.02556857 + r * 0.02459485 + g * 0.01360600 + b * 0.97428075);\n R[7] = max(SPECTRAL_EPSILON, w + c * 0.98074186 + m * 0.98955255 + y * 0.03330189 + r * 0.01929520 + g * 0.01000187 + b * 0.96663223);\n R[8] = max(SPECTRAL_EPSILON, w + c * 0.98580992 + m * 0.98676237 + y * 0.05185294 + r * 0.01423112 + g * 0.01284127 + b * 0.94822893);\n R[9] = max(SPECTRAL_EPSILON, w + c * 0.98971194 + m * 0.97312575 + y * 0.10087639 + r * 0.01033111 + g * 0.02636635 + b * 0.89937713);\n R[10] = max(SPECTRAL_EPSILON, w + c * 0.99238027 + m * 0.91944277 + y * 0.24000413 + r * 0.00765876 + g * 0.07058713 + b * 0.76070164);\n R[11] = max(SPECTRAL_EPSILON, w + c * 0.99409844 + m * 0.32564851 + y * 0.53589066 + r * 0.00593693 + g * 0.70421692 + b * 0.46420440);\n R[12] = max(SPECTRAL_EPSILON, w + c * 0.99517200 + m * 0.13820628 + y * 0.79874659 + r * 0.00485616 + g * 0.85473994 + b * 0.20123039);\n R[13] = max(SPECTRAL_EPSILON, w + c * 0.99576545 + m * 0.05015143 + y * 0.91186529 + r * 0.00426186 + g * 0.95081565 + b * 0.08808402);\n R[14] = max(SPECTRAL_EPSILON, w + c * 0.99593552 + m * 0.02912336 + y * 0.95399623 + r * 0.00409039 + g * 0.97170370 + b * 0.04592894);\n R[15] = max(SPECTRAL_EPSILON, w + c * 0.99564041 + m * 0.02421691 + y * 0.97137099 + r * 0.00438375 + g * 0.97651888 + b * 0.02860373);\n R[16] = max(SPECTRAL_EPSILON, w + c * 0.99464769 + m * 0.02660696 + y * 0.97939505 + r * 0.00537525 + g * 0.97429245 + b * 0.02060067);\n R[17] = max(SPECTRAL_EPSILON, w + c * 0.99229579 + m * 0.03407586 + y * 0.98345207 + r * 0.00772962 + g * 0.97012917 + b * 0.01656701);\n R[18] = max(SPECTRAL_EPSILON, w + c * 0.98638762 + m * 0.04835936 + y * 0.98553736 + r * 0.01366120 + g * 0.94258630 + b * 0.01451549);\n R[19] = max(SPECTRAL_EPSILON, w + c * 0.96829712 + m * 0.00011720 + y * 0.98648905 + r * 0.03181352 + g * 0.99989207 + b * 0.01357964);\n R[20] = max(SPECTRAL_EPSILON, w + c * 0.89228016 + m * 0.00008554 + y * 0.98674535 + r * 0.10791525 + g * 0.99989891 + b * 0.01331243);\n R[21] = max(SPECTRAL_EPSILON, w + c * 0.53740239 + m * 0.85267882 + y * 0.98657555 + r * 0.46249516 + g * 0.13823139 + b * 0.01347661);\n R[22] = max(SPECTRAL_EPSILON, w + c * 0.15360445 + m * 0.93188793 + y * 0.98611877 + r * 0.84604333 + g * 0.06968113 + b * 0.01387181);\n R[23] = max(SPECTRAL_EPSILON, w + c * 0.05705719 + m * 0.94810268 + y * 0.98559942 + r * 0.94275572 + g * 0.05628787 + b * 0.01435472);\n R[24] = max(SPECTRAL_EPSILON, w + c * 0.03126539 + m * 0.94200977 + y * 0.98507063 + r * 0.96860996 + g * 0.06111561 + b * 0.01479836);\n R[25] = max(SPECTRAL_EPSILON, w + c * 0.02205445 + m * 0.91478045 + y * 0.98460039 + r * 0.97783966 + g * 0.08987709 + b * 0.01515250);\n R[26] = max(SPECTRAL_EPSILON, w + c * 0.01802271 + m * 0.87065445 + y * 0.98425301 + r * 0.98187757 + g * 0.13656016 + b * 0.01540513);\n R[27] = max(SPECTRAL_EPSILON, w + c * 0.01613460 + m * 0.78827548 + y * 0.98403909 + r * 0.98377315 + g * 0.22169624 + b * 0.01557233);\n R[28] = max(SPECTRAL_EPSILON, w + c * 0.01520947 + m * 0.65738359 + y * 0.98388535 + r * 0.98470202 + g * 0.32176956 + b * 0.01565710);\n R[29] = max(SPECTRAL_EPSILON, w + c * 0.01475977 + m * 0.59909403 + y * 0.98376116 + r * 0.98515481 + g * 0.36157329 + b * 0.01571025);\n R[30] = max(SPECTRAL_EPSILON, w + c * 0.01454263 + m * 0.56817268 + y * 0.98368246 + r * 0.98537114 + g * 0.48361920 + b * 0.01571916);\n R[31] = max(SPECTRAL_EPSILON, w + c * 0.01444459 + m * 0.54031997 + y * 0.98365023 + r * 0.98546685 + g * 0.46488579 + b * 0.01572133);\n R[32] = max(SPECTRAL_EPSILON, w + c * 0.01439897 + m * 0.52110241 + y * 0.98361309 + r * 0.98550011 + g * 0.47440306 + b * 0.01572502);\n R[33] = max(SPECTRAL_EPSILON, w + c * 0.01437620 + m * 0.51041094 + y * 0.98357259 + r * 0.98551031 + g * 0.48576990 + b * 0.01571717);\n R[34] = max(SPECTRAL_EPSILON, w + c * 0.01436343 + m * 0.50526577 + y * 0.98353856 + r * 0.98550741 + g * 0.49267971 + b * 0.01571905);\n R[35] = max(SPECTRAL_EPSILON, w + c * 0.01435687 + m * 0.50255080 + y * 0.98351247 + r * 0.98551323 + g * 0.49625685 + b * 0.01571059);\n R[36] = max(SPECTRAL_EPSILON, w + c * 0.01435370 + m * 0.50126452 + y * 0.98350101 + r * 0.98551563 + g * 0.49807754 + b * 0.01569728);\n R[37] = max(SPECTRAL_EPSILON, w + c * 0.01435408 + m * 0.50083021 + y * 0.98350852 + r * 0.98551547 + g * 0.49889859 + b * 0.01570020);\n}\n\nvec3 spectral_xyz_to_srgb(vec3 xyz) {\n mat3 XYZ_RGB;\n\n XYZ_RGB[0] = vec3( 3.24306333, -1.53837619, -0.49893282);\n XYZ_RGB[1] = vec3(-0.96896309, 1.87542451, 0.04154303);\n XYZ_RGB[2] = vec3( 0.05568392, -0.20417438, 1.05799454);\n \n float r = dot(XYZ_RGB[0], xyz);\n float g = dot(XYZ_RGB[1], xyz);\n float b = dot(XYZ_RGB[2], xyz);\n\n return spectral_linear_to_srgb(vec3(r, g, b));\n}\n\nvec3 spectral_reflectance_to_xyz(float R[SPECTRAL_SIZE]) {\n vec3 xyz = vec3(0.0);\n \n xyz += R[0] * vec3(0.00006469, 0.00000184, 0.00030502);\n xyz += R[1] * vec3(0.00021941, 0.00000621, 0.00103681);\n xyz += R[2] * vec3(0.00112057, 0.00003101, 0.00531314);\n xyz += R[3] * vec3(0.00376661, 0.00010475, 0.01795439);\n xyz += R[4] * vec3(0.01188055, 0.00035364, 0.05707758);\n xyz += R[5] * vec3(0.02328644, 0.00095147, 0.11365162);\n xyz += R[6] * vec3(0.03455942, 0.00228226, 0.17335873);\n xyz += R[7] * vec3(0.03722379, 0.00420733, 0.19620658);\n xyz += R[8] * vec3(0.03241838, 0.00668880, 0.18608237);\n xyz += R[9] * vec3(0.02123321, 0.00988840, 0.13995048);\n xyz += R[10] * vec3(0.01049099, 0.01524945, 0.08917453);\n xyz += R[11] * vec3(0.00329584, 0.02141831, 0.04789621);\n xyz += R[12] * vec3(0.00050704, 0.03342293, 0.02814563);\n xyz += R[13] * vec3(0.00094867, 0.05131001, 0.01613766);\n xyz += R[14] * vec3(0.00627372, 0.07040208, 0.00775910);\n xyz += R[15] * vec3(0.01686462, 0.08783871, 0.00429615);\n xyz += R[16] * vec3(0.02868965, 0.09424905, 0.00200551);\n xyz += R[17] * vec3(0.04267481, 0.09795667, 0.00086147);\n xyz += R[18] * vec3(0.05625475, 0.09415219, 0.00036904);\n xyz += R[19] * vec3(0.06947040, 0.08678102, 0.00019143);\n xyz += R[20] * vec3(0.08305315, 0.07885653, 0.00014956);\n xyz += R[21] * vec3(0.08612610, 0.06352670, 0.00009231);\n xyz += R[22] * vec3(0.09046614, 0.05374142, 0.00006813);\n xyz += R[23] * vec3(0.08500387, 0.04264606, 0.00002883);\n xyz += R[24] * vec3(0.07090667, 0.03161735, 0.00001577);\n xyz += R[25] * vec3(0.05062889, 0.02088521, 0.00000394);\n xyz += R[26] * vec3(0.03547396, 0.01386011, 0.00000158);\n xyz += R[27] * vec3(0.02146821, 0.00810264, 0.00000000);\n xyz += R[28] * vec3(0.01251646, 0.00463010, 0.00000000);\n xyz += R[29] * vec3(0.00680458, 0.00249138, 0.00000000);\n xyz += R[30] * vec3(0.00346457, 0.00125930, 0.00000000);\n xyz += R[31] * vec3(0.00149761, 0.00054165, 0.00000000);\n xyz += R[32] * vec3(0.00076970, 0.00027795, 0.00000000);\n xyz += R[33] * vec3(0.00040737, 0.00014711, 0.00000000);\n xyz += R[34] * vec3(0.00016901, 0.00006103, 0.00000000);\n xyz += R[35] * vec3(0.00009522, 0.00003439, 0.00000000);\n xyz += R[36] * vec3(0.00004903, 0.00001771, 0.00000000);\n xyz += R[37] * vec3(0.00002000, 0.00000722, 0.00000000);\n\n return xyz;\n}\n\nfloat spectral_linear_to_concentration(float l1, float l2, float t) {\n float t1 = l1 * pow(1.0 - t, 2.0);\n float t2 = l2 * pow(t, 2.0);\n\n return t2 / (t1 + t2);\n}\n\nvec3 spectral_mix(vec3 color1, vec3 color2, float t) {\n vec3 lrgb1 = spectral_srgb_to_linear(color1);\n vec3 lrgb2 = spectral_srgb_to_linear(color2);\n\n float R1[SPECTRAL_SIZE];\n float R2[SPECTRAL_SIZE];\n\n spectral_linear_to_reflectance(lrgb1, R1);\n spectral_linear_to_reflectance(lrgb2, R2);\n\n float l1 = spectral_reflectance_to_xyz(R1)[1];\n float l2 = spectral_reflectance_to_xyz(R2)[1];\n\n t = spectral_linear_to_concentration(l1, l2, t);\n\n float R[SPECTRAL_SIZE];\n\n for (int i = 0; i < SPECTRAL_SIZE; i++) {\n float KS = (1.0 - t) * (pow(1.0 - R1[i], 2.0) / (2.0 * R1[i])) + t * (pow(1.0 - R2[i], 2.0) / (2.0 * R2[i]));\n float KM = 1.0 + KS - sqrt(pow(KS, 2.0) + 2.0 * KS);\n\n //Saunderson correction\n // let S = ((1.0 - K1) * (1.0 - K2) * KM) / (1.0 - K2 * KM);\n\n R[i] = KM;\n }\n\n return spectral_xyz_to_srgb(spectral_reflectance_to_xyz(R));\n}\n\nvec4 spectral_mix(vec4 color1, vec4 color2, float t) {\n return vec4(spectral_mix(color1.rgb, color2.rgb, t), mix(color1.a, color2.a, t));\n}\n\n#endif\n "}},"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((n=n||self).spectral={});\ No newline at end of file