chroma-js
Version:
JavaScript library for color conversions
990 lines (799 loc) • 29.3 kB
JavaScript
/**
* chroma.js - JavaScript library for color conversions
*
* Copyright (c) 2011-2019, Gregor Aisch
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The name Gregor Aisch may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL GREGOR AISCH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* -------------------------------------------------------
*
* chroma.js includes colors from colorbrewer2.org, which are released under
* the following license:
*
* Copyright (c) 2002 Cynthia Brewer, Mark Harrower,
* and The Pennsylvania State University.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*
* ------------------------------------------------------
*
* Named colors are taken from X11 Color Names.
* http://www.w3.org/TR/css3-color/#svg-color
*
* @preserve
*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.chroma = factory());
}(this, (function () { 'use strict';
var limit = function (x, min, max) {
if ( min === void 0 ) min=0;
if ( max === void 0 ) max=1;
return x < min ? min : x > max ? max : x;
};
var clip_rgb = function (rgb) {
rgb._clipped = false;
rgb._unclipped = rgb.slice(0);
for (var i=0; i<=3; i++) {
if (i < 3) {
if (rgb[i] < 0 || rgb[i] > 255) { rgb._clipped = true; }
rgb[i] = limit(rgb[i], 0, 255);
} else if (i === 3) {
rgb[i] = limit(rgb[i], 0, 1);
}
}
return rgb;
};
// ported from jQuery's $.type
var classToType = {};
for (var i = 0, list = ['Boolean', 'Number', 'String', 'Function', 'Array', 'Date', 'RegExp', 'Undefined', 'Null']; i < list.length; i += 1) {
var name = list[i];
classToType[("[object " + name + "]")] = name.toLowerCase();
}
var type = function(obj) {
return classToType[Object.prototype.toString.call(obj)] || "object";
};
var unpack = function (args, keyOrder) {
if ( keyOrder === void 0 ) keyOrder=null;
// if called with more than 3 arguments, we return the arguments
if (args.length >= 3) { return Array.prototype.slice.call(args); }
// with less than 3 args we check if first arg is object
// and use the keyOrder string to extract and sort properties
if (type(args[0]) == 'object' && keyOrder) {
return keyOrder.split('')
.filter(function (k) { return args[0][k] !== undefined; })
.map(function (k) { return args[0][k]; });
}
// otherwise we just return the first argument
// (which we suppose is an array of args)
return args[0];
};
var last = function (args) {
if (args.length < 2) { return null; }
var l = args.length-1;
if (type(args[l]) == 'string') { return args[l].toLowerCase(); }
return null;
};
var PI = Math.PI;
var utils = {
clip_rgb: clip_rgb,
limit: limit,
type: type,
unpack: unpack,
last: last,
PI: PI,
TWOPI: PI*2,
PITHIRD: PI/3,
DEG2RAD: PI / 180,
RAD2DEG: 180 / PI
};
var input = {
format: {},
autodetect: []
};
var last$1 = utils.last;
var clip_rgb$1 = utils.clip_rgb;
var type$1 = utils.type;
var Color = function Color() {
var args = [], len = arguments.length;
while ( len-- ) args[ len ] = arguments[ len ];
var me = this;
if (type$1(args[0]) === 'object' &&
args[0].constructor &&
args[0].constructor === this.constructor) {
// the argument is already a Color instance
return args[0];
}
// last argument could be the mode
var mode = last$1(args);
var autodetect = false;
if (!mode) {
autodetect = true;
if (!input.sorted) {
input.autodetect = input.autodetect.sort(function (a,b) { return b.p - a.p; });
input.sorted = true;
}
// auto-detect format
for (var i = 0, list = input.autodetect; i < list.length; i += 1) {
var chk = list[i];
mode = chk.test.apply(chk, args);
if (mode) { break; }
}
}
if (input.format[mode]) {
var rgb = input.format[mode].apply(null, autodetect ? args : args.slice(0,-1));
me._rgb = clip_rgb$1(rgb);
} else {
throw new Error('unknown format: '+args);
}
// add alpha channel
if (me._rgb.length === 3) { me._rgb.push(1); }
};
Color.prototype.toString = function toString () {
if (type$1(this.hex) == 'function') { return this.hex(); }
return ("[" + (this._rgb.join(',')) + "]");
};
var Color_1 = Color;
var chroma = function () {
var args = [], len = arguments.length;
while ( len-- ) args[ len ] = arguments[ len ];
return new (Function.prototype.bind.apply( chroma.Color, [ null ].concat( args) ));
};
chroma.Color = Color_1;
chroma.version = '2.0.6';
var chroma_1 = chroma;
var unpack$1 = utils.unpack;
var last$2 = utils.last;
var rnd = function (a) { return Math.round(a*100)/100; };
/*
* supported arguments:
* - hsl2css(h,s,l)
* - hsl2css(h,s,l,a)
* - hsl2css([h,s,l], mode)
* - hsl2css([h,s,l,a], mode)
* - hsl2css({h,s,l,a}, mode)
*/
var hsl2css = function () {
var args = [], len = arguments.length;
while ( len-- ) args[ len ] = arguments[ len ];
var hsla = unpack$1(args, 'hsla');
var mode = last$2(args) || 'lsa';
hsla[0] = rnd(hsla[0] || 0);
hsla[1] = rnd(hsla[1]*100) + '%';
hsla[2] = rnd(hsla[2]*100) + '%';
if (mode === 'hsla' || (hsla.length > 3 && hsla[3]<1)) {
hsla[3] = hsla.length > 3 ? hsla[3] : 1;
mode = 'hsla';
} else {
hsla.length = 3;
}
return (mode + "(" + (hsla.join(',')) + ")");
};
var hsl2css_1 = hsl2css;
var unpack$2 = utils.unpack;
/*
* supported arguments:
* - rgb2hsl(r,g,b)
* - rgb2hsl(r,g,b,a)
* - rgb2hsl([r,g,b])
* - rgb2hsl([r,g,b,a])
* - rgb2hsl({r,g,b,a})
*/
var rgb2hsl = function () {
var args = [], len = arguments.length;
while ( len-- ) args[ len ] = arguments[ len ];
args = unpack$2(args, 'rgba');
var r = args[0];
var g = args[1];
var b = args[2];
r /= 255;
g /= 255;
b /= 255;
var min = Math.min(r, g, b);
var max = Math.max(r, g, b);
var l = (max + min) / 2;
var s, h;
if (max === min){
s = 0;
h = Number.NaN;
} else {
s = l < 0.5 ? (max - min) / (max + min) : (max - min) / (2 - max - min);
}
if (r == max) { h = (g - b) / (max - min); }
else if (g == max) { h = 2 + (b - r) / (max - min); }
else if (b == max) { h = 4 + (r - g) / (max - min); }
h *= 60;
if (h < 0) { h += 360; }
if (args.length>3 && args[3]!==undefined) { return [h,s,l,args[3]]; }
return [h,s,l];
};
var rgb2hsl_1 = rgb2hsl;
var unpack$3 = utils.unpack;
var last$3 = utils.last;
var round = Math.round;
/*
* supported arguments:
* - rgb2css(r,g,b)
* - rgb2css(r,g,b,a)
* - rgb2css([r,g,b], mode)
* - rgb2css([r,g,b,a], mode)
* - rgb2css({r,g,b,a}, mode)
*/
var rgb2css = function () {
var args = [], len = arguments.length;
while ( len-- ) args[ len ] = arguments[ len ];
var rgba = unpack$3(args, 'rgba');
var mode = last$3(args) || 'rgb';
if (mode.substr(0,3) == 'hsl') {
return hsl2css_1(rgb2hsl_1(rgba), mode);
}
rgba[0] = round(rgba[0]);
rgba[1] = round(rgba[1]);
rgba[2] = round(rgba[2]);
if (mode === 'rgba' || (rgba.length > 3 && rgba[3]<1)) {
rgba[3] = rgba.length > 3 ? rgba[3] : 1;
mode = 'rgba';
}
return (mode + "(" + (rgba.slice(0,mode==='rgb'?3:4).join(',')) + ")");
};
var rgb2css_1 = rgb2css;
var unpack$4 = utils.unpack;
var round$1 = Math.round;
var hsl2rgb = function () {
var assign;
var args = [], len = arguments.length;
while ( len-- ) args[ len ] = arguments[ len ];
args = unpack$4(args, 'hsl');
var h = args[0];
var s = args[1];
var l = args[2];
var r,g,b;
if (s === 0) {
r = g = b = l*255;
} else {
var t3 = [0,0,0];
var c = [0,0,0];
var t2 = l < 0.5 ? l * (1+s) : l+s-l*s;
var t1 = 2 * l - t2;
var h_ = h / 360;
t3[0] = h_ + 1/3;
t3[1] = h_;
t3[2] = h_ - 1/3;
for (var i=0; i<3; i++) {
if (t3[i] < 0) { t3[i] += 1; }
if (t3[i] > 1) { t3[i] -= 1; }
if (6 * t3[i] < 1)
{ c[i] = t1 + (t2 - t1) * 6 * t3[i]; }
else if (2 * t3[i] < 1)
{ c[i] = t2; }
else if (3 * t3[i] < 2)
{ c[i] = t1 + (t2 - t1) * ((2 / 3) - t3[i]) * 6; }
else
{ c[i] = t1; }
}
(assign = [round$1(c[0]*255),round$1(c[1]*255),round$1(c[2]*255)], r = assign[0], g = assign[1], b = assign[2]);
}
if (args.length > 3) {
// keep alpha channel
return [r,g,b,args[3]];
}
return [r,g,b,1];
};
var hsl2rgb_1 = hsl2rgb;
var RE_RGB = /^rgb\(\s*(-?\d+),\s*(-?\d+)\s*,\s*(-?\d+)\s*\)$/;
var RE_RGBA = /^rgba\(\s*(-?\d+),\s*(-?\d+)\s*,\s*(-?\d+)\s*,\s*([01]|[01]?\.\d+)\)$/;
var RE_RGB_PCT = /^rgb\(\s*(-?\d+(?:\.\d+)?)%,\s*(-?\d+(?:\.\d+)?)%\s*,\s*(-?\d+(?:\.\d+)?)%\s*\)$/;
var RE_RGBA_PCT = /^rgba\(\s*(-?\d+(?:\.\d+)?)%,\s*(-?\d+(?:\.\d+)?)%\s*,\s*(-?\d+(?:\.\d+)?)%\s*,\s*([01]|[01]?\.\d+)\)$/;
var RE_HSL = /^hsl\(\s*(-?\d+(?:\.\d+)?),\s*(-?\d+(?:\.\d+)?)%\s*,\s*(-?\d+(?:\.\d+)?)%\s*\)$/;
var RE_HSLA = /^hsla\(\s*(-?\d+(?:\.\d+)?),\s*(-?\d+(?:\.\d+)?)%\s*,\s*(-?\d+(?:\.\d+)?)%\s*,\s*([01]|[01]?\.\d+)\)$/;
var round$2 = Math.round;
var css2rgb = function (css) {
css = css.toLowerCase().trim();
var m;
// rgb(250,20,0)
if ((m = css.match(RE_RGB))) {
var rgb = m.slice(1,4);
for (var i=0; i<3; i++) {
rgb[i] = +rgb[i];
}
rgb[3] = 1; // default alpha
return rgb;
}
// rgba(250,20,0,0.4)
if ((m = css.match(RE_RGBA))) {
var rgb$1 = m.slice(1,5);
for (var i$1=0; i$1<4; i$1++) {
rgb$1[i$1] = +rgb$1[i$1];
}
return rgb$1;
}
// rgb(100%,0%,0%)
if ((m = css.match(RE_RGB_PCT))) {
var rgb$2 = m.slice(1,4);
for (var i$2=0; i$2<3; i$2++) {
rgb$2[i$2] = round$2(rgb$2[i$2] * 2.55);
}
rgb$2[3] = 1; // default alpha
return rgb$2;
}
// rgba(100%,0%,0%,0.4)
if ((m = css.match(RE_RGBA_PCT))) {
var rgb$3 = m.slice(1,5);
for (var i$3=0; i$3<3; i$3++) {
rgb$3[i$3] = round$2(rgb$3[i$3] * 2.55);
}
rgb$3[3] = +rgb$3[3];
return rgb$3;
}
// hsl(0,100%,50%)
if ((m = css.match(RE_HSL))) {
var hsl = m.slice(1,4);
hsl[1] *= 0.01;
hsl[2] *= 0.01;
var rgb$4 = hsl2rgb_1(hsl);
rgb$4[3] = 1;
return rgb$4;
}
// hsla(0,100%,50%,0.5)
if ((m = css.match(RE_HSLA))) {
var hsl$1 = m.slice(1,4);
hsl$1[1] *= 0.01;
hsl$1[2] *= 0.01;
var rgb$5 = hsl2rgb_1(hsl$1);
rgb$5[3] = +m[4]; // default alpha = 1
return rgb$5;
}
};
css2rgb.test = function (s) {
return RE_RGB.test(s) ||
RE_RGBA.test(s) ||
RE_RGB_PCT.test(s) ||
RE_RGBA_PCT.test(s) ||
RE_HSL.test(s) ||
RE_HSLA.test(s);
};
var css2rgb_1 = css2rgb;
var type$2 = utils.type;
Color_1.prototype.css = function(mode) {
return rgb2css_1(this._rgb, mode);
};
chroma_1.css = function () {
var args = [], len = arguments.length;
while ( len-- ) args[ len ] = arguments[ len ];
return new (Function.prototype.bind.apply( Color_1, [ null ].concat( args, ['css']) ));
};
input.format.css = css2rgb_1;
input.autodetect.push({
p: 5,
test: function (h) {
var rest = [], len = arguments.length - 1;
while ( len-- > 0 ) rest[ len ] = arguments[ len + 1 ];
if (!rest.length && type$2(h) === 'string' && css2rgb_1.test(h)) {
return 'css';
}
}
});
var unpack$5 = utils.unpack;
var last$4 = utils.last;
var round$3 = Math.round;
var rgb2hex = function () {
var args = [], len = arguments.length;
while ( len-- ) args[ len ] = arguments[ len ];
var ref = unpack$5(args, 'rgba');
var r = ref[0];
var g = ref[1];
var b = ref[2];
var a = ref[3];
var mode = last$4(args) || 'auto';
if (a === undefined) { a = 1; }
if (mode === 'auto') {
mode = a < 1 ? 'rgba' : 'rgb';
}
r = round$3(r);
g = round$3(g);
b = round$3(b);
var u = r << 16 | g << 8 | b;
var str = "000000" + u.toString(16); //#.toUpperCase();
str = str.substr(str.length - 6);
var hxa = '0' + round$3(a * 255).toString(16);
hxa = hxa.substr(hxa.length - 2);
switch (mode.toLowerCase()) {
case 'rgba': return ("#" + str + hxa);
case 'argb': return ("#" + hxa + str);
default: return ("#" + str);
}
};
var rgb2hex_1 = rgb2hex;
var RE_HEX = /^#?([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/;
var RE_HEXA = /^#?([A-Fa-f0-9]{8})$/;
var hex2rgb = function (hex) {
if (hex.match(RE_HEX)) {
// remove optional leading #
if (hex.length === 4 || hex.length === 7) {
hex = hex.substr(1);
}
// expand short-notation to full six-digit
if (hex.length === 3) {
hex = hex.split('');
hex = hex[0]+hex[0]+hex[1]+hex[1]+hex[2]+hex[2];
}
var u = parseInt(hex, 16);
var r = u >> 16;
var g = u >> 8 & 0xFF;
var b = u & 0xFF;
return [r,g,b,1];
}
// match rgba hex format, eg #FF000077
if (hex.match(RE_HEXA)) {
if (hex.length === 9) {
// remove optional leading #
hex = hex.substr(1);
}
var u$1 = parseInt(hex, 16);
var r$1 = u$1 >> 24 & 0xFF;
var g$1 = u$1 >> 16 & 0xFF;
var b$1 = u$1 >> 8 & 0xFF;
var a = Math.round((u$1 & 0xFF) / 0xFF * 100) / 100;
return [r$1,g$1,b$1,a];
}
// we used to check for css colors here
// if _input.css? and rgb = _input.css hex
// return rgb
throw new Error(("unknown hex color: " + hex));
};
var hex2rgb_1 = hex2rgb;
var type$3 = utils.type;
Color_1.prototype.hex = function(mode) {
return rgb2hex_1(this._rgb, mode);
};
chroma_1.hex = function () {
var args = [], len = arguments.length;
while ( len-- ) args[ len ] = arguments[ len ];
return new (Function.prototype.bind.apply( Color_1, [ null ].concat( args, ['hex']) ));
};
input.format.hex = hex2rgb_1;
input.autodetect.push({
p: 4,
test: function (h) {
var rest = [], len = arguments.length - 1;
while ( len-- > 0 ) rest[ len ] = arguments[ len + 1 ];
if (!rest.length && type$3(h) === 'string' && [3,4,6,7,8,9].includes(h.length)) {
return 'hex';
}
}
});
var unpack$6 = utils.unpack;
var type$4 = utils.type;
Color_1.prototype.hsl = function() {
return rgb2hsl_1(this._rgb);
};
chroma_1.hsl = function () {
var args = [], len = arguments.length;
while ( len-- ) args[ len ] = arguments[ len ];
return new (Function.prototype.bind.apply( Color_1, [ null ].concat( args, ['hsl']) ));
};
input.format.hsl = hsl2rgb_1;
input.autodetect.push({
p: 2,
test: function () {
var args = [], len = arguments.length;
while ( len-- ) args[ len ] = arguments[ len ];
args = unpack$6(args, 'hsl');
if (type$4(args) === 'array' && args.length === 3) {
return 'hsl';
}
}
});
var labConstants = {
// Corresponds roughly to RGB brighter/darker
Kn: 18,
// D65 standard referent
Xn: 0.950470,
Yn: 1,
Zn: 1.088830,
t0: 0.137931034, // 4 / 29
t1: 0.206896552, // 6 / 29
t2: 0.12841855, // 3 * t1 * t1
t3: 0.008856452, // t1 * t1 * t1
};
var unpack$7 = utils.unpack;
var pow = Math.pow;
var rgb2lab = function () {
var args = [], len = arguments.length;
while ( len-- ) args[ len ] = arguments[ len ];
var ref = unpack$7(args, 'rgb');
var r = ref[0];
var g = ref[1];
var b = ref[2];
var ref$1 = rgb2xyz(r,g,b);
var x = ref$1[0];
var y = ref$1[1];
var z = ref$1[2];
var l = 116 * y - 16;
return [l < 0 ? 0 : l, 500 * (x - y), 200 * (y - z)];
};
var rgb_xyz = function (r) {
if ((r /= 255) <= 0.04045) { return r / 12.92; }
return pow((r + 0.055) / 1.055, 2.4);
};
var xyz_lab = function (t) {
if (t > labConstants.t3) { return pow(t, 1 / 3); }
return t / labConstants.t2 + labConstants.t0;
};
var rgb2xyz = function (r,g,b) {
r = rgb_xyz(r);
g = rgb_xyz(g);
b = rgb_xyz(b);
var x = xyz_lab((0.4124564 * r + 0.3575761 * g + 0.1804375 * b) / labConstants.Xn);
var y = xyz_lab((0.2126729 * r + 0.7151522 * g + 0.0721750 * b) / labConstants.Yn);
var z = xyz_lab((0.0193339 * r + 0.1191920 * g + 0.9503041 * b) / labConstants.Zn);
return [x,y,z];
};
var rgb2lab_1 = rgb2lab;
var unpack$8 = utils.unpack;
var pow$1 = Math.pow;
/*
* L* [0..100]
* a [-100..100]
* b [-100..100]
*/
var lab2rgb = function () {
var args = [], len = arguments.length;
while ( len-- ) args[ len ] = arguments[ len ];
args = unpack$8(args, 'lab');
var l = args[0];
var a = args[1];
var b = args[2];
var x,y,z, r,g,b_;
y = (l + 16) / 116;
x = isNaN(a) ? y : y + a / 500;
z = isNaN(b) ? y : y - b / 200;
y = labConstants.Yn * lab_xyz(y);
x = labConstants.Xn * lab_xyz(x);
z = labConstants.Zn * lab_xyz(z);
r = xyz_rgb(3.2404542 * x - 1.5371385 * y - 0.4985314 * z); // D65 -> sRGB
g = xyz_rgb(-0.9692660 * x + 1.8760108 * y + 0.0415560 * z);
b_ = xyz_rgb(0.0556434 * x - 0.2040259 * y + 1.0572252 * z);
return [r,g,b_,args.length > 3 ? args[3] : 1];
};
var xyz_rgb = function (r) {
return 255 * (r <= 0.00304 ? 12.92 * r : 1.055 * pow$1(r, 1 / 2.4) - 0.055)
};
var lab_xyz = function (t) {
return t > labConstants.t1 ? t * t * t : labConstants.t2 * (t - labConstants.t0)
};
var lab2rgb_1 = lab2rgb;
var unpack$9 = utils.unpack;
var type$5 = utils.type;
Color_1.prototype.lab = function() {
return rgb2lab_1(this._rgb);
};
chroma_1.lab = function () {
var args = [], len = arguments.length;
while ( len-- ) args[ len ] = arguments[ len ];
return new (Function.prototype.bind.apply( Color_1, [ null ].concat( args, ['lab']) ));
};
input.format.lab = lab2rgb_1;
input.autodetect.push({
p: 2,
test: function () {
var args = [], len = arguments.length;
while ( len-- ) args[ len ] = arguments[ len ];
args = unpack$9(args, 'lab');
if (type$5(args) === 'array' && args.length === 3) {
return 'lab';
}
}
});
var unpack$a = utils.unpack;
var type$6 = utils.type;
var round$4 = Math.round;
Color_1.prototype.rgb = function(rnd) {
if ( rnd === void 0 ) rnd=true;
if (rnd === false) { return this._rgb.slice(0,3); }
return this._rgb.slice(0,3).map(round$4);
};
Color_1.prototype.rgba = function(rnd) {
if ( rnd === void 0 ) rnd=true;
return this._rgb.slice(0,4).map(function (v,i) {
return i<3 ? (rnd === false ? v : round$4(v)) : v;
});
};
chroma_1.rgb = function () {
var args = [], len = arguments.length;
while ( len-- ) args[ len ] = arguments[ len ];
return new (Function.prototype.bind.apply( Color_1, [ null ].concat( args, ['rgb']) ));
};
input.format.rgb = function () {
var args = [], len = arguments.length;
while ( len-- ) args[ len ] = arguments[ len ];
var rgba = unpack$a(args, 'rgba');
if (rgba[3] === undefined) { rgba[3] = 1; }
return rgba;
};
input.autodetect.push({
p: 3,
test: function () {
var args = [], len = arguments.length;
while ( len-- ) args[ len ] = arguments[ len ];
args = unpack$a(args, 'rgba');
if (type$6(args) === 'array' && (args.length === 3 ||
args.length === 4 && type$6(args[3]) == 'number' && args[3] >= 0 && args[3] <= 1)) {
return 'rgb';
}
}
});
var type$7 = utils.type;
Color_1.prototype.alpha = function(a, mutate) {
if ( mutate === void 0 ) mutate=false;
if (a !== undefined && type$7(a) === 'number') {
if (mutate) {
this._rgb[3] = a;
return this;
}
return new Color_1([this._rgb[0], this._rgb[1], this._rgb[2], a], 'rgb');
}
return this._rgb[3];
};
Color_1.prototype.darken = function(amount) {
if ( amount === void 0 ) amount=1;
var me = this;
var lab = me.lab();
lab[0] -= labConstants.Kn * amount;
return new Color_1(lab, 'lab').alpha(me.alpha(), true);
};
Color_1.prototype.brighten = function(amount) {
if ( amount === void 0 ) amount=1;
return this.darken(-amount);
};
Color_1.prototype.darker = Color_1.prototype.darken;
Color_1.prototype.brighter = Color_1.prototype.brighten;
Color_1.prototype.get = function(mc) {
var ref = mc.split('.');
var mode = ref[0];
var channel = ref[1];
var src = this[mode]();
if (channel) {
var i = mode.indexOf(channel);
if (i > -1) { return src[i]; }
throw new Error(("unknown channel " + channel + " in mode " + mode));
} else {
return src;
}
};
var interpolator = {};
var type$8 = utils.type;
var mix = function (col1, col2, f) {
if ( f === void 0 ) f=0.5;
var rest = [], len = arguments.length - 3;
while ( len-- > 0 ) rest[ len ] = arguments[ len + 3 ];
var mode = rest[0] || 'lrgb';
if (!interpolator[mode] && !rest.length) {
// fall back to the first supported mode
mode = Object.keys(interpolator)[0];
}
if (!interpolator[mode]) {
throw new Error(("interpolation mode " + mode + " is not defined"));
}
if (type$8(col1) !== 'object') { col1 = new Color_1(col1); }
if (type$8(col2) !== 'object') { col2 = new Color_1(col2); }
return interpolator[mode](col1, col2, f)
.alpha(col1.alpha() + f * (col2.alpha() - col1.alpha()));
};
Color_1.prototype.mix =
Color_1.prototype.interpolate = function(col2, f) {
if ( f === void 0 ) f=0.5;
var rest = [], len = arguments.length - 2;
while ( len-- > 0 ) rest[ len ] = arguments[ len + 2 ];
return mix.apply(void 0, [ this, col2, f ].concat( rest ));
};
var type$9 = utils.type;
Color_1.prototype.set = function(mc, value, mutate) {
if ( mutate === void 0 ) mutate=false;
var ref = mc.split('.');
var mode = ref[0];
var channel = ref[1];
var src = this[mode]();
if (channel) {
var i = mode.indexOf(channel);
if (i > -1) {
if (type$9(value) == 'string') {
switch(value.charAt(0)) {
case '+': src[i] += +value; break;
case '-': src[i] += +value; break;
case '*': src[i] *= +(value.substr(1)); break;
case '/': src[i] /= +(value.substr(1)); break;
default: src[i] = +value;
}
} else if (type$9(value) === 'number') {
src[i] = value;
} else {
throw new Error("unsupported value for Color.set");
}
var out = new Color_1(src, mode);
if (mutate) {
this._rgb = out._rgb;
return this;
}
return out;
}
throw new Error(("unknown channel " + channel + " in mode " + mode));
} else {
return src;
}
};
var sqrt = Math.sqrt;
var pow$2 = Math.pow;
var lrgb = function (col1, col2, f) {
var ref = col1._rgb;
var x1 = ref[0];
var y1 = ref[1];
var z1 = ref[2];
var ref$1 = col2._rgb;
var x2 = ref$1[0];
var y2 = ref$1[1];
var z2 = ref$1[2];
return new Color_1(
sqrt(pow$2(x1,2) * (1-f) + pow$2(x2,2) * f),
sqrt(pow$2(y1,2) * (1-f) + pow$2(y2,2) * f),
sqrt(pow$2(z1,2) * (1-f) + pow$2(z2,2) * f),
'rgb'
)
};
// register interpolator
interpolator.lrgb = lrgb;
var valid = function () {
var args = [], len = arguments.length;
while ( len-- ) args[ len ] = arguments[ len ];
try {
new (Function.prototype.bind.apply( Color_1, [ null ].concat( args) ));
return true;
} catch (e) {
return false;
}
};
// feel free to comment out anything to rollup
// a smaller chroma.js built
// io --> convert colors
// operators --> modify existing Colors
// interpolators
// generators -- > create new colors
chroma_1.mix = chroma_1.interpolate = mix;
// other utility methods
chroma_1.valid = valid;
var indexLight = chroma_1;
return indexLight;
})));