Path: blob/master/sandbox/RFinance2014/libraries/widgets/nvd3/js/cie.js
1433 views
(function(d3) {1var cie = d3.cie = {};23cie.lab = function(l, a, b) {4return arguments.length === 15? (l instanceof Lab ? lab(l.l, l.a, l.b)6: (l instanceof Lch ? lch_lab(l.l, l.c, l.h)7: rgb_lab((l = d3.rgb(l)).r, l.g, l.b)))8: lab(+l, +a, +b);9};1011cie.lch = function(l, c, h) {12return arguments.length === 113? (l instanceof Lch ? lch(l.l, l.c, l.h)14: (l instanceof Lab ? lab_lch(l.l, l.a, l.b)15: lab_lch((l = rgb_lab((l = d3.rgb(l)).r, l.g, l.b)).l, l.a, l.b)))16: lch(+l, +c, +h);17};1819cie.interpolateLab = function(a, b) {20a = cie.lab(a);21b = cie.lab(b);22var al = a.l,23aa = a.a,24ab = a.b,25bl = b.l - al,26ba = b.a - aa,27bb = b.b - ab;28return function(t) {29return lab_rgb(al + bl * t, aa + ba * t, ab + bb * t) + "";30};31};3233cie.interpolateLch = function(a, b) {34a = cie.lch(a);35b = cie.lch(b);36var al = a.l,37ac = a.c,38ah = a.h,39bl = b.l - al,40bc = b.c - ac,41bh = b.h - ah;42if (bh > 180) bh -= 360; else if (bh < -180) bh += 360; // shortest path43return function(t) {44return lch_lab(al + bl * t, ac + bc * t, ah + bh * t) + "";45};46};4748function lab(l, a, b) {49return new Lab(l, a, b);50}5152function Lab(l, a, b) {53this.l = l;54this.a = a;55this.b = b;56}5758Lab.prototype.brighter = function(k) {59return lab(Math.min(100, this.l + K * (arguments.length ? k : 1)), this.a, this.b);60};6162Lab.prototype.darker = function(k) {63return lab(Math.max(0, this.l - K * (arguments.length ? k : 1)), this.a, this.b);64};6566Lab.prototype.rgb = function() {67return lab_rgb(this.l, this.a, this.b);68};6970Lab.prototype.toString = function() {71return this.rgb() + "";72};7374function lch(l, c, h) {75return new Lch(l, c, h);76}7778function Lch(l, c, h) {79this.l = l;80this.c = c;81this.h = h;82}8384Lch.prototype.brighter = function(k) {85return lch(Math.min(100, this.l + K * (arguments.length ? k : 1)), this.c, this.h);86};8788Lch.prototype.darker = function(k) {89return lch(Math.max(0, this.l - K * (arguments.length ? k : 1)), this.c, this.h);90};9192Lch.prototype.rgb = function() {93return lch_lab(this.l, this.c, this.h).rgb();94};9596Lch.prototype.toString = function() {97return this.rgb() + "";98};99100// Corresponds roughly to RGB brighter/darker101var K = 18;102103// D65 standard referent104var X = 0.950470, Y = 1, Z = 1.088830;105106function lab_rgb(l, a, b) {107var y = (l + 16) / 116, x = y + a / 500, z = y - b / 200;108x = lab_xyz(x) * X;109y = lab_xyz(y) * Y;110z = lab_xyz(z) * Z;111return d3.rgb(112xyz_rgb( 3.2404542 * x - 1.5371385 * y - 0.4985314 * z),113xyz_rgb(-0.9692660 * x + 1.8760108 * y + 0.0415560 * z),114xyz_rgb( 0.0556434 * x - 0.2040259 * y + 1.0572252 * z)115);116}117118function rgb_lab(r, g, b) {119r = rgb_xyz(r);120g = rgb_xyz(g);121b = rgb_xyz(b);122var x = xyz_lab((0.4124564 * r + 0.3575761 * g + 0.1804375 * b) / X),123y = xyz_lab((0.2126729 * r + 0.7151522 * g + 0.0721750 * b) / Y),124z = xyz_lab((0.0193339 * r + 0.1191920 * g + 0.9503041 * b) / Z);125return lab(116 * y - 16, 500 * (x - y), 200 * (y - z));126}127128function lab_lch(l, a, b) {129var c = Math.sqrt(a * a + b * b),130h = Math.atan2(b, a) / Math.PI * 180;131return lch(l, c, h);132}133134function lch_lab(l, c, h) {135h = h * Math.PI / 180;136return lab(l, Math.cos(h) * c, Math.sin(h) * c);137}138139function lab_xyz(x) {140return x > 0.206893034 ? x * x * x : (x - 4 / 29) / 7.787037;141}142143function xyz_lab(x) {144return x > 0.008856 ? Math.pow(x, 1 / 3) : 7.787037 * x + 4 / 29;145}146147function xyz_rgb(r) {148return Math.round(255 * (r <= 0.00304 ? 12.92 * r : 1.055 * Math.pow(r, 1 / 2.4) - 0.055));149}150151function rgb_xyz(r) {152return (r /= 255) <= 0.04045 ? r / 12.92 : Math.pow((r + 0.055) / 1.055, 2.4);153}154})(d3);155156157