UNPKG

color-blend

Version:

Blends RGBA colors with different blend modes

3 lines (2 loc) 3.48 kB
function t(t,n,u){return{r:255*u(t.r/255,n.r/255),g:255*u(t.g/255,n.g/255),b:255*u(t.b/255,n.b/255)}}function n(t,n){return n}function u(t,n){return t*n}function r(t,n){return t+n-t*n}function i(t,n){return a(n,t)}function o(t,n){return Math.min(t,n)}function e(t,n){return Math.min(Math.max(t,n),1)}function p(t,n){return 0===t?0:1===n?1:Math.min(1,t/(1-n))}function c(t,n){return 1===t?1:0===n?0:1-Math.min(1,(1-t)/n)}function a(t,n){return n<=.5?u(t,2*n):r(t,2*n-1)}function f(t,n){return n<=.5?t-(1-2*n)*t*(1-t):t+(2*n-1)*((t<=.25?((16*t-12)*t+4)*t:Math.sqrt(t))-t)}function g(t,n){return Math.abs(t-n)}function b(t,n){return t+n-2*t*n}function s(t,n,u){return Math.min(Math.max(t||0,n),u)}function h(t){return{r:s(t.r,0,255),g:s(t.g,0,255),b:s(t.b,0,255),a:s(t.a,0,1)}}function x(t){return{r:255*t.r,g:255*t.g,b:255*t.b,a:t.a}}function O(t){return{r:t.r/255,g:t.g/255,b:t.b/255,a:t.a}}function I(t,n){void 0===n&&(n=0);var u=Math.pow(10,n);return{r:Math.round(t.r*u)/u,g:Math.round(t.g*u)/u,b:Math.round(t.b*u)/u,a:t.a}}function M(t,n,u,r,i,o){return(1-n/u)*r+n/u*Math.round((1-t)*i+t*o)}function m(t,n,u,r,i){void 0===i&&(i={unitInput:!1,unitOutput:!1,roundOutput:!0}),i.unitInput&&(t=x(t),n=x(n)),t=h(t);var o=(n=h(n)).a+t.a-n.a*t.a,e=u(t,n,r),p=h({r:M(t.a,n.a,o,t.r,n.r,e.r),g:M(t.a,n.a,o,t.g,n.g,e.g),b:M(t.a,n.a,o,t.b,n.b,e.b),a:o});return i.unitOutput?O(p):i.roundOutput?I(p):I(p,9)}function d(t,n,u){return x(u(O(t),O(n)))}function l(t){return.3*t.r+.59*t.g+.11*t.b}function v(t,n){var u=n-l(t);return function(t){var n=l(t),u=t.r,r=t.g,i=t.b,o=Math.min(u,r,i),e=Math.max(u,r,i);function p(t){return n+(t-n)*n/(n-o)}function c(t){return n+(t-n)*(1-n)/(e-n)}return o<0&&(u=p(u),r=p(r),i=p(i)),e>1&&(u=c(u),r=c(r),i=c(i)),{r:u,g:r,b:i}}({r:t.r+u,g:t.g+u,b:t.b+u})}function y(t){return Math.max(t.r,t.g,t.b)-Math.min(t.r,t.g,t.b)}function L(t,n){var u=["r","g","b"].sort(function(n,u){return t[n]-t[u]}),r=u[0],i=u[1],o=u[2],e={r:t.r,g:t.g,b:t.b};return e[o]>e[r]?(e[i]=(e[i]-e[r])*n/(e[o]-e[r]),e[o]=n):e[i]=e[o]=0,e[r]=0,e}function k(t,n){return v(L(n,y(t)),l(t))}function q(t,n){return v(L(t,y(n)),l(t))}function w(t,n){return v(n,l(t))}function B(t,n){return v(t,l(n))}exports.color=function(t,n){return m(t,n,d,w,{unitInput:!0,unitOutput:!0})},exports.colorBurn=function(n,u){return m(n,u,t,c,{unitInput:!0,unitOutput:!0})},exports.colorDodge=function(n,u){return m(n,u,t,p,{unitInput:!0,unitOutput:!0})},exports.darken=function(n,u){return m(n,u,t,o,{unitInput:!0,unitOutput:!0})},exports.difference=function(n,u){return m(n,u,t,g,{unitInput:!0,unitOutput:!0})},exports.exclusion=function(n,u){return m(n,u,t,b,{unitInput:!0,unitOutput:!0})},exports.hardLight=function(n,u){return m(n,u,t,a,{unitInput:!0,unitOutput:!0})},exports.hue=function(t,n){return m(t,n,d,k,{unitInput:!0,unitOutput:!0})},exports.lighten=function(n,u){return m(n,u,t,e,{unitInput:!0,unitOutput:!0})},exports.luminosity=function(t,n){return m(t,n,d,B,{unitInput:!0,unitOutput:!0})},exports.multiply=function(n,r){return m(n,r,t,u,{unitInput:!0,unitOutput:!0})},exports.normal=function(u,r){return m(u,r,t,n,{unitInput:!0,unitOutput:!0})},exports.overlay=function(n,u){return m(n,u,t,i,{unitInput:!0,unitOutput:!0})},exports.saturation=function(t,n){return m(t,n,d,q,{unitInput:!0,unitOutput:!0})},exports.screen=function(n,u){return m(n,u,t,r,{unitInput:!0,unitOutput:!0})},exports.softLight=function(n,u){return m(n,u,t,f,{unitInput:!0,unitOutput:!0})}; //# sourceMappingURL=index.js.map