@plangrid/structure
Version:
CSS structure library
1,697 lines (1,542 loc) • 114 kB
JavaScript
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
const ssv = require("ssv")
const paint = require("@plangrid/paint")
const structure = require("./")
const handle = {
paint: paint,
structure: structure
}
const demo = window.demo = function(libs) {
const dope = require("dope")
const ssv = require("ssv")
const slice = [].slice
slice.call(dope.queryData(libs)).forEach(function(el) {
let classes = ssv.split(libs).map(function(lib) {
return handle[lib].bond(dope.dataset(el, lib) || "")
}).join(" ")
classes = ssv.uniq(classes)
el.setAttribute("class", classes)
})
}
demo("paint structure")
function facilitate(types, acts) {
const ssv = require("ssv")
function handle(element, type) {
ssv.split(element.dataset[type]).forEach(invoke, element)
}
function invoke(method) {
acts[method].call(this)
}
function capture(type) {
document.addEventListener(type, function(ev) {
for (let node = ev.target; node; node = node.parentNode) {
if (node === document) break
if (node.dataset[type]) handle(node, type)
}
}, true)
}
types.forEach(capture)
}
function toggleDU(element) {
const original = element.getAttribute("class")
let classes = original
const openUnion = element.dataset.openUnion
const openDiff = element.dataset.openDiff
if (!openUnion) throw new Error("Missing openUnion")
if (!openDiff) throw new Error("Missing openDiff")
let wasOpen = ssv.all(classes, openUnion)
if (wasOpen) {
classes = ssv.union(classes, openDiff)
classes = ssv.diff(classes, openUnion)
} else {
classes = ssv.diff(classes, openDiff)
classes = ssv.union(classes, openUnion)
}
if (original !== classes) {
element.setAttribute("class", classes)
}
}
function toggleExpanded(element) {
const from = element.getAttribute("aria-expanded")
const to = "open" === from ? "closed" : "open"
element.setAttribute("aria-expanded", to)
}
facilitate(["click"], {
toggleDrawerR1: function() {
const drawer = document.querySelector("[data-demo=DrawerR1]")
toggleDU(drawer)
if (drawer.hasAttribute("hidden")) drawer.removeAttribute("hidden")
else drawer.setAttribute("hidden", "")
},
toggleExpanded: function() {
toggleExpanded(this)
},
logThis: function() {
console.log(this)
},
detachThis: function() {
this.parentNode.removeChild(this)
},
fillContentR1: function() {
let text = ""
for (let i = 0, l = 100; i < l;) text += i++ + "<br>"
document.querySelector("[data-demo=ContentR1]").innerHTML = text
}
})
// expose for use in console
window.paint = paint;
window.structure = structure;
},{"./":13,"@plangrid/paint":3,"dope":10,"ssv":11}],2:[function(require,module,exports){
const format = "toLowerCase"
const empty = ""
const dyez = []
const dyes = dyez.slice.bind(dyez)
const dyeWex = Object.create(null)
const dyeDex = Object.create(null)
const dyeGex = Object.create(null)
const hexDye = Object.create(null)
function set(o, k, v, fallback) {
o[k.toLowerCase()] = o[k.toUpperCase()] = v[format]() || fallback
}
function supply(wex, dex, gex, dye) {
set(dyeWex, dye, wex)
set(dyeDex, dye, dex)
set(dyeGex, dye, gex)
set(hexDye, wex, dye, hexDye[wex])
set(hexDye, dex, dye, hexDye[dex])
set(hexDye, gex, dye, hexDye[gex])
dyez.push(dye)
}
supply("#ff0000", "#a30000", empty, "red")
supply("#f38109", "#e6400a", empty, "orange")
supply("#a17541", "#704c22", empty, "brown")
supply("#ffff00", "#d9bd00", empty, "yellow")
supply("#9fb254", "#748726", empty, "olive")
supply("#00ff00", "#3e7935", empty, "green")
supply("#5ceeee", "#007aa5", empty, "cyan")
supply("#0a43c2", "#05266b", "#0000ff", "blue")
supply("#c33ee1", "#7e009d", empty, "purple")
supply("#f77f68", "#be3b6b", empty, "pink")
supply("#dddddd", "#9b9b9b", empty, "gray")
supply("#000000", "#000000", empty, "black")
function via(object) {
return function(key) {
return object[key] || void console.warn("Unknown ink:", key) || empty
}
}
const wet = via(dyeWex)
const dry = via(dyeDex)
const gel = via(dyeGex)
const dye = via(hexDye)
module.exports = Object.freeze({
dry: dry,
dye: dye,
dyes: dyes,
inks: dyes,
gel: gel,
master: dry,
user: wet,
wet: wet,
});
},{}],3:[function(require,module,exports){
const cader = require("cader")
const paint = new cader
const ink = require("./ink")
const sheen = require("./sheen")
const swatch = require("./swatch")
function teach(Skills, Hues) {
const lesson = {}
Skills.forEach(function(Skill) {
const skill = Skill.toLowerCase()
Hues.forEach(function(Hue) {
const hue = Hue.toLowerCase()
lesson[Skill + Hue] = sheen[skill](hue)
})
})
return lesson
}
paint.save(teach(
["Matte", "Maxxe", "Glaze", "Glaxe", "Vivid", "Vivix"],
["Red", "Blue", "Amber", "Green", "Purple"]
)).save(teach(
["Matte", "Maxxe"],
["Mono"]
)).freeze()
Object.assign(paint, sheen, swatch, ink)
Object.freeze(paint)
module.exports = paint;
},{"./ink":2,"./sheen":4,"./swatch":5,"cader":7}],4:[function(require,module,exports){
const sheen = {}
const replace = require("./templater/replace")
function sheener(stereo, mono) {
mono = mono ? mono.join(" ") : ""
return function(hue) {
if ("mono" === hue) return mono
return stereo.map(replace.hue(hue)).join(" ")
}
}
sheen.vivid = sheener([
"ggg-hue-500", "bbb-hue-500", "ccc-white",
"fgg-hue-500", "fbb-hue-500", "fcc-white",
"hgg-hue-700", "hbb-hue-700", "hcc-white",
"agg-hue-700", "abb-hue-700", "acc-white",
"dgg-snow", "dbb-smoke", "dcc-silver",
])
sheen.vivix = sheener([
"ggg-hue-700", "bbb-hue-700", "ccc-white",
"fgg-hue-700", "fbb-hue-700", "fcc-white",
"hgg-hue-700", "hbb-hue-700", "hcc-white",
"agg-hue-700", "abb-hue-700", "acc-white",
"dgg-snow", "dbb-smoke", "dcc-silver",
])
sheen.glaze = sheener([
"ggg-white", "bbb-smog", "ccc-hue-900",
"fgg-white", "fbb-smog", "fcc-hue-900",
"hgg-snow", "hbb-smog", "hcc-hue-900",
"agg-hue-100", "abb-hue-200", "acc-hue-900",
"dgg-snow", "dbb-smoke", "dcc-silver",
])
sheen.glaxe = sheener([
"ggg-hue-100", "bbb-hue-200", "ccc-hue-900",
"fgg-hue-100", "fbb-hue-200", "fcc-hue-900",
"hgg-hue-100", "hbb-hue-200", "hcc-hue-900",
"agg-hue-100", "abb-hue-200", "acc-hue-900",
"dgg-snow", "dbb-smoke", "dcc-silver",
])
sheen.matte = sheener([
"ggg-transparent",
"bbb-transparent",
"ccc-hue-500",
"fcc-hue-500",
"hcc-hue-700",
"acc-hue-700",
"dcc-silver",
], [
"ggg-transparent",
"bbb-transparent",
"ccc-slate",
"fcc-slate",
"hcc-steel",
"acc-steel",
"dcc-silver",
])
sheen.maxxe = sheener([
"ggg-transparent",
"bbb-transparent",
"ccc-hue-700",
"fcc-hue-700",
"hcc-hue-700",
"acc-hue-700",
"dcc-silver",
], [
"ggg-transparent",
"bbb-transparent",
"ccc-steel",
"fcc-steel",
"hcc-steel",
"acc-steel",
"dcc-silver",
])
module.exports = Object.freeze(sheen);
},{"./templater/replace":6}],5:[function(require,module,exports){
const chroma = require("chroma-js");
const deepFreeze = require("deep-freeze");
const unquote = require("unquote");
const push = [].push;
const registry = [];
function register(batch) {
push.apply(registry, batch);
return batch;
}
function pluck(key) {
return registry.map(function(o) {
return o[key]
})
}
function toRgb(v) {
return chroma(v).css("rgba")
}
function swatch(id) {
const name = id && unquote(id);
let value;
function read(o) {
return value = o.id === name && o.value
}
if (registry.some(read)) return value;
throw new Error("Unknown swatch: " + id);
}
function identify(value) {
const target = toRgb(value);
let id;
function read(o) {
return id = toRgb(o.value) === target && o.id
}
if (registry.some(read)) return id;
throw new Error("Cannot identify: " + value);
}
function nearest(value) {
let id;
let compare = chroma.deltaE;
let diff = compare("black", "white");
registry.forEach(function(o) {
if (chroma(o.value).alpha() < 1) return;
var d = compare(value, o.value);
if (d < diff) {
diff = d;
id = o.id;
}
})
if (null == id) throw new Error("Cannot compare: " + value);
return id;
}
function ideal(id) {
return identify(swatch(id))
}
function isIdeal(id) {
return ideal(id) === id
}
function ideals() {
return pluck("id").filter(isIdeal)
}
const grays = register([
{ id: "black", value: "#1F323D" },
{ id: "steel", value: "#40525E" },
{ id: "slate", value: "#62727E" },
{ id: "silver", value: "#8595A1" },
{ id: "carbon", value: "#B4C5D0" },
{ id: "smog", value: "#CDD9E2" },
{ id: "smoke", value: "#E0E7ED" },
{ id: "slush", value: "#E6ECF0" },
{ id: "sleet", value: "#F2F5F8" },
{ id: "snow", value: "#F8FAFB" },
{ id: "white", value: "#ffffff" }
]);
const shades = register([
{ id: "transparent", value: "rgba(255, 255, 255, 0)" },
{ id: "dark-90", value: "rgba(31, 49, 61, 0.9)" },
{ id: "dark-80", value: "rgba(31, 49, 61, 0.8)" },
{ id: "dark-70", value: "rgba(31, 49, 61, 0.7)" },
{ id: "dark-60", value: "rgba(31, 49, 61, 0.6)" },
{ id: "dark-50", value: "rgba(31, 49, 61, 0.5)" },
{ id: "dark-40", value: "rgba(31, 49, 61, 0.4)" },
{ id: "dark-30", value: "rgba(31, 49, 61, 0.3)" },
{ id: "dark-20", value: "rgba(31, 49, 61, 0.2)" },
{ id: "dark-10", value: "rgba(31, 49, 61, 0.1)" },
{ id: "dark-05", value: "rgba(31, 49, 61, 0.05)" },
{ id: "light-90", value: "rgba(250, 251, 252, 0.9)" },
{ id: "light-80", value: "rgba(250, 251, 252, 0.8)" },
{ id: "light-70", value: "rgba(250, 251, 252, 0.7)" },
{ id: "light-60", value: "rgba(250, 251, 252, 0.6)" },
{ id: "light-50", value: "rgba(250, 251, 252, 0.5)" },
{ id: "light-40", value: "rgba(250, 251, 252, 0.4)" },
{ id: "light-30", value: "rgba(250, 251, 252, 0.3)" },
{ id: "light-20", value: "rgba(250, 251, 252, 0.2)" },
{ id: "light-10", value: "rgba(250, 251, 252, 0.1)" },
{ id: "light-05", value: "rgba(250, 251, 252, 0.05)" },
]);
const ui = register([
{ id: "blue-100", value: "#ecf1fc" },
{ id: "blue-200", value: "#b3c9f2" },
{ id: "blue-400", value: "#5597e4" },
{ id: "blue-500", value: "#0085de" },
{ id: "blue-700", value: "#1c609e" },
{ id: "blue-900", value: "#1e456f" },
{ id: "green-100", value: "#ecf7ec" },
{ id: "green-200", value: "#b3e0b3" },
{ id: "green-400", value: "#60bf69" },
{ id: "green-500", value: "#35a849" },
{ id: "green-700", value: "#2e803a" },
{ id: "green-900", value: "#255a2b" },
{ id: "red-100", value: "#ffece9" },
{ id: "red-200", value: "#ffb4a9" },
{ id: "red-400", value: "#f9645a" },
{ id: "red-500", value: "#e23c3b" },
{ id: "red-700", value: "#ac322f" },
{ id: "red-900", value: "#792823" },
{ id: "amber-100", value: "#fff5e7" },
{ id: "amber-200", value: "#ffd6a1" },
{ id: "amber-400", value: "#fbae40" },
{ id: "amber-500", value: "#e79605" },
{ id: "amber-700", value: "#af7311" },
{ id: "amber-900", value: "#7b5114" },
{ id: "purple-100", value: "#f3ecfd" },
{ id: "purple-200", value: "#cdb4f7" },
{ id: "purple-400", value: "#926ced" },
{ id: "purple-500", value: "#714dda" },
{ id: "purple-700", value: "#583da5" },
{ id: "purple-900", value: "#412d73" },
]).concat(register([
// Batch these here for crossover class support
{ id: "blue-hard", value: swatch("blue-900") },
{ id: "blue-dim", value: swatch("blue-700") },
{ id: "blue-base", value: swatch("blue-500") },
{ id: "blue-lit", value: swatch("blue-400") },
{ id: "blue-dull", value: swatch("blue-400") },
{ id: "blue-soft", value: swatch("blue-200") },
{ id: "blue-pale", value: swatch("blue-100") },
{ id: "green-hard", value: swatch("green-900") },
{ id: "green-dim", value: swatch("green-700") },
{ id: "green-base", value: swatch("green-500") },
{ id: "green-lit", value: swatch("green-400") },
{ id: "green-dull", value: swatch("green-400") },
{ id: "green-soft", value: swatch("green-200") },
{ id: "green-pale", value: swatch("green-100") },
{ id: "red-hard", value: swatch("red-900") },
{ id: "red-dim", value: swatch("red-700") },
{ id: "red-base", value: swatch("red-500") },
{ id: "red-lit", value: swatch("red-400") },
{ id: "red-dull", value: swatch("red-400") },
{ id: "red-soft", value: swatch("red-200") },
{ id: "red-pale", value: swatch("red-100") },
{ id: "amber-hard", value: swatch("amber-900") },
{ id: "amber-dim", value: swatch("amber-700") },
{ id: "amber-base", value: swatch("amber-500") },
{ id: "amber-lit", value: swatch("amber-400") },
{ id: "amber-dull", value: swatch("amber-400") }, /* check hex */
{ id: "amber-soft", value: swatch("amber-200") }, /* check hex */
{ id: "amber-pale", value: swatch("amber-100") },
]));
ui.alias = register([
{ id: "brand-hard", value: swatch("blue-hard") },
{ id: "brand-dim", value: swatch("blue-dim") },
{ id: "brand-base", value: swatch("blue-base") },
{ id: "brand", value: swatch("blue-base") },
]);
grays.alias = register([
{ id: "extra-dark-smoke", value: swatch("carbon") },
{ id: "dark-smoke", value: swatch("smog") },
{ id: "extra-dark-snow", value: swatch("slush") },
{ id: "dark-snow", value: swatch("sleet") },
]);
swatch.swatch = swatch;
swatch.grays = grays;
swatch.shades = shades;
swatch.ui = ui;
swatch.identify = identify;
swatch.nearest = nearest;
swatch.pluck = pluck;
swatch.ideal = ideal;
swatch.ideals = ideals;
module.exports = deepFreeze(swatch);
},{"chroma-js":8,"deep-freeze":9,"unquote":12}],6:[function(require,module,exports){
function replacer(pattern) {
return function(sub) {
return function(rule) {
return rule.replace(pattern, sub)
}
}
}
module.exports = Object.freeze({
swid: replacer(/swid/g),
hue: replacer(/hue/g),
});
},{}],7:[function(require,module,exports){
var vacant
var ssv = require("ssv")
var format = ssv.uniq
var model = cader.prototype
var fuse = via(read)
var bond = via(skim)
function defineSkip(object, key, value) {
Object.defineProperty(object, key, {
value: value
})
}
function defineEnum(object, key, value) {
Object.defineProperty(object, key, {
enumerable: true,
value: value
})
}
function cader() {
if (!(this instanceof cader)) return new cader
defineSkip(this, "hash", Object.create(null))
return this
}
function result(fn) {
return function(solo) {
return fn(this.hash, solo)
}
}
function chain(fn) {
return function(solo) {
fn(this.hash, solo)
return this
}
}
function help(hash) {
hash && console.log(JSON.stringify(hash, null, 2))
hash && console.log()
console.log(model)
console.log()
}
function port(object) {
if (object === vacant) throw new Error("Won't serialize: " + object)
var serial = JSON.stringify(object)
if (serial === vacant) throw new Error("Couldn't serialize: " + object)
return JSON.parse(serial)
}
function save(hash, incoming) {
var fresh = port(incoming)
Object.keys(fresh).forEach(function(key) {
var value = fresh[key]
value = sure(value)
value = format(value)
set(hash, key, value)
})
}
function read(hash, key) {
var value = hash[key]
if (value === vacant) throw new Error("Unsaved atom: " + key)
return value
}
function skim(hash, key) {
var value = hash[key]
if (value === vacant) return key
return value
}
function has(hash, key) {
return hash[key] !== vacant
}
function freeze(hash) {
return Object.freeze(hash)
}
function clone(hash) {
return (new cader).save(hash)
}
function pair(hash, mate) {
return clone(hash).save(mate.port())
}
function sure(value) {
if (typeof value != "string") throw new TypeError("Values must be strings")
return value
}
function set(hash, key, value) {
if (ssv.count(key) !== 1) throw new Error("Key must be a single token")
if (has(hash, key)) throw new Error("Already saved: " + key)
if (Object.isFrozen(hash)) throw new Error("Frozen. Consider: .clone()")
defineEnum(hash, key, sure(value))
}
function fusion(fission, cb) {
var atom = ""
var l = fission.length
var i = 0
while (i < l) atom += " " + cb(fission[i++])
return format(atom)
}
function via(method) {
return function(hash, atoms) {
return fusion(ssv.split(atoms), function(tron) {
return method(hash, tron)
})
}
}
defineEnum(model, "fuse", result(fuse))
defineEnum(model, "clone", result(clone))
defineEnum(model, "freeze", chain(freeze))
defineEnum(model, "bond", result(bond))
defineEnum(model, "has", result(has))
defineEnum(model, "help", chain(help))
defineEnum(model, "pair", result(pair))
defineEnum(model, "port", result(port))
defineEnum(model, "save", chain(save))
Object.seal(model);
module.exports = Object.seal(cader);
},{"ssv":11}],8:[function(require,module,exports){
/**
* @license
*
* chroma.js - JavaScript library for color conversions
*
* Copyright (c) 2011-2017, 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.
*
*/
(function() {
var Color, DEG2RAD, LAB_CONSTANTS, PI, PITHIRD, RAD2DEG, TWOPI, _average_lrgb, _guess_formats, _guess_formats_sorted, _input, _interpolators, abs, atan2, bezier, blend, blend_f, brewer, burn, chroma, clip_rgb, cmyk2rgb, colors, cos, css2rgb, darken, dodge, each, floor, hcg2rgb, hex2rgb, hsi2rgb, hsl2css, hsl2rgb, hsv2rgb, interpolate, interpolate_hsx, interpolate_lab, interpolate_lrgb, interpolate_num, interpolate_rgb, lab2lch, lab2rgb, lab_xyz, lch2lab, lch2rgb, lighten, limit, log, luminance_x, m, max, multiply, normal, num2rgb, overlay, pow, rgb2cmyk, rgb2css, rgb2hcg, rgb2hex, rgb2hsi, rgb2hsl, rgb2hsv, rgb2lab, rgb2lch, rgb2luminance, rgb2num, rgb2temperature, rgb2xyz, rgb_xyz, rnd, root, round, screen, sin, sqrt, temperature2rgb, type, unpack, w3cx11, xyz_lab, xyz_rgb,
slice = [].slice;
type = (function() {
/*
for browser-safe type checking+
ported from jQuery's $.type
*/
var classToType, len, name, o, ref;
classToType = {};
ref = "Boolean Number String Function Array Date RegExp Undefined Null".split(" ");
for (o = 0, len = ref.length; o < len; o++) {
name = ref[o];
classToType["[object " + name + "]"] = name.toLowerCase();
}
return function(obj) {
var strType;
strType = Object.prototype.toString.call(obj);
return classToType[strType] || "object";
};
})();
limit = function(x, min, max) {
if (min == null) {
min = 0;
}
if (max == null) {
max = 1;
}
if (x < min) {
x = min;
}
if (x > max) {
x = max;
}
return x;
};
unpack = function(args) {
if (args.length >= 3) {
return [].slice.call(args);
} else {
return args[0];
}
};
clip_rgb = function(rgb) {
var i, o;
rgb._clipped = false;
rgb._unclipped = rgb.slice(0);
for (i = o = 0; o < 3; i = ++o) {
if (i < 3) {
if (rgb[i] < 0 || rgb[i] > 255) {
rgb._clipped = true;
}
if (rgb[i] < 0) {
rgb[i] = 0;
}
if (rgb[i] > 255) {
rgb[i] = 255;
}
} else if (i === 3) {
if (rgb[i] < 0) {
rgb[i] = 0;
}
if (rgb[i] > 1) {
rgb[i] = 1;
}
}
}
if (!rgb._clipped) {
delete rgb._unclipped;
}
return rgb;
};
PI = Math.PI, round = Math.round, cos = Math.cos, floor = Math.floor, pow = Math.pow, log = Math.log, sin = Math.sin, sqrt = Math.sqrt, atan2 = Math.atan2, max = Math.max, abs = Math.abs;
TWOPI = PI * 2;
PITHIRD = PI / 3;
DEG2RAD = PI / 180;
RAD2DEG = 180 / PI;
chroma = function() {
if (arguments[0] instanceof Color) {
return arguments[0];
}
return (function(func, args, ctor) {
ctor.prototype = func.prototype;
var child = new ctor, result = func.apply(child, args);
return Object(result) === result ? result : child;
})(Color, arguments, function(){});
};
chroma["default"] = chroma;
_interpolators = [];
if ((typeof module !== "undefined" && module !== null) && (module.exports != null)) {
module.exports = chroma;
}
if (typeof define === 'function' && define.amd) {
define([], function() {
return chroma;
});
} else {
root = typeof exports !== "undefined" && exports !== null ? exports : this;
root.chroma = chroma;
}
chroma.version = '1.3.5';
_input = {};
_guess_formats = [];
_guess_formats_sorted = false;
Color = (function() {
function Color() {
var arg, args, chk, len, len1, me, mode, o, w;
me = this;
args = [];
for (o = 0, len = arguments.length; o < len; o++) {
arg = arguments[o];
if (arg != null) {
args.push(arg);
}
}
if (args.length > 1) {
mode = args[args.length - 1];
}
if (_input[mode] != null) {
me._rgb = clip_rgb(_input[mode](unpack(args.slice(0, -1))));
} else {
if (!_guess_formats_sorted) {
_guess_formats = _guess_formats.sort(function(a, b) {
return b.p - a.p;
});
_guess_formats_sorted = true;
}
for (w = 0, len1 = _guess_formats.length; w < len1; w++) {
chk = _guess_formats[w];
mode = chk.test.apply(chk, args);
if (mode) {
break;
}
}
if (mode) {
me._rgb = clip_rgb(_input[mode].apply(_input, args));
}
}
if (me._rgb == null) {
console.warn('unknown format: ' + args);
}
if (me._rgb == null) {
me._rgb = [0, 0, 0];
}
if (me._rgb.length === 3) {
me._rgb.push(1);
}
}
Color.prototype.toString = function() {
return this.hex();
};
Color.prototype.clone = function() {
return chroma(me._rgb);
};
return Color;
})();
chroma._input = _input;
/**
ColorBrewer colors for chroma.js
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.
@preserve
*/
chroma.brewer = brewer = {
OrRd: ['#fff7ec', '#fee8c8', '#fdd49e', '#fdbb84', '#fc8d59', '#ef6548', '#d7301f', '#b30000', '#7f0000'],
PuBu: ['#fff7fb', '#ece7f2', '#d0d1e6', '#a6bddb', '#74a9cf', '#3690c0', '#0570b0', '#045a8d', '#023858'],
BuPu: ['#f7fcfd', '#e0ecf4', '#bfd3e6', '#9ebcda', '#8c96c6', '#8c6bb1', '#88419d', '#810f7c', '#4d004b'],
Oranges: ['#fff5eb', '#fee6ce', '#fdd0a2', '#fdae6b', '#fd8d3c', '#f16913', '#d94801', '#a63603', '#7f2704'],
BuGn: ['#f7fcfd', '#e5f5f9', '#ccece6', '#99d8c9', '#66c2a4', '#41ae76', '#238b45', '#006d2c', '#00441b'],
YlOrBr: ['#ffffe5', '#fff7bc', '#fee391', '#fec44f', '#fe9929', '#ec7014', '#cc4c02', '#993404', '#662506'],
YlGn: ['#ffffe5', '#f7fcb9', '#d9f0a3', '#addd8e', '#78c679', '#41ab5d', '#238443', '#006837', '#004529'],
Reds: ['#fff5f0', '#fee0d2', '#fcbba1', '#fc9272', '#fb6a4a', '#ef3b2c', '#cb181d', '#a50f15', '#67000d'],
RdPu: ['#fff7f3', '#fde0dd', '#fcc5c0', '#fa9fb5', '#f768a1', '#dd3497', '#ae017e', '#7a0177', '#49006a'],
Greens: ['#f7fcf5', '#e5f5e0', '#c7e9c0', '#a1d99b', '#74c476', '#41ab5d', '#238b45', '#006d2c', '#00441b'],
YlGnBu: ['#ffffd9', '#edf8b1', '#c7e9b4', '#7fcdbb', '#41b6c4', '#1d91c0', '#225ea8', '#253494', '#081d58'],
Purples: ['#fcfbfd', '#efedf5', '#dadaeb', '#bcbddc', '#9e9ac8', '#807dba', '#6a51a3', '#54278f', '#3f007d'],
GnBu: ['#f7fcf0', '#e0f3db', '#ccebc5', '#a8ddb5', '#7bccc4', '#4eb3d3', '#2b8cbe', '#0868ac', '#084081'],
Greys: ['#ffffff', '#f0f0f0', '#d9d9d9', '#bdbdbd', '#969696', '#737373', '#525252', '#252525', '#000000'],
YlOrRd: ['#ffffcc', '#ffeda0', '#fed976', '#feb24c', '#fd8d3c', '#fc4e2a', '#e31a1c', '#bd0026', '#800026'],
PuRd: ['#f7f4f9', '#e7e1ef', '#d4b9da', '#c994c7', '#df65b0', '#e7298a', '#ce1256', '#980043', '#67001f'],
Blues: ['#f7fbff', '#deebf7', '#c6dbef', '#9ecae1', '#6baed6', '#4292c6', '#2171b5', '#08519c', '#08306b'],
PuBuGn: ['#fff7fb', '#ece2f0', '#d0d1e6', '#a6bddb', '#67a9cf', '#3690c0', '#02818a', '#016c59', '#014636'],
Viridis: ['#440154', '#482777', '#3f4a8a', '#31678e', '#26838f', '#1f9d8a', '#6cce5a', '#b6de2b', '#fee825'],
Spectral: ['#9e0142', '#d53e4f', '#f46d43', '#fdae61', '#fee08b', '#ffffbf', '#e6f598', '#abdda4', '#66c2a5', '#3288bd', '#5e4fa2'],
RdYlGn: ['#a50026', '#d73027', '#f46d43', '#fdae61', '#fee08b', '#ffffbf', '#d9ef8b', '#a6d96a', '#66bd63', '#1a9850', '#006837'],
RdBu: ['#67001f', '#b2182b', '#d6604d', '#f4a582', '#fddbc7', '#f7f7f7', '#d1e5f0', '#92c5de', '#4393c3', '#2166ac', '#053061'],
PiYG: ['#8e0152', '#c51b7d', '#de77ae', '#f1b6da', '#fde0ef', '#f7f7f7', '#e6f5d0', '#b8e186', '#7fbc41', '#4d9221', '#276419'],
PRGn: ['#40004b', '#762a83', '#9970ab', '#c2a5cf', '#e7d4e8', '#f7f7f7', '#d9f0d3', '#a6dba0', '#5aae61', '#1b7837', '#00441b'],
RdYlBu: ['#a50026', '#d73027', '#f46d43', '#fdae61', '#fee090', '#ffffbf', '#e0f3f8', '#abd9e9', '#74add1', '#4575b4', '#313695'],
BrBG: ['#543005', '#8c510a', '#bf812d', '#dfc27d', '#f6e8c3', '#f5f5f5', '#c7eae5', '#80cdc1', '#35978f', '#01665e', '#003c30'],
RdGy: ['#67001f', '#b2182b', '#d6604d', '#f4a582', '#fddbc7', '#ffffff', '#e0e0e0', '#bababa', '#878787', '#4d4d4d', '#1a1a1a'],
PuOr: ['#7f3b08', '#b35806', '#e08214', '#fdb863', '#fee0b6', '#f7f7f7', '#d8daeb', '#b2abd2', '#8073ac', '#542788', '#2d004b'],
Set2: ['#66c2a5', '#fc8d62', '#8da0cb', '#e78ac3', '#a6d854', '#ffd92f', '#e5c494', '#b3b3b3'],
Accent: ['#7fc97f', '#beaed4', '#fdc086', '#ffff99', '#386cb0', '#f0027f', '#bf5b17', '#666666'],
Set1: ['#e41a1c', '#377eb8', '#4daf4a', '#984ea3', '#ff7f00', '#ffff33', '#a65628', '#f781bf', '#999999'],
Set3: ['#8dd3c7', '#ffffb3', '#bebada', '#fb8072', '#80b1d3', '#fdb462', '#b3de69', '#fccde5', '#d9d9d9', '#bc80bd', '#ccebc5', '#ffed6f'],
Dark2: ['#1b9e77', '#d95f02', '#7570b3', '#e7298a', '#66a61e', '#e6ab02', '#a6761d', '#666666'],
Paired: ['#a6cee3', '#1f78b4', '#b2df8a', '#33a02c', '#fb9a99', '#e31a1c', '#fdbf6f', '#ff7f00', '#cab2d6', '#6a3d9a', '#ffff99', '#b15928'],
Pastel2: ['#b3e2cd', '#fdcdac', '#cbd5e8', '#f4cae4', '#e6f5c9', '#fff2ae', '#f1e2cc', '#cccccc'],
Pastel1: ['#fbb4ae', '#b3cde3', '#ccebc5', '#decbe4', '#fed9a6', '#ffffcc', '#e5d8bd', '#fddaec', '#f2f2f2']
};
(function() {
var key, results;
results = [];
for (key in brewer) {
results.push(brewer[key.toLowerCase()] = brewer[key]);
}
return results;
})();
/**
X11 color names
http://www.w3.org/TR/css3-color/#svg-color
*/
w3cx11 = {
aliceblue: '#f0f8ff',
antiquewhite: '#faebd7',
aqua: '#00ffff',
aquamarine: '#7fffd4',
azure: '#f0ffff',
beige: '#f5f5dc',
bisque: '#ffe4c4',
black: '#000000',
blanchedalmond: '#ffebcd',
blue: '#0000ff',
blueviolet: '#8a2be2',
brown: '#a52a2a',
burlywood: '#deb887',
cadetblue: '#5f9ea0',
chartreuse: '#7fff00',
chocolate: '#d2691e',
coral: '#ff7f50',
cornflower: '#6495ed',
cornflowerblue: '#6495ed',
cornsilk: '#fff8dc',
crimson: '#dc143c',
cyan: '#00ffff',
darkblue: '#00008b',
darkcyan: '#008b8b',
darkgoldenrod: '#b8860b',
darkgray: '#a9a9a9',
darkgreen: '#006400',
darkgrey: '#a9a9a9',
darkkhaki: '#bdb76b',
darkmagenta: '#8b008b',
darkolivegreen: '#556b2f',
darkorange: '#ff8c00',
darkorchid: '#9932cc',
darkred: '#8b0000',
darksalmon: '#e9967a',
darkseagreen: '#8fbc8f',
darkslateblue: '#483d8b',
darkslategray: '#2f4f4f',
darkslategrey: '#2f4f4f',
darkturquoise: '#00ced1',
darkviolet: '#9400d3',
deeppink: '#ff1493',
deepskyblue: '#00bfff',
dimgray: '#696969',
dimgrey: '#696969',
dodgerblue: '#1e90ff',
firebrick: '#b22222',
floralwhite: '#fffaf0',
forestgreen: '#228b22',
fuchsia: '#ff00ff',
gainsboro: '#dcdcdc',
ghostwhite: '#f8f8ff',
gold: '#ffd700',
goldenrod: '#daa520',
gray: '#808080',
green: '#008000',
greenyellow: '#adff2f',
grey: '#808080',
honeydew: '#f0fff0',
hotpink: '#ff69b4',
indianred: '#cd5c5c',
indigo: '#4b0082',
ivory: '#fffff0',
khaki: '#f0e68c',
laserlemon: '#ffff54',
lavender: '#e6e6fa',
lavenderblush: '#fff0f5',
lawngreen: '#7cfc00',
lemonchiffon: '#fffacd',
lightblue: '#add8e6',
lightcoral: '#f08080',
lightcyan: '#e0ffff',
lightgoldenrod: '#fafad2',
lightgoldenrodyellow: '#fafad2',
lightgray: '#d3d3d3',
lightgreen: '#90ee90',
lightgrey: '#d3d3d3',
lightpink: '#ffb6c1',
lightsalmon: '#ffa07a',
lightseagreen: '#20b2aa',
lightskyblue: '#87cefa',
lightslategray: '#778899',
lightslategrey: '#778899',
lightsteelblue: '#b0c4de',
lightyellow: '#ffffe0',
lime: '#00ff00',
limegreen: '#32cd32',
linen: '#faf0e6',
magenta: '#ff00ff',
maroon: '#800000',
maroon2: '#7f0000',
maroon3: '#b03060',
mediumaquamarine: '#66cdaa',
mediumblue: '#0000cd',
mediumorchid: '#ba55d3',
mediumpurple: '#9370db',
mediumseagreen: '#3cb371',
mediumslateblue: '#7b68ee',
mediumspringgreen: '#00fa9a',
mediumturquoise: '#48d1cc',
mediumvioletred: '#c71585',
midnightblue: '#191970',
mintcream: '#f5fffa',
mistyrose: '#ffe4e1',
moccasin: '#ffe4b5',
navajowhite: '#ffdead',
navy: '#000080',
oldlace: '#fdf5e6',
olive: '#808000',
olivedrab: '#6b8e23',
orange: '#ffa500',
orangered: '#ff4500',
orchid: '#da70d6',
palegoldenrod: '#eee8aa',
palegreen: '#98fb98',
paleturquoise: '#afeeee',
palevioletred: '#db7093',
papayawhip: '#ffefd5',
peachpuff: '#ffdab9',
peru: '#cd853f',
pink: '#ffc0cb',
plum: '#dda0dd',
powderblue: '#b0e0e6',
purple: '#800080',
purple2: '#7f007f',
purple3: '#a020f0',
rebeccapurple: '#663399',
red: '#ff0000',
rosybrown: '#bc8f8f',
royalblue: '#4169e1',
saddlebrown: '#8b4513',
salmon: '#fa8072',
sandybrown: '#f4a460',
seagreen: '#2e8b57',
seashell: '#fff5ee',
sienna: '#a0522d',
silver: '#c0c0c0',
skyblue: '#87ceeb',
slateblue: '#6a5acd',
slategray: '#708090',
slategrey: '#708090',
snow: '#fffafa',
springgreen: '#00ff7f',
steelblue: '#4682b4',
tan: '#d2b48c',
teal: '#008080',
thistle: '#d8bfd8',
tomato: '#ff6347',
turquoise: '#40e0d0',
violet: '#ee82ee',
wheat: '#f5deb3',
white: '#ffffff',
whitesmoke: '#f5f5f5',
yellow: '#ffff00',
yellowgreen: '#9acd32'
};
chroma.colors = colors = w3cx11;
lab2rgb = function() {
var a, args, b, g, l, r, x, y, z;
args = unpack(arguments);
l = args[0], a = args[1], b = args[2];
y = (l + 16) / 116;
x = isNaN(a) ? y : y + a / 500;
z = isNaN(b) ? y : y - b / 200;
y = LAB_CONSTANTS.Yn * lab_xyz(y);
x = LAB_CONSTANTS.Xn * lab_xyz(x);
z = LAB_CONSTANTS.Zn * lab_xyz(z);
r = xyz_rgb(3.2404542 * x - 1.5371385 * y - 0.4985314 * z);
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];
};
xyz_rgb = function(r) {
return 255 * (r <= 0.00304 ? 12.92 * r : 1.055 * pow(r, 1 / 2.4) - 0.055);
};
lab_xyz = function(t) {
if (t > LAB_CONSTANTS.t1) {
return t * t * t;
} else {
return LAB_CONSTANTS.t2 * (t - LAB_CONSTANTS.t0);
}
};
LAB_CONSTANTS = {
Kn: 18,
Xn: 0.950470,
Yn: 1,
Zn: 1.088830,
t0: 0.137931034,
t1: 0.206896552,
t2: 0.12841855,
t3: 0.008856452
};
rgb2lab = function() {
var b, g, r, ref, ref1, x, y, z;
ref = unpack(arguments), r = ref[0], g = ref[1], b = ref[2];
ref1 = rgb2xyz(r, g, b), x = ref1[0], y = ref1[1], z = ref1[2];
return [116 * y - 16, 500 * (x - y), 200 * (y - z)];
};
rgb_xyz = function(r) {
if ((r /= 255) <= 0.04045) {
return r / 12.92;
} else {
return pow((r + 0.055) / 1.055, 2.4);
}
};
xyz_lab = function(t) {
if (t > LAB_CONSTANTS.t3) {
return pow(t, 1 / 3);
} else {
return t / LAB_CONSTANTS.t2 + LAB_CONSTANTS.t0;
}
};
rgb2xyz = function() {
var b, g, r, ref, x, y, z;
ref = unpack(arguments), r = ref[0], g = ref[1], b = ref[2];
r = rgb_xyz(r);
g = rgb_xyz(g);
b = rgb_xyz(b);
x = xyz_lab((0.4124564 * r + 0.3575761 * g + 0.1804375 * b) / LAB_CONSTANTS.Xn);
y = xyz_lab((0.2126729 * r + 0.7151522 * g + 0.0721750 * b) / LAB_CONSTANTS.Yn);
z = xyz_lab((0.0193339 * r + 0.1191920 * g + 0.9503041 * b) / LAB_CONSTANTS.Zn);
return [x, y, z];
};
chroma.lab = function() {
return (function(func, args, ctor) {
ctor.prototype = func.prototype;
var child = new ctor, result = func.apply(child, args);
return Object(result) === result ? result : child;
})(Color, slice.call(arguments).concat(['lab']), function(){});
};
_input.lab = lab2rgb;
Color.prototype.lab = function() {
return rgb2lab(this._rgb);
};
bezier = function(colors) {
var I, I0, I1, c, lab0, lab1, lab2, lab3, ref, ref1, ref2;
colors = (function() {
var len, o, results;
results = [];
for (o = 0, len = colors.length; o < len; o++) {
c = colors[o];
results.push(chroma(c));
}
return results;
})();
if (colors.length === 2) {
ref = (function() {
var len, o, results;
results = [];
for (o = 0, len = colors.length; o < len; o++) {
c = colors[o];
results.push(c.lab());
}
return results;
})(), lab0 = ref[0], lab1 = ref[1];
I = function(t) {
var i, lab;
lab = (function() {
var o, results;
results = [];
for (i = o = 0; o <= 2; i = ++o) {
results.push(lab0[i] + t * (lab1[i] - lab0[i]));
}
return results;
})();
return chroma.lab.apply(chroma, lab);
};
} else if (colors.length === 3) {
ref1 = (function() {
var len, o, results;
results = [];
for (o = 0, len = colors.length; o < len; o++) {
c = colors[o];
results.push(c.lab());
}
return results;
})(), lab0 = ref1[0], lab1 = ref1[1], lab2 = ref1[2];
I = function(t) {
var i, lab;
lab = (function() {
var o, results;
results = [];
for (i = o = 0; o <= 2; i = ++o) {
results.push((1 - t) * (1 - t) * lab0[i] + 2 * (1 - t) * t * lab1[i] + t * t * lab2[i]);
}
return results;
})();
return chroma.lab.apply(chroma, lab);
};
} else if (colors.length === 4) {
ref2 = (function() {
var len, o, results;
results = [];
for (o = 0, len = colors.length; o < len; o++) {
c = colors[o];
results.push(c.lab());
}
return results;
})(), lab0 = ref2[0], lab1 = ref2[1], lab2 = ref2[2], lab3 = ref2[3];
I = function(t) {
var i, lab;
lab = (function() {
var o, results;
results = [];
for (i = o = 0; o <= 2; i = ++o) {
results.push((1 - t) * (1 - t) * (1 - t) * lab0[i] + 3 * (1 - t) * (1 - t) * t * lab1[i] + 3 * (1 - t) * t * t * lab2[i] + t * t * t * lab3[i]);
}
return results;
})();
return chroma.lab.apply(chroma, lab);
};
} else if (colors.length === 5) {
I0 = bezier(colors.slice(0, 3));
I1 = bezier(colors.slice(2, 5));
I = function(t) {
if (t < 0.5) {
return I0(t * 2);
} else {
return I1((t - 0.5) * 2);
}
};
}
return I;
};
chroma.bezier = function(colors) {
var f;
f = bezier(colors);
f.scale = function() {
return chroma.scale(f);
};
return f;
};
/*
chroma.js
Copyright (c) 2011-2013, 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:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* 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.
* 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.
@source: https://github.com/gka/chroma.js
*/
chroma.cubehelix = function(start, rotations, hue, gamma, lightness) {
var dh, dl, f;
if (start == null) {
start = 300;
}
if (rotations == null) {
rotations = -1.5;
}
if (hue == null) {
hue = 1;
}
if (gamma == null) {
gamma = 1;
}
if (lightness == null) {
lightness = [0, 1];
}
dh = 0;
if (type(lightness) === 'array') {
dl = lightness[1] - lightness[0];
} else {
dl = 0;
lightness = [lightness, lightness];
}
f = function(fract) {
var a, amp, b, cos_a, g, h, l, r, sin_a;
a = TWOPI * ((start + 120) / 360 + rotations * fract);
l = pow(lightness[0] + dl * fract, gamma);
h = dh !== 0 ? hue[0] + fract * dh : hue;
amp = h * l * (1 - l) / 2;
cos_a = cos(a);
sin_a = sin(a);
r = l + amp * (-0.14861 * cos_a + 1.78277 * sin_a);
g = l + amp * (-0.29227 * cos_a - 0.90649 * sin_a);
b = l + amp * (+1.97294 * cos_a);
return chroma(clip_rgb([r * 255, g * 255, b * 255]));
};
f.start = function(s) {
if (s == null) {
return start;
}
start = s;
return f;
};
f.rotations = function(r) {
if (r == null) {
return rotations;
}
rotations = r;
return f;
};
f.gamma = function(g) {
if (g == null) {
return gamma;
}
gamma = g;
return f;
};
f.hue = function(h) {
if (h == null) {
return hue;
}
hue = h;
if (type(hue) === 'array') {
dh = hue[1] - hue[0];
if (dh === 0) {
hue = hue[1];
}
} else {
dh = 0;
}
return f;
};
f.lightness = function(h) {
if (h == null) {
return lightness;
}
if (type(h) === 'array') {
lightness = h;
dl = h[1] - h[0];
} else {
lightness = [h, h];
dl = 0;
}
return f;
};
f.scale = function() {
return chroma.scale(f);
};
f.hue(hue);
return f;
};
chroma.random = function() {
var code, digits, i, o;
digits = '0123456789abcdef';
code = '#';
for (i = o = 0; o < 6; i = ++o) {
code += digits.charAt(floor(Math.random() * 16));
}
return new Color(code);
};
_interpolators = [];
interpolate = function(col1, col2, f, m) {
var interpol, len, o, res;
if (f == null) {
f = 0.5;
}
if (m == null) {
m = 'rgb';
}
/*
interpolates between colors
f = 0 --> me
f = 1 --> col
*/
if (type(col1) !== 'object') {
col1 = chroma(col1);
}
if (type(col2) !== 'object') {
col2 = chroma(col2);
}
for (o = 0, len = _interpolators.length; o < len; o++) {
interpol = _interpolators[o];
if (m === interpol[0]) {
res = interpol[1](col1, col2, f, m);
break;
}
}
if (res == null) {
throw "color mode " + m + " is not supported";
}
return res.alpha(col1.alpha() + f * (col2.alpha() - col1.alpha()));
};
chroma.interpolate = interpolate;
Color.prototype.interpolate = function(col2, f, m) {
return interpolate(this, col2, f, m);
};
chroma.mix = interpolate;
Color.prototype.mix = Color.prototype.interpolate;
_input.rgb = function() {
var k, ref, results, v;
ref = unpack(arguments);
results = [];
for (k in ref) {
v = ref[k];
results.push(v);
}
return results;
};
chroma.rgb = function() {
return (function(func, args, ctor) {
ctor.prototype = func.prototype;
var child = new ctor, result = func.apply(child, args);
return Object(result) === result ? result : child;
})(Color, slice.call(arguments).concat(['rgb']), function(){});
};
Color.prototype.rgb = function(round) {
if (round == null) {
round = true;
}
if (round) {
return this._rgb.map(Math.round).slice(0, 3);
} else {
return this._rgb.slice(0, 3);
}
};
Color.prototype.rgba = function(round) {
if (round == null) {
round = true;
}
if (!round) {
return this._rgb.slice(0);
}
return [Math.round(this._rgb[0]), Math.round(this._rgb[1]), Math.round(this._rgb[2]), this._rgb[3]];
};
_guess_formats.push({
p: 3,
test: function(n) {
var a;
a = unpack(arguments);
if (type(a) === 'array' && a.length === 3) {
return 'rgb';
}
if (a.length === 4 && type(a[3]) === "number" && a[3] >= 0 && a[3] <= 1) {
return 'rgb';
}
}
});
_input.lrgb = _input.rgb;
interpolate_lrgb = function(col1, col2, f, m) {
var xyz0, xyz1;
xyz0 = col1._rgb;
xyz1 = col2._rgb;
return new Color(sqrt(pow(xyz0[0], 2) * (1 - f) + pow(xyz1[0], 2) * f), sqrt(pow(xyz0[1], 2) * (1 - f) + pow(xyz1[1], 2) * f), sqrt(pow(xyz0[2], 2) * (1 - f) + pow(xyz1[2], 2) * f), m);
};
_average_lrgb = function(colors) {
var col, f, len, o, rgb, xyz;
f = 1 / colors.length;
xyz = [0, 0, 0, 0];
for (o = 0, len = colors.length; o < len; o++) {
col = colors[o];
rgb = col._rgb;
xyz[0] += pow(rgb[0], 2) * f;
xyz[1] += pow(rgb[1], 2) * f;
xyz[2] += pow(rgb[2], 2) * f;
xyz[3] += rgb[3] * f;
}
xyz[0] = sqrt(xyz[0]);
xyz[1] = sqrt(xyz[1]);
xyz[2] = sqrt(xyz[2]);
return new Color(xyz);
};
_interpolators.push(['lrgb', interpolate_lrgb]);
chroma.average = function(colors, mode) {
var A, alpha, c, cnt, dx, dy, first, i, l, len, o, xyz, xyz2;
if (mode == null) {
mode = 'rgb';
}
l = colors.length;
colors = colors.map(function(c) {
return chroma(c);
});
first = colors.splice(0, 1)[0];
if (mode === 'lrgb') {
return _average_lrgb(colors);
}
xyz = first.get(mode);
cnt = [];
dx = 0;
dy = 0;
for (i in xyz) {
xyz[i] = xyz[i] || 0;
cnt.push(!isNaN(xyz[i]) ? 1 : 0);
if (mode.charAt(i) === 'h' && !isNaN(xyz[i])) {
A = xyz[i] / 180 * PI;
dx += cos(A);
dy += sin(A);
}
}
alpha = first.alpha();
for (o = 0, len = colors.length; o < len; o++) {
c = colors[o];
xyz2 = c.get(mode);
alpha += c.alpha();
for (i in xyz) {
if (!isNaN(xyz2[i])) {
xyz[i] += xyz2[i];
cnt[i] += 1;
if (mode.charAt(i) === 'h') {
A = xyz[i] / 180 * PI;
dx += cos(A);
dy += sin(A);
}
}
}
}
for (i in xyz) {
xyz[i] = xyz[i] / cnt[i];
if (mode.charAt(i) === 'h') {
A = atan2(dy / cnt[i], dx / cnt[i]) / PI * 180;
while (A < 0) {
A += 360;
}
while (A >= 360) {
A -= 360;
}
xyz[i] = A;
}
}
return chroma(xyz, mode).alpha(alpha / l);
};
hex2rgb = function(hex) {
var a, b, g, r, rgb, u;
if (hex.match(/^#?([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/)) {
if (hex.length === 4 || hex.length === 7) {
hex = hex.substr(1);
}
if (hex.length === 3) {
hex = hex.split("");
hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
}
u = parseInt(hex, 16);
r = u >> 16;
g = u >> 8 & 0xFF;
b = u & 0xFF;
return [r, g, b, 1];
}
if (hex.match(/^#?([A-Fa-f0-9]{8})$/)) {
if (hex.length === 9) {
hex = hex.substr(1);
}
u = parseInt(hex, 16);
r = u >> 24 & 0xFF;
g = u >> 16 & 0xFF;
b = u >> 8 & 0xFF;
a = round((u & 0xFF) / 0xFF * 100) / 100;
return [r, g, b, a];
}
if ((_input.css != null) && (rgb = _input.css(hex))) {
return rgb;
}
throw "unknown color: " + hex;
};
rgb2hex = function(channels, mode) {
var a, b, g, hxa, r, str, u;
if (mode == null) {
mode = 'rgb';
}
r = channels[0], g = channels[1], b = channels[2], a = channels[3];
r = Math.round(r);
g = Math.round(g);
b = Math.round(b);
u = r << 16 | g << 8 | b;
str = "000000" + u.toString(16);
str = str.substr(str.length - 6);
hxa = '0' + round(a * 255).toString(16);
hxa = hxa.substr(hxa.length - 2);
return "#" + (function() {
switch (mode.toLowerCase()) {
case 'rgba':
return str + hxa;
case 'argb':
return hxa + str;
default:
return str;
}
})();
};
_input.hex = function(h) {
return hex2rgb(h);
};
chroma.hex = function() {
return (function(func, args, ctor) {
ctor.prototype = func.prototype;
var child = new ctor, result = func.apply(child, args);
return Object(result) === result ? result : child;
})(Color, slice.call(arguments).concat(['hex']), function(){});
};
Color.prototype.hex = function(mode) {
if (mode == null) {
mode = 'rgb';
}
return rgb2hex(this._rgb, mode);
};
_guess_formats.push({
p: 4,
test: function(n) {
if (arguments.length === 1 && type(n) === "string") {
return 'hex';
}
}
});
hsl2rgb = function() {
var args, b, c, g, h, i, l, o, r, ref, s, t1, t2, t3;
args = unpack(arguments);
h = args[0], s = args[1], l = args[2];
if (s === 0) {
r = g = b = l * 255;
} else {
t3 = [0, 0, 0];
c = [0, 0, 0];
t2 = l < 0.5 ? l * (1 + s) : l + s - l * s;
t1 = 2 * l - t2;
h /= 360;
t3[0] = h + 1 / 3;
t3[1] = h;
t3[2] = h - 1 / 3;
for (i = o = 0; o <= 2; i = ++o) {
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;
}
}
ref = [roun