Unlock custom brushes, natural fill effects and intuitive hatching in p5.js
Is it finished?
Alejandro060761c328cd
diff --git a/examples.js b/examples.jsindex 63060db..45a02d7 100644--- a/examples.js+++ b/examples.js@@ -87,7 +87,7 @@ function setup () {// USE BRUSHBOX LIBRARY// Load BrushBox: load(BUFFER) -> Leave empty if you want to draw to the main p5 canvas- brush.load();+ //brush.load();translate(-width/2,-height/2)@@ -188,10 +188,13 @@ function setup () {brush.noField()brush.beginShape(0.7)brush.vertex(20,20,1)+ brush.vertex(40,40,0.8)brush.vertex(200,25,0.8)- brush.vertex(30,100,1.2)- brush.vertex(200,200,1)- brush.endShape(CLOSE)+ brush.vertex(100,90,0.8)+ brush.vertex(230,100,1.2)+ brush.vertex(30,200,1)+ brush.vertex(70,100,1)+ brush.endShape()brush.stroke("#002185")brush.pick("marker")@@ -209,12 +212,13 @@ function setup () {//polygon.draw()brush.noField()brush.noStroke()-- brush.fill("#fcd300",60)- brush.bleed(0.1)++ brush.fill("#fcd300",100)+ brush.bleed(0.3)brush.circle(100,100,50)- brush.fill("#002185",60)+ brush.fill("#002185",80)brush.circle(150,100,50)+}function draw() {diff --git a/index.html b/index.htmlindex 3f39750..45e710a 100644--- a/index.html+++ b/index.html@@ -3,7 +3,6 @@<head><title>Brushbox - Examples</title><script src="./p5.min.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 768c44b..301da77 100644--- a/p5.brush.js+++ b/p5.brush.js@@ -3,6 +3,27 @@// License: MIT License// Author: Alejandro Campos (@acamposuribe)// ==========================================================+// MIT License+//+// Copyright (c) 2023 Alejandro Campos Uribe+//+// 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) :@@ -13,15 +34,15 @@//////////////////////////////////////////////////// CONFIG AND LOAD FUNCTIONS- let _r;+ let _r, isReady = false;function _config (objct = {}) {if (objct.R) R.source = objct.R // Seed RNG- if (objct.SVG) S.activate(); // Doesn't work at the moment}function _load (canvasID = false) {+ isReady = true;_r = (!canvasID) ? window.self : canvasID; // Set buffer- Mix.load(); // Load Color-Mix system- FF.create(); // Create flow_field if needed+ Mix.load();+ FF.create();globalScale(_r.width / 250) // Adjust standard brushes to match canvas}function _preload () {@@ -71,7 +92,7 @@}//////////////////////////////////////////////////- // COLOR-MIX FUNCTIONS (Using Spectral.js right now)+ // COLOR-MIX FUNCTIONS (Using Spectral.js)const Mix = {loaded: false,load() {@@ -79,7 +100,7 @@this.mask = _r.createFramebuffer({width: _r.width, height: _r.height, density: _r.pixelDensity()});this.mask.begin(), _r.clear(), this.mask.end();if (!Mix.loaded) {- this.frag = this.frag.replace('#include "spectral.glsl"', spectral.glsl());+ this.frag = this.frag.replace('#include "spectral.glsl"', glsl());}this.shader = _r.createShader(this.vert, this.frag);Mix.loaded = true;@@ -131,36 +152,17 @@}`}- //////////////////////////////////////////////////- // SVG mode -> Include p5.svg in the HTML file- // Not working at the moment !! Broken with p5 1.8- const S = {- on: false,- activate() {- this.final = createGraphics(_r.width,_r.height,SVG)- this.on = true;- },- b: [],- export(name) {- for (let g of this.b) {- let layerBuffer = createGraphics(_r.width, _r.height,SVG);- for (let b of g) {- layerBuffer.image(b,0,0)- }- this.final.image(layerBuffer,0,0)- }- this.final.save(name)- }- }//////////////////////////////////////////////////// FLOWFIELDfunction flowField (a) {+ if (!isReady) _load();FF.isActive = trueif (FF.current !== a) FF.current = a;}function disableField () {FF.isActive = false}function newField(name,funct) {+ if (!isReady) _load();FF.list.set(name,{gen: funct});FF.current = name;FF.refresh()@@ -336,7 +338,6 @@}if ((b.blend !== false) && (b.type === "marker" || b.type === "custom" || b.type === "image" )) { b.blend = true } else {b.blend = false}B.list.set(a,{param:b,colors:[],buffers:[]})- if (S.on) S.b.push(B.list.get(a).buffers)},c: "#000000", w: 1, cr: null, name: "HB",set(a,c,w) {B.name = a, B.c = c, B.w = w},@@ -346,15 +347,18 @@spacing() {B.p = B.list.get(B.name).param; return B.p.spacing * B.w},clip(a) {B.cr = a},line(x1,y1,x2,y2) {+ if (!isReady) _load();B.l = dist(x1,y1,x2,y2), B.position = new Position(x1,y1), B.flow = false, B.plot = false;B.draw(calcAngle(x1,y1,x2,y2),true)},flowLine(x,y,length,dir) {+ if (!isReady) _load();if (angleMode() === "radians") dir = dir * 180 / Math.PIB.l = length, B.position = new Position(x,y), B.flow = true, B.plot = false;B.draw(dir,false)},flowShape(p,x,y,scale) {+ if (!isReady) _load();B.l = p.length, B.position = new Position(x,y), B.plot = p, p.calcIndex(0);B.push();let st = B.p.spacing * B.w@@ -363,12 +367,6 @@}B.pop();},- beginShape() {B.vertices = []},- vertex(x,y) {B.vertices.push([x,y])},- endShape(bool = false) {- if (bool) B.vertices.push(B.vertices[0])- for (let i = 0; i < B.vertices.length - 1; i++) {B.line(B.vertices[i][0],B.vertices[i][1],B.vertices[i+1][0],B.vertices[i+1][1])}- },draw(dir,bool) {B.dir = dir;B.push()@@ -383,13 +381,11 @@if (B.p.pressure.type !== "custom") {B.a = R.random(-1,1), B.b = R.random(1,1.5), B.cp = R.random(3,3.5);}else {B.cp = R.random(-0.2,0.2)}B.min = B.p.pressure.min_max[0], B.max = B.p.pressure.min_max[1];- if (S.on) B.pickBuffer(), B.buffer.beginShape();if (B.p.blend) trans(), Mix.mask.begin();_r.push(), B.c = _r.color(B.c), _r.noStroke();if (B.p.blend) _r.translate(matrix[0],matrix[1]), B.markerTip();},tip() {- if (S.on) B.buffer.vertex(B.position.x,B.position.y) // SVGlet pressure = B.plot ? B.simPressure() * B.plot.pressure(B.position.plotted) : B.simPressure(); // Pressurelet alpha = Math.floor(B.p.opacity * Math.pow(pressure,B.p.type === "marker" ? 1 : 1.5)); // Alphaif (B.p.blend) {_r.fill(255,0,0,alpha/2)} else {B.c.setAlpha(alpha), _r.fill(B.c)}; // Color@@ -434,7 +430,6 @@if (B.p.blend) B.markerTip();_r.pop();if (B.p.blend) Mix.mask.end(), Mix.blend(B.c);- if (S.on) B.buffer.endShape();},markerTip() {if (B.crop()) {@@ -458,20 +453,6 @@if (B.cr) return B.position.x >= B.cr[0] && B.position.x <= B.cr[2] && B.position.y >= B.cr[1] && B.position.y <= B.cr[3];else return true;},- pickBuffer() {- if (S.on) {- let c_list = B.list.get(B.name).colors, b_list = B.list.get(B.name).buffers;- if (c_list.includes(B.c)) {- B.buffer = b_list[c_list.indexOf(B.c)]- } else {- let new_b = createGraphics(width, height,SVG);- new_b.stroke(B.c), new_b.strokeWeight(B.p.weight), new_b.noFill();- c_list.push(B.c)- b_list.push(new_b)- B.buffer = b_list[c_list.indexOf(B.c)]- }- }- },gauss(a = 0.5 + B.p.pressure.curve[0] * B.a, b = 1 - B.p.pressure.curve[1] * B.b, c = B.cp, min = B.min, max = B.max) {return R.map((1 / ( 1 + Math.pow(Math.abs( ( B.position.plotted - a * B.l ) / ( b * B.l / 2 ) ), 2 * c))), 0, 1, min, max);},@@ -480,6 +461,7 @@else return this.gauss()},hatch(polygons, dist, angle, options = {rand: false, continuous: false, gradient: false}) {+ if (!isReady) _load();if (angleMode() === "radians") angle = angle * 180 / Math.PIangle = angle % 180;let dots = [];@@ -590,12 +572,14 @@return points;}draw () {+ if (!isReady) _load();if (B.isActive) {for (let s of this.sides) {B.line(s[0].x,s[0].y,s[1].x,s[1].y)}}}- fill () {F.fill(this)}+ fill () {if (!isReady) _load(); F.fill(this)}hatch (dist, angle, options) {+ if (!isReady) _load();B.hatch(this,dist,angle,options)}}@@ -620,12 +604,14 @@//////////////////////////////////////////////////// BASIC GEOMETRIESfunction _rect(x,y,w,h,mode = false) {+ if (!isReady) _load();if (mode) x = x - w / 2, y = y - h / 2;let p = new Polygon([[x,y],[x+w,y],[x+w,y+h],[x,y+h]])p.draw();p.fill();}function _circle(x,y,radius,r = false) {+ if (!isReady) _load();let p = new Plot("curve")let l = Math.PI * radius / 2;let angle = 0@@ -694,9 +680,16 @@while (d <= _d) {this.suma = d; d += this.segments[this.index+1]; this.index++;}}genPol (_x,_y) {+ if (!isReady) _load();+ let max = 0;+ let min = 9999;+ for (let o of this.segments) {+ max = Math.max(max,o) == 0 ? max : Math.max(max,o);+ min = Math.min(min,o) == 0 ? min : Math.min(min,o);+ }let _step = B.spacing() // get last spacinglet vertices = []- let side = this.length * 0.5 * F.b;+ let side = (max + min) / 1.5 * F.b;let linepoint = new Position(_x,_y);let numsteps = Math.round(this.length/_step);for (let steps = 0; steps < numsteps; steps++) {@@ -707,12 +700,14 @@return new Polygon(vertices);}draw (x,y,scale) {+ if (!isReady) _load();if (this.origin) x = this.origin[0], y = this.origin[1], scale = 1;B.flowShape(this,x,y,scale)}}let strokeArray = false, strokeOption;function _beginShape(curvature) {+ if (!isReady) _load();strokeOption = curvature;strokeArray = [];}@@ -722,13 +717,12 @@function _endShape(a) {if (a === CLOSE) strokeArray.push(strokeArray[0])let sp = new Spline(strokeArray, strokeOption)- if (F.isActive) {- let pol = sp.p.genPol(p.origin[0],p.origin[1])- }+ if (F.isActive) {sp.p.genPol(sp.origin[0],sp.origin[1]).fill()}if (B.isActive) sp.draw()strokeArray = false;}function _beginStroke(type,x,y) {+ if (!isReady) _load();strokeOption = [x,y];strokeArray = new Plot(type)}@@ -793,6 +787,7 @@}function _spline(array_points, curvature = 0.5) {+ if (!isReady) _load();let p = new Spline(array_points, curvature = 0.5)p.draw()}@@ -833,8 +828,8 @@calcCenter () {let midx = 0, midy = 0;for(let i = 0; i < this.v.length; ++i) {- midx += this.v[i].x;- midy += this.v[i].y;+ midx += this.v[i].x;+ midy += this.v[i].y;}midx /= this.v.length, midy /= this.v.length;return createVector(midx,midy)@@ -853,32 +848,32 @@var vertixlength = this.v.length;if (_a >= 0.2) {vertixlength = int(_a * this.v.length);}for (let i = 0; i < vertixlength; i ++) {- const j = (i + 1) % vertixlength;- const v1 = this.v[i];- const v2 = this.v[j];- let mod = (_a == 0.1) ? 0.75 : this.m[i];- if (degrow) mod = -0.5;- const chmod = m => {return m + (randomGaussian(0.5,0.1) - 0.5) * 0.1;}- newVerts.push(v1);- newMods.push(chmod(mod));- const segment = p5.Vector.sub(v2, v1);- const len = segment.mag();- segment.mult(constrain(randomGaussian(0.5,0.2),0.1,0.9));- const v = p5.Vector.add(segment, v1);- segment.rotate(-90 + (randomGaussian(0,0.4)) * 45);- segment.setMag(randomGaussian(0.5,0.2) * R.random(0.6,1.4) * len * mod);- v.add(segment);- newVerts.push(v);- newMods.push(chmod(mod));+ const j = (i + 1) % vertixlength;+ const v1 = this.v[i];+ const v2 = this.v[j];+ let mod = (_a == 0.1) ? 0.75 : this.m[i];+ if (degrow) mod = -0.5;+ const chmod = m => {return m + (randomGaussian(0.5,0.1) - 0.5) * 0.1;}+ newVerts.push(v1);+ newMods.push(chmod(mod));+ const segment = p5.Vector.sub(v2, v1);+ const len = segment.mag();+ segment.mult(constrain(randomGaussian(0.5,0.2),0.1,0.9));+ const v = p5.Vector.add(segment, v1);+ segment.rotate(-90 + (randomGaussian(0,0.4)) * 45);+ segment.setMag(randomGaussian(0.5,0.2) * R.random(0.6,1.4) * len * mod);+ v.add(segment);+ newVerts.push(v);+ newMods.push(chmod(mod));}return new fillPol (newVerts, newMods, this.midP);}fill (color, intensity) {let pol = this.grow()- let pol4 = this.grow()+ let pol4 = this.grow(0.6)let pol2 = pol.grow().grow(0.5);let pol3 = pol2.grow(0.4);- let numLayers = 30+ let numLayers = 24trans();Mix.mask.begin();push();@@ -886,15 +881,15 @@for (let i = 0; i < numLayers; i ++) {if (i == int(numLayers/4) || i == int(2*numLayers/4) || i == int(3*numLayers/4)) {pol = pol.grow();- pol4 = pol4.grow()+ pol4 = pol4.grow(0.1)pol2 = pol2.grow(0.1);pol3 = pol3.grow(0.1);}pol.grow().layer(i, intensity / 2);- pol4.grow(0.1,true).layer(i, 2);- pol2.grow(0.1).layer(i,int(map(i,0,numLayers/2,intensity / 1.5,0)));- pol3.grow(0.1).layer(i,int(map(i,0,2*numLayers/3,intensity / 1.5,0)));- pol.grow().grow().grow().erase(intensity); // Texture light+ pol4.grow(0.1,true).grow().layer(i, 2);+ pol2.grow(0.1).layer(i, int( map(i, 0, numLayers / 2, intensity, 0) ));+ pol3.grow(0.1).layer(i, int( map(i, 0, 2 * numLayers / 3, intensity, 0) ));+ pol.grow().grow().grow().erase(intensity);}pop();Mix.mask.end();@@ -902,16 +897,16 @@}layer (_nr,_alpha) {fill(255, 0, 0, _alpha)- stroke(255, 0, 0, int(0.3*_alpha))- strokeWeight(map(_nr, 0, 25, 1, 3))- noStroke()+ stroke(255, 0, 0, 1)+ strokeWeight(map(_nr, 0, 24, 3, 0))beginShape();for(let v of this.v) {vertex(v.x, v.y);}endShape(CLOSE);+ noStroke()}erase (_i) {- erase(2)- for(let i = 0; i < R.random(80,150); i++) {+ erase(3)+ for(let i = 0; i < R.random(80,120); i++) {circle(this.midP.x + randomGaussian(0,this.size / 2), this.midP.y + randomGaussian(0,this.size / 2), R.random(0.030,0.20) * this.size);}noErase()@@ -928,8 +923,8 @@this._cachedStrokeStyle = this.curStrokeColor.slice();this.curStrokeColor = [1, 1, 1, opacityStroke / 255];}- }- p5.RendererGL.prototype.noErase = function() {+ }+ p5.RendererGL.prototype.noErase = function() {if (this._isErasing) {this.curFillColor = this._cachedFillStyle.slice();this.curStrokeColor = this._cachedStrokeStyle.slice();@@ -939,7 +934,7 @@this._isErasing = false;this._applyBlendMode();}- }+ }//////////////////////////////////////////////////// STANDARD BRUSHES@@ -1014,8 +1009,6 @@exports.move = _moveexports.endStroke = _endStroke--// Hatchesexports.Polygon = Polygon;exports.hatch = B.hatch;@@ -1023,5 +1016,215 @@// Classesexports.Plot = Plot;exports.Pos = Position;++// Shader From Spectral.js: https://github.com/rvanwijnen/spectral.js+// 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 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+ `;+}})));\ No newline at end of filediff --git a/spectral.min.js b/spectral.min.jsdeleted file mode 100644index ec37aed..0000000--- a/spectral.min.js+++ /dev/null@@ -1 +0,0 @@-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