-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathspectral.min.js
1 lines (1 loc) · 16.5 KB
/
spectral.min.js
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={});