@bjornlu/colorblind
Version:
Simulate color blindness
2 lines (1 loc) • 1.58 kB
JavaScript
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((n="undefined"!=typeof globalThis?globalThis:n||self).colorblind={})}(this,(function(n){"use strict";const t=[0,1.05118294,-.05116099,0,1,0,0,0,1],e=[1,0,0,.9513092,0,.04866992,0,0,1],r=[1,0,0,0,1,0,-.86744736,1.86727089,0],o=[.212656,.715158,.072186],i=[.31399022,.63951294,.04649755,.15537241,.75789446,.08670142,.01775239,.10944209,.87256922],u=[5.47221206,-4.6419601,.16963708,-1.1252419,2.29317094,-.1678952,.02980165,-.19318073,1.16364789];function c(n,t){return[n[0]*t[0]+n[1]*t[1]+n[2]*t[2],n[3]*t[0]+n[4]*t[1]+n[5]*t[2],n[6]*t[0]+n[7]*t[1]+n[8]*t[2]]}function a(n){return[n.r/255,n.g/255,n.b/255]}function f(n){return[n.l,n.m,n.s]}function s(n){return{r:255*n[0],g:255*n[1],b:255*n[2]}}function l(n){return{l:n[0],m:n[1],s:n[2]}}function d(n){return{r:p(n.r),g:p(n.g),b:p(n.b)}}function p(n){return Math.round(Math.min(Math.max(n,0),255))}function m(n,t){const e=function(n){const t=a(n);return l(c(i,t))}(d(n));return d(function(n){const t=f(n);return s(c(u,t))}(l(c(t,f(e)))))}function h(n,t){const e=a(d(n)),r=(o=e,i=t,[o[0]*i[0]+o[1]*i[1]+o[2]*i[2]])[0];var o,i;return d(s(Array(3).fill(r)))}n.simulate=function(n,i){switch(i){case"protanopia":return m(n,t);case"deuteranopia":return m(n,e);case"tritanopia":return m(n,r);case"achromatopsia":return h(n,o);default:throw new Error("Invalid color deficiency provided")}},n.simulateDichromatic=m,n.simulateMonochromatic=h,Object.defineProperty(n,"__esModule",{value:!0})}));