d3-cam02
Version:
CAM02 color space.
2 lines • 6.46 kB
JavaScript
// https://github.com/connorgr/d3-cam02 Version 0.1.5. Copyright 2017 Connor Gramazio.
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("d3-color")):"function"==typeof define&&define.amd?define(["exports","d3-color"],n):n(t.d3=t.d3||{},t.d3)}(this,function(t,n){"use strict";function h(t,n,h){return t/=255,n/=255,h/=255,t=t>.04045?Math.pow((t+.055)/1.055,2.4):t/12.92,n=n>.04045?Math.pow((n+.055)/1.055,2.4):n/12.92,h=h>.04045?Math.pow((h+.055)/1.055,2.4):h/12.92,{x:100*(.4124*t+.3576*n+.1805*h),y:100*(.2126*t+.7152*n+.0722*h),z:100*(.0193*t+.1192*n+.9505*h)}}function r(t,n,h){function r(t){return 255*(t<=.0031308?12.92*t:1.055*Math.pow(t,1/2.4)-.055)}t/=100,n/=100,h/=100;var a=3.2404542*t+-1.5371385*n-.4985314*h,o=-.969266*t+1.8760108*n+.041556*h,i=.0556434*t+-.2040259*n+1.0572252*h;return{r:r(a),g:r(o),b:r(i)}}function a(t,n,h){return{l:.7328*t+.4296*n-.1624*h,m:-.7036*t+1.6975*n+.0061*h,s:.003*t+.0136*n+.9834*h}}function o(t,n,h){return{x:1.096124*t-.278869*n+.182745*h,y:.454369*t+.473533*n+.072098*h,z:-.009628*t-.005698*n+1.015326*h}}function i(t,n,h){return{lh:.7409792*t+.218025*n+.0410058*h,mh:.2853532*t+.6242014*n+.0904454*h,sh:-.009628*t-.005698*n+1.015326*h}}function c(t,n,h){return{x:1.910197*t-1.112124*n+.201908*h,y:.37095*t+.629054*n-8e-6*h,z:h}}function e(t,n){var h=Math.pow(n*t/100,.42);return 400*h/(27.13+h)+.1}function s(t,n){return 100/n*Math.pow(27.13*Math.abs(t-.1)/(400-Math.abs(t-.1)),1/.42)}function u(t,n,h){function r(t,n){var h=J.D65_Y,r=J.d;return t*(h*r/n+(1-r))}var o={},c=a(J.D65_X,J.D65_Y,J.D65_Z),s=r(t,c.l),u=r(n,c.m),M=r(h,c.s),f=i(s,u,M),p=f.lh,l=f.mh,b=f.sh,y=e(p,J.fl),d=e(l,J.fl),w=e(b,J.fl),_=y-12*d/11+w/11,v=1/9*(y+d-2*w);o.h=180/Math.PI*Math.atan2(v,_),o.h<0&&(o.h+=360);var D;o.h<20.14?(D=(o.h+122.47)/1.2+(20.14-o.h)/.8,o.H=300+(o.h+122.47)/1.2*100/D):o.h<90?(D=(o.h-20.14)/.8+(90-o.h)/.7,o.H=(o.h-20.14)/.8*100/D):o.h<164.25?(D=(o.h-90)/.7+(164.25-o.h)/1,o.H=100+(o.h-90)/.7*100/D):o.h<237.53?(D=(o.h-164.25)/1+(237.53-o.h)/1.2,o.H=200+(o.h-164.25)/1*100/D):(D=(o.h-237.53)/1.2+(360-o.h+20.14)/.8,o.H=300+(o.h-237.53)/1.2*100/D);var g=(2*y+d+.05*w-.305)*J.nbb;o.J=100*Math.pow(g/J.achromaticResponseToWhite,J.c*J.z);var m=.25*(Math.cos(o.h*Math.PI/180+2)+3.8),x=5e4/13*J.nc*J.ncb*m*Math.sqrt(_*_+v*v)/(y+d+1.05*w);return o.C=Math.pow(x,.9)*Math.sqrt(o.J/100)*Math.pow(1.64-Math.pow(.29,J.n),.73),o.Q=4/J.c*Math.sqrt(o.J/100)*(J.achromaticResponseToWhite+4)*Math.pow(J.fl,.25),o.M=o.C*Math.pow(J.fl,.25),o.s=100*Math.sqrt(o.M/o.Q),o}function M(t,n,h,r){var a=t/r+.305;return{l:.32787*a+.32145*n+.20527*h,m:.32787*a-.63507*n-.18603*h,s:.32787*a-.15681*n-4.49038*h}}function f(t,n,h){var i,e,u,f,p=Math.pow(n/(Math.sqrt(t/100)*Math.pow(1.64-Math.pow(.29,J.n),.73)),1/.9),l=.25*(Math.cos(h*Math.PI/180+2)+3.8),b=Math.pow(t/100,1/(J.c*J.z))*J.achromaticResponseToWhite,y=5e4/13*J.nc*J.ncb*l/p,d=b/J.nbb+.305,w=1.05,_=h*Math.PI/180;Math.abs(Math.sin(_))>=Math.abs(Math.cos(_))?(i=y/Math.sin(_),f=3.05*d*(460/1403)/(i+220/1403*3.05*(Math.cos(_)/Math.sin(_))-27/1403+w*(6300/1403)),u=f*(Math.cos(_)/Math.sin(_))):(e=y/Math.cos(_),u=3.05*d*(460/1403)/(e+220/1403*3.05-Math.sin(_)/Math.cos(_)*-4.695652173913044),f=u*(Math.sin(_)/Math.cos(_)));var v=M(b,u,f,J.nbb),D=v.l,g=v.m,m=v.s,x=s(D,J.fl),Y=s(g,J.fl),z=s(m,J.fl),q=c(x,Y,z),C=a(q.x,q.y,q.z),P=a(J.D65_X,J.D65_Y,J.D65_Z),k=C.l/(J.D65_Y*J.d/P.l+(1-J.d)),I=C.m/(J.D65_Y*J.d/P.m+(1-J.d)),j=C.s/(J.D65_Y*J.d/P.s+(1-J.d)),H=o(k,I,j);return r(H.x,H.y,H.z)}function p(t){if(t instanceof b)return new b(t.J,t.C,t.h,t.opacity);t instanceof n.rgb||(t=n.rgb(t));var r=h(t.r,t.g,t.b),o=a(r.x,r.y,r.z),i=u(o.l,o.m,o.s);return new b(i.J,i.C,i.h,t.opacity)}function l(t,n,h,r){return 1===arguments.length?p(t):new b(t,n,h,null==r?1:r)}function b(t,n,h,r){this.J=+t,this.C=+n,this.h=+h,this.opacity=+r}function y(t){if(t instanceof w)return new w(t.J,t.a,t.b,t.opacity);t instanceof n.rgb||(t=n.rgb(t));var r=h(t.r,t.g,t.b),o=a(r.x,r.y,r.z),i=u(o.l,o.m,o.s),c=m.ucs,e=(1+100*c.c1)*i.J/(1+c.c1*i.J);e/=c.k_l;var s=1/c.c2*Math.log(1+c.c2*i.M);return new w(e,s*Math.cos(v*i.h),s*Math.sin(v*i.h),t.opacity)}function d(t,n,h,r){return r=null==r?1:r,1===arguments.length?y(t):new w(t,n,h,r)}function w(t,n,h,r){this.J=t,this.a=n,this.b=h,this.opacity=r}function _(t,n){function h(t){return function(){return t}}function r(t,n){return function(h){return t+h*n}}function a(t,n){var a=n-t;return a?r(t,a):h(isNaN(t)?n:t)}t=y(t),n=y(n);var o=a(t.J,n.J),i=a(t.a,n.a),c=a(t.b,n.b),e=a(t.opacity,n.opacity);return function(n){return t.J=o(n),t.a=i(n),t.b=c(n),t.opacity=e(n),t+""}}var v=Math.PI/180,D=180/Math.PI,J=function(){var t={D65_X:95.047,D65_Y:100,D65_Z:108.883,la:64/Math.PI/5,yb:20,f:1,c:.69,nc:1};t.D65_LMS=a(t.D65_X,t.D65_Y,t.D65_Z),t.n=t.yb/t.D65_Y,t.z=1.48+Math.sqrt(t.n);var n=1/(5*t.la+1);return t.fl=.2*Math.pow(n,4)*(5*t.la)+.1*Math.pow(1-Math.pow(n,4),2)*Math.pow(5*t.la,1/3),t.nbb=.725*Math.pow(1/t.n,.2),t.ncb=t.nbb,t.d=t.f*(1-1/3.6*Math.exp((-t.la-42)/92)),t.achromaticResponseToWhite=function(){var n=t.D65_LMS.l,h=t.D65_LMS.m,r=t.D65_LMS.s,a=n*(t.D65_Y*t.d/n+(1-t.d)),o=h*(t.D65_Y*t.d/h+(1-t.d)),c=r*(t.D65_Y*t.d/r+(1-t.d)),s=i(a,o,c),u=s.lh,M=s.mh,f=s.sh;return(2*e(u,t.fl)+e(M,t.fl)+.05*e(f,t.fl)-.305)*t.nbb}(),t}(),g=b.prototype=l.prototype=Object.create(n.color.prototype);g.constructor=b,g.brighter=function(t){return new b(this.J+18*(null===t?1:t),this.C,this.h,this.opacity)},g.darker=function(t){return new b(this.J-18*(null===t?1:t),this.C,this.h,this.opacity)},g.rgb=function(){var t=f(this.J,this.C,this.h);return n.rgb(t.r,t.g,t.b,this.opacity)};var m={lcd:{k_l:.77,c1:.007,c2:.0053},scd:{k_l:1.24,c1:.007,c2:.0363},ucs:{k_l:1,c1:.007,c2:.0228}},x=w.prototype=d.prototype=Object.create(n.color.prototype);x.constructor=b,x.brighter=function(t){return new w(this.J+18*(null===t?1:t),this.a,this.b,this.opacity)},x.darker=function(t){return new w(this.J-18*(null===t?1:t),this.a,this.b,this.opacity)},x.rgb=function(){var t=m.ucs,h=this.J,r=this.a,a=this.b,o=Math.sqrt(r*r+a*a),i=(Math.exp(o*t.c2)-1)/t.c2,c=D*Math.atan2(a,r);c<0&&(c=360+c);var e=i/Math.pow(J.fl,.25),s=h/(1+t.c1*(100-h)),u=f(s,e,c);return n.rgb(u.r,u.g,u.b,this.opacity)},x.de=function(t){return function(n){n instanceof w||(n=y(n));var h=t.k_l,r=Math.abs(this.J-n.J),a=Math.abs(this.a-n.a),o=Math.abs(this.b-n.b);return Math.sqrt(r/h*(r/h)+a*a+o*o)}}(m.ucs),t.jch=l,t.jab=d,t.interpolateJab=_,Object.defineProperty(t,"__esModule",{value:!0})});