Skip to content

Unlock custom brushes, natural fill effects and intuitive hatching in p5.js

Things to get ready

Alejandroa6838de7f797
diff --git a/brush.jpg b/brush_tips/brush.jpg
similarity index 100%
rename from brush.jpg
rename to brush_tips/brush.jpg
diff --git a/brush2.jpg b/brush_tips/brush2.jpg
similarity index 100%
rename from brush2.jpg
rename to brush_tips/brush2.jpg
diff --git a/examples.js b/examples.js
index bb69f23..ed4cec0 100644
--- a/examples.js
+++ b/examples.js
@@ -33,22 +33,23 @@ let palette = ["#7b4800", "#002185", "#003c32", "#fcd300", "#ff2702", "#6b9404"]
//////////////////////////////////////////////////
-// BRUSHBOX
+// p5.brush
// YOU CAN SEED THE BRUSHBOX RANDOM NUMBER GENERATOR for determinism
-brushbox.config({
+brush.config({
// R: function () { return YOUR_RNG() },
})
// YOU CAN CREATE YOU OWN BRUSHES
-brushbox.newBrush("watercolor", {
+brush.newBrush("watercolor", {
type: "image", // this is the TIP TYPE: choose standard / spray / marker / custom / image
- weight: 17, // Base weight of the brush tip
+ weight: 17, // Base weight of the brush tip
vibration: 2, // Vibration of the lines, spread
definition: 0.5, // Between 0 and 1
quality: 8, // + quality = more continuous line
opacity: 10, // Base opacity of the brush (this will be affected by pressure)
spacing: 1, // Spacing between the points that compose the brush stroke
+ blend: true, // Activate / Disable realistic color mixing. By default, this is active for marker-custom-image brushes
pressure: {
type: "custom", // "standard" or "custom". Use "custom"" for custom pressure curves. Use standard for simple gauss bell curve
//curve: [0.15,0.2], // If "standard", pick a and b values for the gauss curve.
@@ -61,7 +62,7 @@ brushbox.newBrush("watercolor", {
},
// if you select the image type brush, link your image below. If not, you can remove these lines.
image: {
- src: "./brush.jpg",
+ src: "./brush_tips/brush.jpg",
},
// For "custom" and "image" types, you can define the tip angle rotation here.
rotate: "natural", // "none" disables rotation | "natural" follows the direction of the stroke | "random"
@@ -72,7 +73,7 @@ brushbox.newBrush("watercolor", {
function preload() {
// If you are going to use custom image brush tips, include this in preload!
- brushbox.preload();
+ brush.preload();
}
function setup () {
@@ -84,63 +85,65 @@ function setup () {
// USE BRUSHBOX LIBRARY
// Load BrushBox: load(BUFFER) -> Leave empty if you want to draw to the main p5 canvas
- brushbox.load();
+ brush.load();
// Set your brush name, color, and weight. There are 9 standard brushes to select from.
// Select colors with HEX codes, arrays of [r,g,b] or names
- brushbox.set("marker","black",1)
+ brush.set("marker","black",1)
// Translate(x,y) - here I'm translating, so to draw like a 2D canvas
- brushbox.translate(-width/2,-height/2)
+ brush.translate(-width/2,-height/2)
// Draw a line (x1,y1,x2,y2)
- brushbox.line(20,20,200,20)
+ brush.line(20,20,200,20)
// Change the color or weight scale. Use hex color codes
- brushbox.color("#002185")
- brushbox.strokeWeight(1.2)
+ brush.color("#002185")
+ brush.strokeWeight(1.2)
// You can also draw flowlines (x1,y1,distance,angle)
// If you want to draw flowLines, you need to select one of the provided flow_fields.
- brushbox.flowField("seabed");
- brushbox.flowLine(20,40,180,0)
+ brush.flowField("seabed");
+ brush.flowLine(20,40,180,0)
// You can also clip all following lines with a rectangle (x1,y1,x2,y2)
- brushbox.clip([30,10,100,100]);
- brushbox.flowLine(20,60,100,0)
+ brush.clip([30,10,100,100]);
+ brush.flowLine(20,60,100,0)
// And remove the clip
- brushbox.clip(false);
+ brush.clip(false);
// You can set a different brush
- brushbox.brush("marker")
+ brush.brush("marker")
// You can create and draw polygons
- brushbox.strokeWeight(1)
- let polygon = new brushbox.Polygon([
+ brush.strokeWeight(1)
+ let polygon = new brush.Polygon([
[20,80],
[100,80],
[100,120],
[30,140],
]);
//polygon.draw();
- brushbox.set("rotring","#9c2128",1)
+ brush.set("rotring","#9c2128",1)
// You can also atch these polygons .hath(distance_between_lines, angle_of_lines, 0-1.0 for % precission)
//polygon.hatch(2,45,0.05)
- // You can also hatch arrays of polygons brushbox.hatch(ARRAY WITH POLS, distance_between_lines, angle_of_lines, 0-1.0 for % precission)
- brushbox.color("#080f15")
+ // You can also hatch arrays of polygons brush.hatch(ARRAY WITH POLS, distance_between_lines, angle_of_lines, 0-1.0 for % precission)
+ brush.color("#080f15")
let pol_array = []
for (let i = 0; i < 5; i++) {
- pol_array.push(new brushbox.Polygon([
+ pol_array.push(new brush.Polygon([
[random(width),random(height)],
[random(width),random(height)],
[random(width),random(height)],
[random(width),random(height)]
]));
}
- brushbox.hatch(pol_array,3,0)
+ brush.hatch(pol_array,3,0)
// You can draw rectangles (x,y,width,height,mode) - mode is optional, you can set it to "center"
- brushbox.set("charcoal","#6b9404",1)
- //brushbox.rect(100,100,100,100,"center")
+ brush.set("charcoal","#6b9404",1)
+ //brush.rect(100,100,100,100,"center")
// You can draw ricles (x,y,radius)
- brushbox.color("#7b4800")
- brushbox.circle(100,100,50)
+ brush.color("#002185")
+ brush.circle(100,100,50)
+ brush.color("#fcd300")
+ brush.circle(102,100,50)
// You can create curves/plots (here I'm creating 4 spirals). This offers full control of the brush pressure, though it's a tad more complicated
- brushbox.brush("marker2")
- brushbox.flowField("seabed")
+ brush.brush("watercolor")
+ brush.flowField("seabed")
for (let j = 0; j < 4; j++) {
- let plot = new brushbox.Plot("curve")
+ let plot = new brush.Plot("curve")
for (let i = 0; i < Math.floor(random(40,90)); i++) {
plot.addSegment(0,1+i*4,random(1.2))
plot.addSegment(90,2+i*4,random(1.2))
@@ -149,36 +152,37 @@ function setup () {
}
plot.endPlot(0,1)
plot.rotate(Math.floor(random(0,180)))
- brushbox.color(random(palette))
- //brushbox.flowShape(plot,width*random(0.3,0.7),height*random(0.1,0.9),random(0.5,1))
+ brush.color(random(palette))
+ brush.flowShape(plot,width*random(0.3,0.7),height*random(0.1,0.9),random(0.5,1))
}
- brushbox.disableField()
- brushbox.brush("HB")
+ brush.disableField()
+ brush.brush("HB")
// A different way of creating curves, by an array of points [x, y, brush pressure at that point]
let points = []
for (let i = 0; i < 55; i++) {
points.push([random(width),random(height),random(0.7,1.2)])
}
- // brushbox.spline(array_points, curvature) curvature is a value 0-1. Use 0 for a truncated line
+ // brush.spline(array_points, curvature) curvature is a value 0-1. Use 0 for a truncated line
// The curve will go from the first point to the last one, using the rest as control points
- //brushbox.spline(points,1)
+ //brush.spline(points,1)
- brushbox.flowField("seabed")
+ brush.flowField("seabed")
}
function draw() {
- brushbox.set(random(brushbox.brushes()),random(palette),random(1,1.5))
- //brushbox.flowLine(width*random(0,1),height*random(0,1),width*random(0.5,0.8),random(0,360))
+ //brush.set(random(brush.brushes()),random(palette),random(1,1.5))
+ //brush.flowLine(width*random(0,1),height*random(0,1),width*random(0.5,0.8),random(0,360))
-
- let polygon = new brushbox.Polygon([
+ /*
+ let polygon = new brush.Polygon([
[random(width),random(height)],
[random(width),random(height)],
[random(width),random(height)],
[random(width),random(height)]
])
- brushbox.hatch(polygon,random(1,5),random(0,180))
+ brush.hatch(polygon,random(1,5),random(0,180))
+ */
}
diff --git a/index.html b/index.html
index 7949757..3f39750 100644
--- a/index.html
+++ b/index.html
@@ -3,8 +3,8 @@
<head>
<title>Brushbox - Examples</title>
<script src="./p5.min.js"></script>
- <script src="./mixbox.js"></script>
- <script src="./brushbox.js"></script>
+ <script src="./spectral.min.js"></script>
+ <script src="./p5.brush.js"></script>
<link rel="stylesheet" href="./style.css">
</head>
<body>
diff --git a/brushbox.js b/p5.brush.js
similarity index 93%
rename from brushbox.js
rename to p5.brush.js
index 45c6ba1..9550831 100644
--- a/brushbox.js
+++ b/p5.brush.js
@@ -1,5 +1,5 @@
// ==========================================================
-// BRUSHBOX 1.0 (c) 2023.
+// p5.brush 1.0 (c) 2023.
// License: MIT License
// Author: Alejandro Campos (@acamposuribe)
// ==========================================================
@@ -7,7 +7,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
- (global = global || self, factory(global.brushbox = {}));
+ (global = global || self, factory(global.brush = {}));
} (this, (function (exports) {
'use strict';
@@ -20,7 +20,7 @@
}
p5.prototype.registerMethod('beforePreload', config);
function load (canvasID = false,field = false) {
- _r = (!canvasID) ? window : canvasID; // Set buffer
+ _r = (!canvasID) ? window.self : canvasID; // Set buffer
Mix.load(); // Load Color-Mix system
if (field) FF.create(field); // Create flow_field if needed
}
@@ -72,7 +72,7 @@
}
//////////////////////////////////////////////////
- // COLOR-MIX FUNCTIONS (Using Mixbox right now)
+ // COLOR-MIX FUNCTIONS (Using Spectral.js right now)
const Mix = {
loaded: false,
load() {
@@ -80,25 +80,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) {
- // Create texture for mixing
- if (!this.tex) {
- this.tex = createGraphics(512, 512);
- this.tex.pixelDensity(1);
- this.tex.loadPixels();
- for (var b = 0; b < 64; b++)
- for (var g = 0; g < 64; g++)
- for (var r = 0; r < 64; r++) {
- var x = (b % 8) * 64 + r;
- var y = ((b / 8) | 0) * 64 + g;
- var xyz = r + g * 64 + b * 64 * 64;
- this.tex.pixels[(x + y * 512) * 4 + 0] = mixbox.lut[xyz + 192];
- this.tex.pixels[(x + y * 512) * 4 + 1] = mixbox.lut[xyz + 262336];
- this.tex.pixels[(x + y * 512) * 4 + 2] = mixbox.lut[xyz + 524480];
- this.tex.pixels[(x + y * 512) * 4 + 3] = 255;
- }
- this.tex.updatePixels();
- }
- this.frag = this.frag.replace('#include "mixbox.glsl"', mixbox.glsl());
+ this.frag = this.frag.replace('#include "spectral.glsl"', spectral.glsl());
}
this.shader = _r.createShader(this.vert, this.frag);
Mix.loaded = true;
@@ -110,7 +92,6 @@
// Load shader and send data from canvas
_r.shader(this.shader);
this.shader.setUniform('addColor', this.pigment);
- this.shader.setUniform('mixbox_lut', this.tex._renderer);
this.shader.setUniform('source', _r._renderer);
this.shader.setUniform('mask', this.mask);
// Give geometry to shader
@@ -126,9 +107,8 @@
precision highp float;varying vec2 vVertTexCoord;
uniform sampler2D source;
uniform sampler2D mask;
- uniform sampler2D mixbox_lut;
uniform vec4 addColor;
- #include "mixbox.glsl"
+ #include "spectral.glsl"
vec3 rgb(float r, float g, float b){return vec3(r / 255.0, g / 255.0, b / 255.0);}
float rand(vec2 co, float a, float b, float c){return fract(sin(dot(co, vec2(a, b))) * c);}
@@ -142,7 +122,7 @@
float r3 = map(rand(vVertTexCoord,17.9898,3.233,33358.5453),0.0,1.0,-1.0,1.0);
float d; if (maskColor.r == 1.0) {d = maskColor.b * 0.2;} else {d = 0.0;}
vec4 canvasColor = texture2D(source, vVertTexCoord);
- vec3 mixedColor = mixbox_lerp(vec3(canvasColor.r,canvasColor.g,canvasColor.b), rgb(addColor.r,addColor.g,addColor.b), maskColor.a);
+ vec3 mixedColor = spectral_mix(vec3(canvasColor.r,canvasColor.g,canvasColor.b), rgb(addColor.r,addColor.g,addColor.b), maskColor.a * 0.9);
gl_FragColor = vec4(mixedColor.r + 0.03 * r1 - d,mixedColor.g + 0.03 * r2 - d,mixedColor.b + 0.03 * r3 - d,1.0);
}
else {
@@ -351,6 +331,7 @@
T.add(b.image.src);
b.tip = function() {_r.image(T.tips.get(B.p.image.src),-B.p.weight/2,-B.p.weight/2,B.p.weight,B.p.weight)}
}
+ 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)
},
@@ -395,20 +376,19 @@
},
push() {
B.p = B.list.get(B.name).param
- B.mode = (B.p.type === "marker" || B.p.type === "custom" || B.p.type === "image") ? "blend" : "normal";
if (B.p.pressure.type !== "custom") {B.a = R.random(-1,1), B.b = R.random(1,1.5), B.cp = R.random(3,4.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.mode === "blend") Mix.mask.begin();
+ if (B.p.blend) Mix.mask.begin();
_r.push(), _r.translate(trans[0],trans[1]), B.c = _r.color(B.c), _r.noStroke();
- if (B.mode === "blend") B.markerTip();
+ if (B.p.blend) B.markerTip();
},
tip() {
if (S.on) B.buffer.vertex(B.position.x,B.position.y) // SVG
let pressure = B.plot ? B.simPressure() * B.plot.pressure(B.position.plotted) : B.simPressure(); // Pressure
let alpha = Math.floor(B.p.opacity * Math.pow(pressure,B.p.type === "marker" ? 1 : 1.5)); // Alpha
- if (B.mode === "blend") {_r.fill(255,0,1,alpha)} else {B.c.setAlpha(alpha), _r.fill(B.c)}; // Color
+ if (B.p.blend) {_r.fill(255,0,1,alpha)} else {B.c.setAlpha(alpha), _r.fill(B.c)}; // Color
if (B.crop()) {
if (B.p.type === "spray") { // SPRAY TYPE BRUSHES
let vibration = (B.w * B.p.vibration * pressure) + B.w * randomGaussian() * B.p.vibration / 3;
@@ -438,16 +418,18 @@
},
adjust_tip(pressure, alpha, mode) {
_r.scale(pressure);
- if (B.p.type === "image") _r.tint(255,0,1,alpha);
+ if (B.p.type === "image") {
+ (B.p.blend) ? _r.tint(255,0,1,alpha) : _r.tint(_r.red(B.c),_r.green(B.c),_r.blue(B.c),alpha);
+ }
if (mode === "random") _r.rotate(R.randInt(0,360));
if (mode === "natural") {
_r.rotate(((B.plot) ? - B.plot.angle(B.position.plotted) : - B.dir) + (B.flow ? B.position.angle() : 0));
}
},
pop() {
- if (B.mode === "blend") B.markerTip();
+ if (B.p.blend) B.markerTip();
_r.pop();
- if (B.mode === "blend") Mix.mask.end(), Mix.blend(B.c);
+ if (B.p.blend) Mix.mask.end(), Mix.blend(B.c);
if (S.on) B.buffer.endShape();
},
markerTip() {
@@ -458,7 +440,7 @@
for (let s = 0; s < 5; s++) {
if (B.p.type === "marker") {
_r.circle(B.position.x,B.position.y, s/5 * B.w * B.p.weight * pressure)
- } else {
+ } else if (B.p.type === "custom" || B.p.type === "image") {
_r.push();
_r.translate(B.position.x, B.position.y),
B.adjust_tip(B.w * s/5 * pressure,alpha,B.p.rotate)
@@ -552,8 +534,8 @@
for (let i = 0; i < 4 * image.width * image.height; i += 4) {
let a = (imageData.data[i] + imageData.data[i + 1] + imageData.data[i + 2]) / 3;
imageData.data[i] = 255;
- imageData.data[i + 1] = 0;
- imageData.data[i + 2] = 0;
+ imageData.data[i + 1] = 255;
+ imageData.data[i + 2] = 255;
imageData.data[i + 3] = 255 - a;
}
context.putImageData(imageData,0,0);
@@ -736,7 +718,7 @@
}
draw (x,y,scale) {
if (this.origin) x = this.origin[0], y = this.origin[1], scale = 1;
- brushbox.flowShape(this,x,y,scale)
+ B.flowShape(this,x,y,scale)
}
}
diff --git a/spectral.min.js b/spectral.min.js
new file mode 100644
index 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