Kromek Radangel gamma spectrometer USB HID daemon and WebUI. https://git.unino.de/pvivell/radangel
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

chroma.js 75KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733
  1. /**
  2. * @license
  3. *
  4. * chroma.js - JavaScript library for color conversions
  5. *
  6. * Copyright (c) 2011-2017, Gregor Aisch
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice, this
  13. * list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * 3. The name Gregor Aisch may not be used to endorse or promote products
  20. * derived from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL GREGOR AISCH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  26. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  29. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  31. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. */
  34. (function() {
  35. 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,
  36. slice = [].slice;
  37. type = (function() {
  38. /*
  39. for browser-safe type checking+
  40. ported from jQuery's $.type
  41. */
  42. var classToType, len, name, o, ref;
  43. classToType = {};
  44. ref = "Boolean Number String Function Array Date RegExp Undefined Null".split(" ");
  45. for (o = 0, len = ref.length; o < len; o++) {
  46. name = ref[o];
  47. classToType["[object " + name + "]"] = name.toLowerCase();
  48. }
  49. return function(obj) {
  50. var strType;
  51. strType = Object.prototype.toString.call(obj);
  52. return classToType[strType] || "object";
  53. };
  54. })();
  55. limit = function(x, min, max) {
  56. if (min == null) {
  57. min = 0;
  58. }
  59. if (max == null) {
  60. max = 1;
  61. }
  62. if (x < min) {
  63. x = min;
  64. }
  65. if (x > max) {
  66. x = max;
  67. }
  68. return x;
  69. };
  70. unpack = function(args) {
  71. if (args.length >= 3) {
  72. return Array.prototype.slice.call(args);
  73. } else {
  74. return args[0];
  75. }
  76. };
  77. clip_rgb = function(rgb) {
  78. var i, o;
  79. rgb._clipped = false;
  80. rgb._unclipped = rgb.slice(0);
  81. for (i = o = 0; o < 3; i = ++o) {
  82. if (i < 3) {
  83. if (rgb[i] < 0 || rgb[i] > 255) {
  84. rgb._clipped = true;
  85. }
  86. if (rgb[i] < 0) {
  87. rgb[i] = 0;
  88. }
  89. if (rgb[i] > 255) {
  90. rgb[i] = 255;
  91. }
  92. } else if (i === 3) {
  93. if (rgb[i] < 0) {
  94. rgb[i] = 0;
  95. }
  96. if (rgb[i] > 1) {
  97. rgb[i] = 1;
  98. }
  99. }
  100. }
  101. if (!rgb._clipped) {
  102. delete rgb._unclipped;
  103. }
  104. return rgb;
  105. };
  106. 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;
  107. TWOPI = PI * 2;
  108. PITHIRD = PI / 3;
  109. DEG2RAD = PI / 180;
  110. RAD2DEG = 180 / PI;
  111. chroma = function() {
  112. if (arguments[0] instanceof Color) {
  113. return arguments[0];
  114. }
  115. return (function(func, args, ctor) {
  116. ctor.prototype = func.prototype;
  117. var child = new ctor, result = func.apply(child, args);
  118. return Object(result) === result ? result : child;
  119. })(Color, arguments, function(){});
  120. };
  121. chroma["default"] = chroma;
  122. _interpolators = [];
  123. if ((typeof module !== "undefined" && module !== null) && (module.exports != null)) {
  124. module.exports = chroma;
  125. }
  126. if (typeof define === 'function' && define.amd) {
  127. define([], function() {
  128. return chroma;
  129. });
  130. } else {
  131. root = typeof exports !== "undefined" && exports !== null ? exports : this;
  132. root.chroma = chroma;
  133. }
  134. chroma.version = '1.4.0';
  135. _input = {};
  136. _guess_formats = [];
  137. _guess_formats_sorted = false;
  138. Color = (function() {
  139. function Color() {
  140. var arg, args, chk, len, len1, me, mode, o, w;
  141. me = this;
  142. args = [];
  143. for (o = 0, len = arguments.length; o < len; o++) {
  144. arg = arguments[o];
  145. if (arg != null) {
  146. args.push(arg);
  147. }
  148. }
  149. if (args.length > 1) {
  150. mode = args[args.length - 1];
  151. }
  152. if (_input[mode] != null) {
  153. me._rgb = clip_rgb(_input[mode](unpack(args.slice(0, -1))));
  154. } else {
  155. if (!_guess_formats_sorted) {
  156. _guess_formats = _guess_formats.sort(function(a, b) {
  157. return b.p - a.p;
  158. });
  159. _guess_formats_sorted = true;
  160. }
  161. for (w = 0, len1 = _guess_formats.length; w < len1; w++) {
  162. chk = _guess_formats[w];
  163. mode = chk.test.apply(chk, args);
  164. if (mode) {
  165. break;
  166. }
  167. }
  168. if (mode) {
  169. me._rgb = clip_rgb(_input[mode].apply(_input, args));
  170. }
  171. }
  172. if (me._rgb == null) {
  173. console.warn('unknown format: ' + args);
  174. }
  175. if (me._rgb == null) {
  176. me._rgb = [0, 0, 0];
  177. }
  178. if (me._rgb.length === 3) {
  179. me._rgb.push(1);
  180. }
  181. }
  182. Color.prototype.toString = function() {
  183. return this.hex();
  184. };
  185. return Color;
  186. })();
  187. chroma._input = _input;
  188. /**
  189. ColorBrewer colors for chroma.js
  190. Copyright (c) 2002 Cynthia Brewer, Mark Harrower, and The
  191. Pennsylvania State University.
  192. Licensed under the Apache License, Version 2.0 (the "License");
  193. you may not use this file except in compliance with the License.
  194. You may obtain a copy of the License at
  195. http://www.apache.org/licenses/LICENSE-2.0
  196. Unless required by applicable law or agreed to in writing, software distributed
  197. under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  198. CONDITIONS OF ANY KIND, either express or implied. See the License for the
  199. specific language governing permissions and limitations under the License.
  200. @preserve
  201. */
  202. chroma.brewer = brewer = {
  203. OrRd: ['#fff7ec', '#fee8c8', '#fdd49e', '#fdbb84', '#fc8d59', '#ef6548', '#d7301f', '#b30000', '#7f0000'],
  204. PuBu: ['#fff7fb', '#ece7f2', '#d0d1e6', '#a6bddb', '#74a9cf', '#3690c0', '#0570b0', '#045a8d', '#023858'],
  205. BuPu: ['#f7fcfd', '#e0ecf4', '#bfd3e6', '#9ebcda', '#8c96c6', '#8c6bb1', '#88419d', '#810f7c', '#4d004b'],
  206. Oranges: ['#fff5eb', '#fee6ce', '#fdd0a2', '#fdae6b', '#fd8d3c', '#f16913', '#d94801', '#a63603', '#7f2704'],
  207. BuGn: ['#f7fcfd', '#e5f5f9', '#ccece6', '#99d8c9', '#66c2a4', '#41ae76', '#238b45', '#006d2c', '#00441b'],
  208. YlOrBr: ['#ffffe5', '#fff7bc', '#fee391', '#fec44f', '#fe9929', '#ec7014', '#cc4c02', '#993404', '#662506'],
  209. YlGn: ['#ffffe5', '#f7fcb9', '#d9f0a3', '#addd8e', '#78c679', '#41ab5d', '#238443', '#006837', '#004529'],
  210. Reds: ['#fff5f0', '#fee0d2', '#fcbba1', '#fc9272', '#fb6a4a', '#ef3b2c', '#cb181d', '#a50f15', '#67000d'],
  211. RdPu: ['#fff7f3', '#fde0dd', '#fcc5c0', '#fa9fb5', '#f768a1', '#dd3497', '#ae017e', '#7a0177', '#49006a'],
  212. Greens: ['#f7fcf5', '#e5f5e0', '#c7e9c0', '#a1d99b', '#74c476', '#41ab5d', '#238b45', '#006d2c', '#00441b'],
  213. YlGnBu: ['#ffffd9', '#edf8b1', '#c7e9b4', '#7fcdbb', '#41b6c4', '#1d91c0', '#225ea8', '#253494', '#081d58'],
  214. Purples: ['#fcfbfd', '#efedf5', '#dadaeb', '#bcbddc', '#9e9ac8', '#807dba', '#6a51a3', '#54278f', '#3f007d'],
  215. GnBu: ['#f7fcf0', '#e0f3db', '#ccebc5', '#a8ddb5', '#7bccc4', '#4eb3d3', '#2b8cbe', '#0868ac', '#084081'],
  216. Greys: ['#ffffff', '#f0f0f0', '#d9d9d9', '#bdbdbd', '#969696', '#737373', '#525252', '#252525', '#000000'],
  217. YlOrRd: ['#ffffcc', '#ffeda0', '#fed976', '#feb24c', '#fd8d3c', '#fc4e2a', '#e31a1c', '#bd0026', '#800026'],
  218. PuRd: ['#f7f4f9', '#e7e1ef', '#d4b9da', '#c994c7', '#df65b0', '#e7298a', '#ce1256', '#980043', '#67001f'],
  219. Blues: ['#f7fbff', '#deebf7', '#c6dbef', '#9ecae1', '#6baed6', '#4292c6', '#2171b5', '#08519c', '#08306b'],
  220. PuBuGn: ['#fff7fb', '#ece2f0', '#d0d1e6', '#a6bddb', '#67a9cf', '#3690c0', '#02818a', '#016c59', '#014636'],
  221. Viridis: ['#440154', '#482777', '#3f4a8a', '#31678e', '#26838f', '#1f9d8a', '#6cce5a', '#b6de2b', '#fee825'],
  222. Spectral: ['#9e0142', '#d53e4f', '#f46d43', '#fdae61', '#fee08b', '#ffffbf', '#e6f598', '#abdda4', '#66c2a5', '#3288bd', '#5e4fa2'],
  223. RdYlGn: ['#a50026', '#d73027', '#f46d43', '#fdae61', '#fee08b', '#ffffbf', '#d9ef8b', '#a6d96a', '#66bd63', '#1a9850', '#006837'],
  224. RdBu: ['#67001f', '#b2182b', '#d6604d', '#f4a582', '#fddbc7', '#f7f7f7', '#d1e5f0', '#92c5de', '#4393c3', '#2166ac', '#053061'],
  225. PiYG: ['#8e0152', '#c51b7d', '#de77ae', '#f1b6da', '#fde0ef', '#f7f7f7', '#e6f5d0', '#b8e186', '#7fbc41', '#4d9221', '#276419'],
  226. PRGn: ['#40004b', '#762a83', '#9970ab', '#c2a5cf', '#e7d4e8', '#f7f7f7', '#d9f0d3', '#a6dba0', '#5aae61', '#1b7837', '#00441b'],
  227. RdYlBu: ['#a50026', '#d73027', '#f46d43', '#fdae61', '#fee090', '#ffffbf', '#e0f3f8', '#abd9e9', '#74add1', '#4575b4', '#313695'],
  228. BrBG: ['#543005', '#8c510a', '#bf812d', '#dfc27d', '#f6e8c3', '#f5f5f5', '#c7eae5', '#80cdc1', '#35978f', '#01665e', '#003c30'],
  229. RdGy: ['#67001f', '#b2182b', '#d6604d', '#f4a582', '#fddbc7', '#ffffff', '#e0e0e0', '#bababa', '#878787', '#4d4d4d', '#1a1a1a'],
  230. PuOr: ['#7f3b08', '#b35806', '#e08214', '#fdb863', '#fee0b6', '#f7f7f7', '#d8daeb', '#b2abd2', '#8073ac', '#542788', '#2d004b'],
  231. Set2: ['#66c2a5', '#fc8d62', '#8da0cb', '#e78ac3', '#a6d854', '#ffd92f', '#e5c494', '#b3b3b3'],
  232. Accent: ['#7fc97f', '#beaed4', '#fdc086', '#ffff99', '#386cb0', '#f0027f', '#bf5b17', '#666666'],
  233. Set1: ['#e41a1c', '#377eb8', '#4daf4a', '#984ea3', '#ff7f00', '#ffff33', '#a65628', '#f781bf', '#999999'],
  234. Set3: ['#8dd3c7', '#ffffb3', '#bebada', '#fb8072', '#80b1d3', '#fdb462', '#b3de69', '#fccde5', '#d9d9d9', '#bc80bd', '#ccebc5', '#ffed6f'],
  235. Dark2: ['#1b9e77', '#d95f02', '#7570b3', '#e7298a', '#66a61e', '#e6ab02', '#a6761d', '#666666'],
  236. Paired: ['#a6cee3', '#1f78b4', '#b2df8a', '#33a02c', '#fb9a99', '#e31a1c', '#fdbf6f', '#ff7f00', '#cab2d6', '#6a3d9a', '#ffff99', '#b15928'],
  237. Pastel2: ['#b3e2cd', '#fdcdac', '#cbd5e8', '#f4cae4', '#e6f5c9', '#fff2ae', '#f1e2cc', '#cccccc'],
  238. Pastel1: ['#fbb4ae', '#b3cde3', '#ccebc5', '#decbe4', '#fed9a6', '#ffffcc', '#e5d8bd', '#fddaec', '#f2f2f2']
  239. };
  240. (function() {
  241. var key, results;
  242. results = [];
  243. for (key in brewer) {
  244. results.push(brewer[key.toLowerCase()] = brewer[key]);
  245. }
  246. return results;
  247. })();
  248. /**
  249. X11 color names
  250. http://www.w3.org/TR/css3-color/#svg-color
  251. */
  252. w3cx11 = {
  253. aliceblue: '#f0f8ff',
  254. antiquewhite: '#faebd7',
  255. aqua: '#00ffff',
  256. aquamarine: '#7fffd4',
  257. azure: '#f0ffff',
  258. beige: '#f5f5dc',
  259. bisque: '#ffe4c4',
  260. black: '#000000',
  261. blanchedalmond: '#ffebcd',
  262. blue: '#0000ff',
  263. blueviolet: '#8a2be2',
  264. brown: '#a52a2a',
  265. burlywood: '#deb887',
  266. cadetblue: '#5f9ea0',
  267. chartreuse: '#7fff00',
  268. chocolate: '#d2691e',
  269. coral: '#ff7f50',
  270. cornflower: '#6495ed',
  271. cornflowerblue: '#6495ed',
  272. cornsilk: '#fff8dc',
  273. crimson: '#dc143c',
  274. cyan: '#00ffff',
  275. darkblue: '#00008b',
  276. darkcyan: '#008b8b',
  277. darkgoldenrod: '#b8860b',
  278. darkgray: '#a9a9a9',
  279. darkgreen: '#006400',
  280. darkgrey: '#a9a9a9',
  281. darkkhaki: '#bdb76b',
  282. darkmagenta: '#8b008b',
  283. darkolivegreen: '#556b2f',
  284. darkorange: '#ff8c00',
  285. darkorchid: '#9932cc',
  286. darkred: '#8b0000',
  287. darksalmon: '#e9967a',
  288. darkseagreen: '#8fbc8f',
  289. darkslateblue: '#483d8b',
  290. darkslategray: '#2f4f4f',
  291. darkslategrey: '#2f4f4f',
  292. darkturquoise: '#00ced1',
  293. darkviolet: '#9400d3',
  294. deeppink: '#ff1493',
  295. deepskyblue: '#00bfff',
  296. dimgray: '#696969',
  297. dimgrey: '#696969',
  298. dodgerblue: '#1e90ff',
  299. firebrick: '#b22222',
  300. floralwhite: '#fffaf0',
  301. forestgreen: '#228b22',
  302. fuchsia: '#ff00ff',
  303. gainsboro: '#dcdcdc',
  304. ghostwhite: '#f8f8ff',
  305. gold: '#ffd700',
  306. goldenrod: '#daa520',
  307. gray: '#808080',
  308. green: '#008000',
  309. greenyellow: '#adff2f',
  310. grey: '#808080',
  311. honeydew: '#f0fff0',
  312. hotpink: '#ff69b4',
  313. indianred: '#cd5c5c',
  314. indigo: '#4b0082',
  315. ivory: '#fffff0',
  316. khaki: '#f0e68c',
  317. laserlemon: '#ffff54',
  318. lavender: '#e6e6fa',
  319. lavenderblush: '#fff0f5',
  320. lawngreen: '#7cfc00',
  321. lemonchiffon: '#fffacd',
  322. lightblue: '#add8e6',
  323. lightcoral: '#f08080',
  324. lightcyan: '#e0ffff',
  325. lightgoldenrod: '#fafad2',
  326. lightgoldenrodyellow: '#fafad2',
  327. lightgray: '#d3d3d3',
  328. lightgreen: '#90ee90',
  329. lightgrey: '#d3d3d3',
  330. lightpink: '#ffb6c1',
  331. lightsalmon: '#ffa07a',
  332. lightseagreen: '#20b2aa',
  333. lightskyblue: '#87cefa',
  334. lightslategray: '#778899',
  335. lightslategrey: '#778899',
  336. lightsteelblue: '#b0c4de',
  337. lightyellow: '#ffffe0',
  338. lime: '#00ff00',
  339. limegreen: '#32cd32',
  340. linen: '#faf0e6',
  341. magenta: '#ff00ff',
  342. maroon: '#800000',
  343. maroon2: '#7f0000',
  344. maroon3: '#b03060',
  345. mediumaquamarine: '#66cdaa',
  346. mediumblue: '#0000cd',
  347. mediumorchid: '#ba55d3',
  348. mediumpurple: '#9370db',
  349. mediumseagreen: '#3cb371',
  350. mediumslateblue: '#7b68ee',
  351. mediumspringgreen: '#00fa9a',
  352. mediumturquoise: '#48d1cc',
  353. mediumvioletred: '#c71585',
  354. midnightblue: '#191970',
  355. mintcream: '#f5fffa',
  356. mistyrose: '#ffe4e1',
  357. moccasin: '#ffe4b5',
  358. navajowhite: '#ffdead',
  359. navy: '#000080',
  360. oldlace: '#fdf5e6',
  361. olive: '#808000',
  362. olivedrab: '#6b8e23',
  363. orange: '#ffa500',
  364. orangered: '#ff4500',
  365. orchid: '#da70d6',
  366. palegoldenrod: '#eee8aa',
  367. palegreen: '#98fb98',
  368. paleturquoise: '#afeeee',
  369. palevioletred: '#db7093',
  370. papayawhip: '#ffefd5',
  371. peachpuff: '#ffdab9',
  372. peru: '#cd853f',
  373. pink: '#ffc0cb',
  374. plum: '#dda0dd',
  375. powderblue: '#b0e0e6',
  376. purple: '#800080',
  377. purple2: '#7f007f',
  378. purple3: '#a020f0',
  379. rebeccapurple: '#663399',
  380. red: '#ff0000',
  381. rosybrown: '#bc8f8f',
  382. royalblue: '#4169e1',
  383. saddlebrown: '#8b4513',
  384. salmon: '#fa8072',
  385. sandybrown: '#f4a460',
  386. seagreen: '#2e8b57',
  387. seashell: '#fff5ee',
  388. sienna: '#a0522d',
  389. silver: '#c0c0c0',
  390. skyblue: '#87ceeb',
  391. slateblue: '#6a5acd',
  392. slategray: '#708090',
  393. slategrey: '#708090',
  394. snow: '#fffafa',
  395. springgreen: '#00ff7f',
  396. steelblue: '#4682b4',
  397. tan: '#d2b48c',
  398. teal: '#008080',
  399. thistle: '#d8bfd8',
  400. tomato: '#ff6347',
  401. turquoise: '#40e0d0',
  402. violet: '#ee82ee',
  403. wheat: '#f5deb3',
  404. white: '#ffffff',
  405. whitesmoke: '#f5f5f5',
  406. yellow: '#ffff00',
  407. yellowgreen: '#9acd32'
  408. };
  409. chroma.colors = colors = w3cx11;
  410. lab2rgb = function() {
  411. var a, args, b, g, l, r, x, y, z;
  412. args = unpack(arguments);
  413. l = args[0], a = args[1], b = args[2];
  414. y = (l + 16) / 116;
  415. x = isNaN(a) ? y : y + a / 500;
  416. z = isNaN(b) ? y : y - b / 200;
  417. y = LAB_CONSTANTS.Yn * lab_xyz(y);
  418. x = LAB_CONSTANTS.Xn * lab_xyz(x);
  419. z = LAB_CONSTANTS.Zn * lab_xyz(z);
  420. r = xyz_rgb(3.2404542 * x - 1.5371385 * y - 0.4985314 * z);
  421. g = xyz_rgb(-0.9692660 * x + 1.8760108 * y + 0.0415560 * z);
  422. b = xyz_rgb(0.0556434 * x - 0.2040259 * y + 1.0572252 * z);
  423. return [r, g, b, args.length > 3 ? args[3] : 1];
  424. };
  425. xyz_rgb = function(r) {
  426. return 255 * (r <= 0.00304 ? 12.92 * r : 1.055 * pow(r, 1 / 2.4) - 0.055);
  427. };
  428. lab_xyz = function(t) {
  429. if (t > LAB_CONSTANTS.t1) {
  430. return t * t * t;
  431. } else {
  432. return LAB_CONSTANTS.t2 * (t - LAB_CONSTANTS.t0);
  433. }
  434. };
  435. LAB_CONSTANTS = {
  436. Kn: 18,
  437. Xn: 0.950470,
  438. Yn: 1,
  439. Zn: 1.088830,
  440. t0: 0.137931034,
  441. t1: 0.206896552,
  442. t2: 0.12841855,
  443. t3: 0.008856452
  444. };
  445. rgb2lab = function() {
  446. var b, g, r, ref, ref1, x, y, z;
  447. ref = unpack(arguments), r = ref[0], g = ref[1], b = ref[2];
  448. ref1 = rgb2xyz(r, g, b), x = ref1[0], y = ref1[1], z = ref1[2];
  449. return [116 * y - 16, 500 * (x - y), 200 * (y - z)];
  450. };
  451. rgb_xyz = function(r) {
  452. if ((r /= 255) <= 0.04045) {
  453. return r / 12.92;
  454. } else {
  455. return pow((r + 0.055) / 1.055, 2.4);
  456. }
  457. };
  458. xyz_lab = function(t) {
  459. if (t > LAB_CONSTANTS.t3) {
  460. return pow(t, 1 / 3);
  461. } else {
  462. return t / LAB_CONSTANTS.t2 + LAB_CONSTANTS.t0;
  463. }
  464. };
  465. rgb2xyz = function() {
  466. var b, g, r, ref, x, y, z;
  467. ref = unpack(arguments), r = ref[0], g = ref[1], b = ref[2];
  468. r = rgb_xyz(r);
  469. g = rgb_xyz(g);
  470. b = rgb_xyz(b);
  471. x = xyz_lab((0.4124564 * r + 0.3575761 * g + 0.1804375 * b) / LAB_CONSTANTS.Xn);
  472. y = xyz_lab((0.2126729 * r + 0.7151522 * g + 0.0721750 * b) / LAB_CONSTANTS.Yn);
  473. z = xyz_lab((0.0193339 * r + 0.1191920 * g + 0.9503041 * b) / LAB_CONSTANTS.Zn);
  474. return [x, y, z];
  475. };
  476. chroma.lab = function() {
  477. return (function(func, args, ctor) {
  478. ctor.prototype = func.prototype;
  479. var child = new ctor, result = func.apply(child, args);
  480. return Object(result) === result ? result : child;
  481. })(Color, slice.call(arguments).concat(['lab']), function(){});
  482. };
  483. _input.lab = lab2rgb;
  484. Color.prototype.lab = function() {
  485. return rgb2lab(this._rgb);
  486. };
  487. bezier = function(colors) {
  488. var I, I0, I1, c, lab0, lab1, lab2, lab3, ref, ref1, ref2;
  489. colors = (function() {
  490. var len, o, results;
  491. results = [];
  492. for (o = 0, len = colors.length; o < len; o++) {
  493. c = colors[o];
  494. results.push(chroma(c));
  495. }
  496. return results;
  497. })();
  498. if (colors.length === 2) {
  499. ref = (function() {
  500. var len, o, results;
  501. results = [];
  502. for (o = 0, len = colors.length; o < len; o++) {
  503. c = colors[o];
  504. results.push(c.lab());
  505. }
  506. return results;
  507. })(), lab0 = ref[0], lab1 = ref[1];
  508. I = function(t) {
  509. var i, lab;
  510. lab = (function() {
  511. var o, results;
  512. results = [];
  513. for (i = o = 0; o <= 2; i = ++o) {
  514. results.push(lab0[i] + t * (lab1[i] - lab0[i]));
  515. }
  516. return results;
  517. })();
  518. return chroma.lab.apply(chroma, lab);
  519. };
  520. } else if (colors.length === 3) {
  521. ref1 = (function() {
  522. var len, o, results;
  523. results = [];
  524. for (o = 0, len = colors.length; o < len; o++) {
  525. c = colors[o];
  526. results.push(c.lab());
  527. }
  528. return results;
  529. })(), lab0 = ref1[0], lab1 = ref1[1], lab2 = ref1[2];
  530. I = function(t) {
  531. var i, lab;
  532. lab = (function() {
  533. var o, results;
  534. results = [];
  535. for (i = o = 0; o <= 2; i = ++o) {
  536. results.push((1 - t) * (1 - t) * lab0[i] + 2 * (1 - t) * t * lab1[i] + t * t * lab2[i]);
  537. }
  538. return results;
  539. })();
  540. return chroma.lab.apply(chroma, lab);
  541. };
  542. } else if (colors.length === 4) {
  543. ref2 = (function() {
  544. var len, o, results;
  545. results = [];
  546. for (o = 0, len = colors.length; o < len; o++) {
  547. c = colors[o];
  548. results.push(c.lab());
  549. }
  550. return results;
  551. })(), lab0 = ref2[0], lab1 = ref2[1], lab2 = ref2[2], lab3 = ref2[3];
  552. I = function(t) {
  553. var i, lab;
  554. lab = (function() {
  555. var o, results;
  556. results = [];
  557. for (i = o = 0; o <= 2; i = ++o) {
  558. 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]);
  559. }
  560. return results;
  561. })();
  562. return chroma.lab.apply(chroma, lab);
  563. };
  564. } else if (colors.length === 5) {
  565. I0 = bezier(colors.slice(0, 3));
  566. I1 = bezier(colors.slice(2, 5));
  567. I = function(t) {
  568. if (t < 0.5) {
  569. return I0(t * 2);
  570. } else {
  571. return I1((t - 0.5) * 2);
  572. }
  573. };
  574. }
  575. return I;
  576. };
  577. chroma.bezier = function(colors) {
  578. var f;
  579. f = bezier(colors);
  580. f.scale = function() {
  581. return chroma.scale(f);
  582. };
  583. return f;
  584. };
  585. chroma.cubehelix = function(start, rotations, hue, gamma, lightness) {
  586. var dh, dl, f;
  587. if (start == null) {
  588. start = 300;
  589. }
  590. if (rotations == null) {
  591. rotations = -1.5;
  592. }
  593. if (hue == null) {
  594. hue = 1;
  595. }
  596. if (gamma == null) {
  597. gamma = 1;
  598. }
  599. if (lightness == null) {
  600. lightness = [0, 1];
  601. }
  602. dh = 0;
  603. if (type(lightness) === 'array') {
  604. dl = lightness[1] - lightness[0];
  605. } else {
  606. dl = 0;
  607. lightness = [lightness, lightness];
  608. }
  609. f = function(fract) {
  610. var a, amp, b, cos_a, g, h, l, r, sin_a;
  611. a = TWOPI * ((start + 120) / 360 + rotations * fract);
  612. l = pow(lightness[0] + dl * fract, gamma);
  613. h = dh !== 0 ? hue[0] + fract * dh : hue;
  614. amp = h * l * (1 - l) / 2;
  615. cos_a = cos(a);
  616. sin_a = sin(a);
  617. r = l + amp * (-0.14861 * cos_a + 1.78277 * sin_a);
  618. g = l + amp * (-0.29227 * cos_a - 0.90649 * sin_a);
  619. b = l + amp * (+1.97294 * cos_a);
  620. return chroma(clip_rgb([r * 255, g * 255, b * 255, 1]));
  621. };
  622. f.start = function(s) {
  623. if (s == null) {
  624. return start;
  625. }
  626. start = s;
  627. return f;
  628. };
  629. f.rotations = function(r) {
  630. if (r == null) {
  631. return rotations;
  632. }
  633. rotations = r;
  634. return f;
  635. };
  636. f.gamma = function(g) {
  637. if (g == null) {
  638. return gamma;
  639. }
  640. gamma = g;
  641. return f;
  642. };
  643. f.hue = function(h) {
  644. if (h == null) {
  645. return hue;
  646. }
  647. hue = h;
  648. if (type(hue) === 'array') {
  649. dh = hue[1] - hue[0];
  650. if (dh === 0) {
  651. hue = hue[1];
  652. }
  653. } else {
  654. dh = 0;
  655. }
  656. return f;
  657. };
  658. f.lightness = function(h) {
  659. if (h == null) {
  660. return lightness;
  661. }
  662. if (type(h) === 'array') {
  663. lightness = h;
  664. dl = h[1] - h[0];
  665. } else {
  666. lightness = [h, h];
  667. dl = 0;
  668. }
  669. return f;
  670. };
  671. f.scale = function() {
  672. return chroma.scale(f);
  673. };
  674. f.hue(hue);
  675. return f;
  676. };
  677. chroma.random = function() {
  678. var code, digits, i, o;
  679. digits = '0123456789abcdef';
  680. code = '#';
  681. for (i = o = 0; o < 6; i = ++o) {
  682. code += digits.charAt(floor(Math.random() * 16));
  683. }
  684. return new Color(code);
  685. };
  686. _interpolators = [];
  687. interpolate = function(col1, col2, f, m) {
  688. var interpol, len, o, res;
  689. if (f == null) {
  690. f = 0.5;
  691. }
  692. if (m == null) {
  693. m = 'rgb';
  694. }
  695. /*
  696. interpolates between colors
  697. f = 0 --> me
  698. f = 1 --> col
  699. */
  700. if (type(col1) !== 'object') {
  701. col1 = chroma(col1);
  702. }
  703. if (type(col2) !== 'object') {
  704. col2 = chroma(col2);
  705. }
  706. for (o = 0, len = _interpolators.length; o < len; o++) {
  707. interpol = _interpolators[o];
  708. if (m === interpol[0]) {
  709. res = interpol[1](col1, col2, f, m);
  710. break;
  711. }
  712. }
  713. if (res == null) {
  714. throw "color mode " + m + " is not supported";
  715. }
  716. return res.alpha(col1.alpha() + f * (col2.alpha() - col1.alpha()));
  717. };
  718. chroma.interpolate = interpolate;
  719. Color.prototype.interpolate = function(col2, f, m) {
  720. return interpolate(this, col2, f, m);
  721. };
  722. chroma.mix = interpolate;
  723. Color.prototype.mix = Color.prototype.interpolate;
  724. _input.rgb = function() {
  725. var k, ref, results, v;
  726. ref = unpack(arguments);
  727. results = [];
  728. for (k in ref) {
  729. v = ref[k];
  730. results.push(v);
  731. }
  732. return results;
  733. };
  734. chroma.rgb = function() {
  735. return (function(func, args, ctor) {
  736. ctor.prototype = func.prototype;
  737. var child = new ctor, result = func.apply(child, args);
  738. return Object(result) === result ? result : child;
  739. })(Color, slice.call(arguments).concat(['rgb']), function(){});
  740. };
  741. Color.prototype.rgb = function(round) {
  742. if (round == null) {
  743. round = true;
  744. }
  745. if (round) {
  746. return this._rgb.map(Math.round).slice(0, 3);
  747. } else {
  748. return this._rgb.slice(0, 3);
  749. }
  750. };
  751. Color.prototype.rgba = function(round) {
  752. if (round == null) {
  753. round = true;
  754. }
  755. if (!round) {
  756. return this._rgb.slice(0);
  757. }
  758. return [Math.round(this._rgb[0]), Math.round(this._rgb[1]), Math.round(this._rgb[2]), this._rgb[3]];
  759. };
  760. _guess_formats.push({
  761. p: 3,
  762. test: function(n) {
  763. var a;
  764. a = unpack(arguments);
  765. if (type(a) === 'array' && a.length === 3) {
  766. return 'rgb';
  767. }
  768. if (a.length === 4 && type(a[3]) === "number" && a[3] >= 0 && a[3] <= 1) {
  769. return 'rgb';
  770. }
  771. }
  772. });
  773. _input.lrgb = _input.rgb;
  774. interpolate_lrgb = function(col1, col2, f, m) {
  775. var xyz0, xyz1;
  776. xyz0 = col1._rgb;
  777. xyz1 = col2._rgb;
  778. 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);
  779. };
  780. _average_lrgb = function(colors) {
  781. var col, f, len, o, rgb, xyz;
  782. f = 1 / colors.length;
  783. xyz = [0, 0, 0, 0];
  784. for (o = 0, len = colors.length; o < len; o++) {
  785. col = colors[o];
  786. rgb = col._rgb;
  787. xyz[0] += pow(rgb[0], 2) * f;
  788. xyz[1] += pow(rgb[1], 2) * f;
  789. xyz[2] += pow(rgb[2], 2) * f;
  790. xyz[3] += rgb[3] * f;
  791. }
  792. xyz[0] = sqrt(xyz[0]);
  793. xyz[1] = sqrt(xyz[1]);
  794. xyz[2] = sqrt(xyz[2]);
  795. if (xyz[3] > 1) {
  796. xyz[3] = 1;
  797. }
  798. return new Color(clip_rgb(xyz));
  799. };
  800. _interpolators.push(['lrgb', interpolate_lrgb]);
  801. chroma.average = function(colors, mode) {
  802. var A, alpha, c, cnt, dx, dy, first, i, l, len, o, xyz, xyz2;
  803. if (mode == null) {
  804. mode = 'rgb';
  805. }
  806. l = colors.length;
  807. colors = colors.map(function(c) {
  808. return chroma(c);
  809. });
  810. first = colors.splice(0, 1)[0];
  811. if (mode === 'lrgb') {
  812. return _average_lrgb(colors);
  813. }
  814. xyz = first.get(mode);
  815. cnt = [];
  816. dx = 0;
  817. dy = 0;
  818. for (i in xyz) {
  819. xyz[i] = xyz[i] || 0;
  820. cnt.push(isNaN(xyz[i]) ? 0 : 1);
  821. if (mode.charAt(i) === 'h' && !isNaN(xyz[i])) {
  822. A = xyz[i] / 180 * PI;
  823. dx += cos(A);
  824. dy += sin(A);
  825. }
  826. }
  827. alpha = first.alpha();
  828. for (o = 0, len = colors.length; o < len; o++) {
  829. c = colors[o];
  830. xyz2 = c.get(mode);
  831. alpha += c.alpha();
  832. for (i in xyz) {
  833. if (!isNaN(xyz2[i])) {
  834. cnt[i] += 1;
  835. if (mode.charAt(i) === 'h') {
  836. A = xyz2[i] / 180 * PI;
  837. dx += cos(A);
  838. dy += sin(A);
  839. } else {
  840. xyz[i] += xyz2[i];
  841. }
  842. }
  843. }
  844. }
  845. for (i in xyz) {
  846. if (mode.charAt(i) === 'h') {
  847. A = atan2(dy / cnt[i], dx / cnt[i]) / PI * 180;
  848. while (A < 0) {
  849. A += 360;
  850. }
  851. while (A >= 360) {
  852. A -= 360;
  853. }
  854. xyz[i] = A;
  855. } else {
  856. xyz[i] = xyz[i] / cnt[i];
  857. }
  858. }
  859. return chroma(xyz, mode).alpha(alpha / l);
  860. };
  861. hex2rgb = function(hex) {
  862. var a, b, g, r, rgb, u;
  863. if (hex.match(/^#?([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/)) {
  864. if (hex.length === 4 || hex.length === 7) {
  865. hex = hex.substr(1);
  866. }
  867. if (hex.length === 3) {
  868. hex = hex.split("");
  869. hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
  870. }
  871. u = parseInt(hex, 16);
  872. r = u >> 16;
  873. g = u >> 8 & 0xFF;
  874. b = u & 0xFF;
  875. return [r, g, b, 1];
  876. }
  877. if (hex.match(/^#?([A-Fa-f0-9]{8})$/)) {
  878. if (hex.length === 9) {
  879. hex = hex.substr(1);
  880. }
  881. u = parseInt(hex, 16);
  882. r = u >> 24 & 0xFF;
  883. g = u >> 16 & 0xFF;
  884. b = u >> 8 & 0xFF;
  885. a = round((u & 0xFF) / 0xFF * 100) / 100;
  886. return [r, g, b, a];
  887. }
  888. if ((_input.css != null) && (rgb = _input.css(hex))) {
  889. return rgb;
  890. }
  891. throw "unknown color: " + hex;
  892. };
  893. rgb2hex = function(channels, mode) {
  894. var a, b, g, hxa, r, str, u;
  895. if (mode == null) {
  896. mode = 'auto';
  897. }
  898. r = channels[0], g = channels[1], b = channels[2], a = channels[3];
  899. if (mode === 'auto') {
  900. mode = a < 1 ? 'rgba' : 'rgb';
  901. }
  902. r = Math.round(r);
  903. g = Math.round(g);
  904. b = Math.round(b);
  905. u = r << 16 | g << 8 | b;
  906. str = "000000" + u.toString(16);
  907. str = str.substr(str.length - 6);
  908. hxa = '0' + round(a * 255).toString(16);
  909. hxa = hxa.substr(hxa.length - 2);
  910. return "#" + (function() {
  911. switch (mode.toLowerCase()) {
  912. case 'rgba':
  913. return str + hxa;
  914. case 'argb':
  915. return hxa + str;
  916. default:
  917. return str;
  918. }
  919. })();
  920. };
  921. _input.hex = function(h) {
  922. return hex2rgb(h);
  923. };
  924. chroma.hex = function() {
  925. return (function(func, args, ctor) {
  926. ctor.prototype = func.prototype;
  927. var child = new ctor, result = func.apply(child, args);
  928. return Object(result) === result ? result : child;
  929. })(Color, slice.call(arguments).concat(['hex']), function(){});
  930. };
  931. Color.prototype.hex = function(mode) {
  932. if (mode == null) {
  933. mode = 'auto';
  934. }
  935. return rgb2hex(this._rgb, mode);
  936. };
  937. _guess_formats.push({
  938. p: 4,
  939. test: function(n) {
  940. if (arguments.length === 1 && type(n) === "string") {
  941. return 'hex';
  942. }
  943. }
  944. });
  945. hsl2rgb = function() {
  946. var args, b, c, g, h, i, l, o, r, ref, s, t1, t2, t3;
  947. args = unpack(arguments);
  948. h = args[0], s = args[1], l = args[2];
  949. if (s === 0) {
  950. r = g = b = l * 255;
  951. } else {
  952. t3 = [0, 0, 0];
  953. c = [0, 0, 0];
  954. t2 = l < 0.5 ? l * (1 + s) : l + s - l * s;
  955. t1 = 2 * l - t2;
  956. h /= 360;
  957. t3[0] = h + 1 / 3;
  958. t3[1] = h;
  959. t3[2] = h - 1 / 3;
  960. for (i = o = 0; o <= 2; i = ++o) {
  961. if (t3[i] < 0) {
  962. t3[i] += 1;
  963. }
  964. if (t3[i] > 1) {
  965. t3[i] -= 1;
  966. }
  967. if (6 * t3[i] < 1) {
  968. c[i] = t1 + (t2 - t1) * 6 * t3[i];
  969. } else if (2 * t3[i] < 1) {
  970. c[i] = t2;
  971. } else if (3 * t3[i] < 2) {
  972. c[i] = t1 + (t2 - t1) * ((2 / 3) - t3[i]) * 6;
  973. } else {
  974. c[i] = t1;
  975. }
  976. }
  977. ref = [round(c[0] * 255), round(c[1] * 255), round(c[2] * 255)], r = ref[0], g = ref[1], b = ref[2];
  978. }
  979. if (args.length > 3) {
  980. return [r, g, b, args[3]];
  981. } else {
  982. return [r, g, b];
  983. }
  984. };
  985. rgb2hsl = function(r, g, b) {
  986. var h, l, min, ref, s;
  987. if (r !== void 0 && r.length >= 3) {
  988. ref = r, r = ref[0], g = ref[1], b = ref[2];
  989. }
  990. r /= 255;
  991. g /= 255;
  992. b /= 255;
  993. min = Math.min(r, g, b);
  994. max = Math.max(r, g, b);
  995. l = (max + min) / 2;
  996. if (max === min) {
  997. s = 0;
  998. h = Number.NaN;
  999. } else {
  1000. s = l < 0.5 ? (max - min) / (max + min) : (max - min) / (2 - max - min);
  1001. }
  1002. if (r === max) {
  1003. h = (g - b) / (max - min);
  1004. } else if (g === max) {
  1005. h = 2 + (b - r) / (max - min);
  1006. } else if (b === max) {
  1007. h = 4 + (r - g) / (max - min);
  1008. }
  1009. h *= 60;
  1010. if (h < 0) {
  1011. h += 360;
  1012. }
  1013. return [h, s, l];
  1014. };
  1015. chroma.hsl = function() {
  1016. return (function(func, args, ctor) {
  1017. ctor.prototype = func.prototype;
  1018. var child = new ctor, result = func.apply(child, args);
  1019. return Object(result) === result ? result : child;
  1020. })(Color, slice.call(arguments).concat(['hsl']), function(){});
  1021. };
  1022. _input.hsl = hsl2rgb;
  1023. Color.prototype.hsl = function() {
  1024. return rgb2hsl(this._rgb);
  1025. };
  1026. hsv2rgb = function() {
  1027. var args, b, f, g, h, i, p, q, r, ref, ref1, ref2, ref3, ref4, ref5, s, t, v;
  1028. args = unpack(arguments);
  1029. h = args[0], s = args[1], v = args[2];
  1030. v *= 255;
  1031. if (s === 0) {
  1032. r = g = b = v;
  1033. } else {
  1034. if (h === 360) {
  1035. h = 0;
  1036. }
  1037. if (h > 360) {
  1038. h -= 360;
  1039. }
  1040. if (h < 0) {
  1041. h += 360;
  1042. }
  1043. h /= 60;
  1044. i = floor(h);
  1045. f = h - i;
  1046. p = v * (1 - s);
  1047. q = v * (1 - s * f);
  1048. t = v * (1 - s * (1 - f));
  1049. switch (i) {
  1050. case 0:
  1051. ref = [v, t, p], r = ref[0], g = ref[1], b = ref[2];
  1052. break;
  1053. case 1:
  1054. ref1 = [q, v, p], r = ref1[0], g = ref1[1], b = ref1[2];
  1055. break;
  1056. case 2:
  1057. ref2 = [p, v, t], r = ref2[0], g = ref2[1], b = ref2[2];
  1058. break;
  1059. case 3:
  1060. ref3 = [p, q, v], r = ref3[0], g = ref3[1], b = ref3[2];
  1061. break;
  1062. case 4:
  1063. ref4 = [t, p, v], r = ref4[0], g = ref4[1], b = ref4[2];
  1064. break;
  1065. case 5:
  1066. ref5 = [v, p, q], r = ref5[0], g = ref5[1], b = ref5[2];
  1067. }
  1068. }
  1069. return [r, g, b, args.length > 3 ? args[3] : 1];
  1070. };
  1071. rgb2hsv = function() {
  1072. var b, delta, g, h, min, r, ref, s, v;
  1073. ref = unpack(arguments), r = ref[0], g = ref[1], b = ref[2];
  1074. min = Math.min(r, g, b);
  1075. max = Math.max(r, g, b);
  1076. delta = max - min;
  1077. v = max / 255.0;
  1078. if (max === 0) {
  1079. h = Number.NaN;
  1080. s = 0;
  1081. } else {
  1082. s = delta / max;
  1083. if (r === max) {
  1084. h = (g - b) / delta;
  1085. }
  1086. if (g === max) {
  1087. h = 2 + (b - r) / delta;
  1088. }
  1089. if (b === max) {
  1090. h = 4 + (r - g) / delta;
  1091. }
  1092. h *= 60;
  1093. if (h < 0) {
  1094. h += 360;
  1095. }
  1096. }
  1097. return [h, s, v];
  1098. };
  1099. chroma.hsv = function() {
  1100. return (function(func, args, ctor) {
  1101. ctor.prototype = func.prototype;
  1102. var child = new ctor, result = func.apply(child, args);
  1103. return Object(result) === result ? result : child;
  1104. })(Color, slice.call(arguments).concat(['hsv']), function(){});
  1105. };
  1106. _input.hsv = hsv2rgb;
  1107. Color.prototype.hsv = function() {
  1108. return rgb2hsv(this._rgb);
  1109. };
  1110. num2rgb = function(num) {
  1111. var b, g, r;
  1112. if (type(num) === "number" && num >= 0 && num <= 0xFFFFFF) {
  1113. r = num >> 16;
  1114. g = (num >> 8) & 0xFF;
  1115. b = num & 0xFF;
  1116. return [r, g, b, 1];
  1117. }
  1118. console.warn("unknown num color: " + num);
  1119. return [0, 0, 0, 1];
  1120. };
  1121. rgb2num = function() {
  1122. var b, g, r, ref;
  1123. ref = unpack(arguments), r = ref[0], g = ref[1], b = ref[2];
  1124. return (r << 16) + (g << 8) + b;
  1125. };
  1126. chroma.num = function(num) {
  1127. return new Color(num, 'num');
  1128. };
  1129. Color.prototype.num = function(mode) {
  1130. if (mode == null) {
  1131. mode = 'rgb';
  1132. }
  1133. return rgb2num(this._rgb, mode);
  1134. };
  1135. _input.num = num2rgb;
  1136. _guess_formats.push({
  1137. p: 1,
  1138. test: function(n) {
  1139. if (arguments.length === 1 && type(n) === "number" && n >= 0 && n <= 0xFFFFFF) {
  1140. return 'num';
  1141. }
  1142. }
  1143. });
  1144. hcg2rgb = function() {
  1145. var _c, _g, args, b, c, f, g, h, i, p, q, r, ref, ref1, ref2, ref3, ref4, ref5, t, v;
  1146. args = unpack(arguments);
  1147. h = args[0], c = args[1], _g = args[2];
  1148. c = c / 100;
  1149. g = g / 100 * 255;
  1150. _c = c * 255;
  1151. if (c === 0) {
  1152. r = g = b = _g;
  1153. } else {
  1154. if (h === 360) {
  1155. h = 0;
  1156. }
  1157. if (h > 360) {
  1158. h -= 360;
  1159. }
  1160. if (h < 0) {
  1161. h += 360;
  1162. }
  1163. h /= 60;
  1164. i = floor(h);
  1165. f = h - i;
  1166. p = _g * (1 - c);
  1167. q = p + _c * (1 - f);
  1168. t = p + _c * f;
  1169. v = p + _c;
  1170. switch (i) {
  1171. case 0:
  1172. ref = [v, t, p], r = ref[0], g = ref[1], b = ref[2];
  1173. break;
  1174. case 1:
  1175. ref1 = [q, v, p], r = ref1[0], g = ref1[1], b = ref1[2];
  1176. break;
  1177. case 2:
  1178. ref2 = [p, v, t], r = ref2[0], g = ref2[1], b = ref2[2];
  1179. break;
  1180. case 3:
  1181. ref3 = [p, q, v], r = ref3[0], g = ref3[1], b = ref3[2];
  1182. break;
  1183. case 4:
  1184. ref4 = [t, p, v], r = ref4[0], g = ref4[1], b = ref4[2];
  1185. break;
  1186. case 5:
  1187. ref5 = [v, p, q], r = ref5[0], g = ref5[1], b = ref5[2];
  1188. }
  1189. }
  1190. return [r, g, b, args.length > 3 ? args[3] : 1];
  1191. };
  1192. rgb2hcg = function() {
  1193. var _g, b, c, delta, g, h, min, r, ref;
  1194. ref = unpack(arguments), r = ref[0], g = ref[1], b = ref[2];
  1195. min = Math.min(r, g, b);
  1196. max = Math.max(r, g, b);
  1197. delta = max - min;
  1198. c = delta * 100 / 255;
  1199. _g = min / (255 - delta) * 100;
  1200. if (delta === 0) {
  1201. h = Number.NaN;
  1202. } else {
  1203. if (r === max) {
  1204. h = (g - b) / delta;
  1205. }
  1206. if (g === max) {
  1207. h = 2 + (b - r) / delta;
  1208. }
  1209. if (b === max) {
  1210. h = 4 + (r - g) / delta;
  1211. }
  1212. h *= 60;
  1213. if (h < 0) {
  1214. h += 360;
  1215. }
  1216. }
  1217. return [h, c, _g];
  1218. };
  1219. chroma.hcg = function() {
  1220. return (function(func, args, ctor) {
  1221. ctor.prototype = func.prototype;
  1222. var child = new ctor, result = func.apply(child, args);
  1223. return Object(result) === result ? result : child;
  1224. })(Color, slice.call(arguments).concat(['hcg']), function(){});
  1225. };
  1226. _input.hcg = hcg2rgb;
  1227. Color.prototype.hcg = function() {
  1228. return rgb2hcg(this._rgb);
  1229. };
  1230. css2rgb = function(css) {
  1231. var aa, ab, hsl, i, m, o, rgb, w;
  1232. css = css.toLowerCase();
  1233. if ((chroma.colors != null) && chroma.colors[css]) {
  1234. return hex2rgb(chroma.colors[css]);
  1235. }
  1236. if (m = css.match(/rgb\(\s*(\-?\d+),\s*(\-?\d+)\s*,\s*(\-?\d+)\s*\)/)) {
  1237. rgb = m.slice(1, 4);
  1238. for (i = o = 0; o <= 2; i = ++o) {
  1239. rgb[i] = +rgb[i];
  1240. }
  1241. rgb[3] = 1;
  1242. } else if (m = css.match(/rgba\(\s*(\-?\d+),\s*(\-?\d+)\s*,\s*(\-?\d+)\s*,\s*([01]|[01]?\.\d+)\)/)) {
  1243. rgb = m.slice(1, 5);
  1244. for (i = w = 0; w <= 3; i = ++w) {
  1245. rgb[i] = +rgb[i];
  1246. }
  1247. } else if (m = css.match(/rgb\(\s*(\-?\d+(?:\.\d+)?)%,\s*(\-?\d+(?:\.\d+)?)%\s*,\s*(\-?\d+(?:\.\d+)?)%\s*\)/)) {
  1248. rgb = m.slice(1, 4);
  1249. for (i = aa = 0; aa <= 2; i = ++aa) {
  1250. rgb[i] = round(rgb[i] * 2.55);
  1251. }
  1252. rgb[3] = 1;
  1253. } else if (m = css.match(/rgba\(\s*(\-?\d+(?:\.\d+)?)%,\s*(\-?\d+(?:\.\d+)?)%\s*,\s*(\-?\d+(?:\.\d+)?)%\s*,\s*([01]|[01]?\.\d+)\)/)) {
  1254. rgb = m.slice(1, 5);
  1255. for (i = ab = 0; ab <= 2; i = ++ab) {
  1256. rgb[i] = round(rgb[i] * 2.55);
  1257. }
  1258. rgb[3] = +rgb[3];
  1259. } else if (m = css.match(/hsl\(\s*(\-?\d+(?:\.\d+)?),\s*(\-?\d+(?:\.\d+)?)%\s*,\s*(\-?\d+(?:\.\d+)?)%\s*\)/)) {
  1260. hsl = m.slice(1, 4);
  1261. hsl[1] *= 0.01;
  1262. hsl[2] *= 0.01;
  1263. rgb = hsl2rgb(hsl);
  1264. rgb[3] = 1;
  1265. } else if (m = css.match(/hsla\(\s*(\-?\d+(?:\.\d+)?),\s*(\-?\d+(?:\.\d+)?)%\s*,\s*(\-?\d+(?:\.\d+)?)%\s*,\s*([01]|[01]?\.\d+)\)/)) {
  1266. hsl = m.slice(1, 4);
  1267. hsl[1] *= 0.01;
  1268. hsl[2] *= 0.01;
  1269. rgb = hsl2rgb(hsl);
  1270. rgb[3] = +m[4];
  1271. }
  1272. return rgb;
  1273. };
  1274. rgb2css = function(rgba) {
  1275. var mode;
  1276. mode = rgba[3] < 1 ? 'rgba' : 'rgb';
  1277. if (mode === 'rgb') {
  1278. return mode + '(' + rgba.slice(0, 3).map(round).join(',') + ')';
  1279. } else if (mode === 'rgba') {
  1280. return mode + '(' + rgba.slice(0, 3).map(round).join(',') + ',' + rgba[3] + ')';
  1281. } else {
  1282. }
  1283. };
  1284. rnd = function(a) {
  1285. return round(a * 100) / 100;
  1286. };
  1287. hsl2css = function(hsl, alpha) {
  1288. var mode;
  1289. mode = alpha < 1 ? 'hsla' : 'hsl';
  1290. hsl[0] = rnd(hsl[0] || 0);
  1291. hsl[1] = rnd(hsl[1] * 100) + '%';
  1292. hsl[2] = rnd(hsl[2] * 100) + '%';
  1293. if (mode === 'hsla') {
  1294. hsl[3] = alpha;
  1295. }
  1296. return mode + '(' + hsl.join(',') + ')';
  1297. };
  1298. _input.css = function(h) {
  1299. return css2rgb(h);
  1300. };
  1301. chroma.css = function() {
  1302. return (function(func, args, ctor) {
  1303. ctor.prototype = func.prototype;
  1304. var child = new ctor, result = func.apply(child, args);
  1305. return Object(result) === result ? result : child;
  1306. })(Color, slice.call(arguments).concat(['css']), function(){});
  1307. };
  1308. Color.prototype.css = function(mode) {
  1309. if (mode == null) {
  1310. mode = 'rgb';
  1311. }
  1312. if (mode.slice(0, 3) === 'rgb') {
  1313. return rgb2css(this._rgb);
  1314. } else if (mode.slice(0, 3) === 'hsl') {
  1315. return hsl2css(this.hsl(), this.alpha());
  1316. }
  1317. };
  1318. _input.named = function(name) {
  1319. return hex2rgb(w3cx11[name]);
  1320. };
  1321. _guess_formats.push({
  1322. p: 5,
  1323. test: function(n) {
  1324. if (arguments.length === 1 && (w3cx11[n] != null)) {
  1325. return 'named';
  1326. }
  1327. }
  1328. });
  1329. Color.prototype.name = function(n) {
  1330. var h, k;
  1331. if (arguments.length) {
  1332. if (w3cx11[n]) {
  1333. this._rgb = hex2rgb(w3cx11[n]);
  1334. }
  1335. this._rgb[3] = 1;
  1336. this;
  1337. }
  1338. h = this.hex('rgb');
  1339. for (k in w3cx11) {
  1340. if (h === w3cx11[k]) {
  1341. return k;
  1342. }
  1343. }
  1344. return h;
  1345. };
  1346. lch2lab = function() {
  1347. /*
  1348. Convert from a qualitative parameter h and a quantitative parameter l to a 24-bit pixel.
  1349. These formulas were invented by David Dalrymple to obtain maximum contrast without going
  1350. out of gamut if the parameters are in the range 0-1.
  1351. A saturation multiplier was added by Gregor Aisch
  1352. */
  1353. var c, h, l, ref;
  1354. ref = unpack(arguments), l = ref[0], c = ref[1], h = ref[2];
  1355. h = h * DEG2RAD;
  1356. return [l, cos(h) * c, sin(h) * c];
  1357. };
  1358. lch2rgb = function() {
  1359. var L, a, args, b, c, g, h, l, r, ref, ref1;
  1360. args = unpack(arguments);
  1361. l = args[0], c = args[1], h = args[2];
  1362. ref = lch2lab(l, c, h), L = ref[0], a = ref[1], b = ref[2];
  1363. ref1 = lab2rgb(L, a, b), r = ref1[0], g = ref1[1], b = ref1[2];
  1364. return [r, g, b, args.length > 3 ? args[3] : 1];
  1365. };
  1366. lab2lch = function() {
  1367. var a, b, c, h, l, ref;
  1368. ref = unpack(arguments), l = ref[0], a = ref[1], b = ref[2];
  1369. c = sqrt(a * a + b * b);
  1370. h = (atan2(b, a) * RAD2DEG + 360) % 360;
  1371. if (round(c * 10000) === 0) {
  1372. h = Number.NaN;
  1373. }
  1374. return [l, c, h];
  1375. };
  1376. rgb2lch = function() {
  1377. var a, b, g, l, r, ref, ref1;
  1378. ref = unpack(arguments), r = ref[0], g = ref[1], b = ref[2];
  1379. ref1 = rgb2lab(r, g, b), l = ref1[0], a = ref1[1], b = ref1[2];
  1380. return lab2lch(l, a, b);
  1381. };
  1382. chroma.lch = function() {
  1383. var args;
  1384. args = unpack(arguments);
  1385. return new Color(args, 'lch');
  1386. };
  1387. chroma.hcl = function() {
  1388. var args;
  1389. args = unpack(arguments);
  1390. return new Color(args, 'hcl');
  1391. };
  1392. _input.lch = lch2rgb;
  1393. _input.hcl = function() {
  1394. var c, h, l, ref;
  1395. ref = unpack(arguments), h = ref[0], c = ref[1], l = ref[2];
  1396. return lch2rgb([l, c, h]);
  1397. };
  1398. Color.prototype.lch = function() {
  1399. return rgb2lch(this._rgb);
  1400. };
  1401. Color.prototype.hcl = function() {
  1402. return rgb2lch(this._rgb).reverse();
  1403. };
  1404. rgb2cmyk = function(mode) {
  1405. var b, c, f, g, k, m, r, ref, y;
  1406. if (mode == null) {
  1407. mode = 'rgb';
  1408. }
  1409. ref = unpack(arguments), r = ref[0], g = ref[1], b = ref[2];
  1410. r = r / 255;
  1411. g = g / 255;
  1412. b = b / 255;
  1413. k = 1 - Math.max(r, Math.max(g, b));
  1414. f = k < 1 ? 1 / (1 - k) : 0;
  1415. c = (1 - r - k) * f;
  1416. m = (1 - g - k) * f;
  1417. y = (1 - b - k) * f;
  1418. return [c, m, y, k];
  1419. };
  1420. cmyk2rgb = function() {
  1421. var alpha, args, b, c, g, k, m, r, y;
  1422. args = unpack(arguments);
  1423. c = args[0], m = args[1], y = args[2], k = args[3];
  1424. alpha = args.length > 4 ? args[4] : 1;
  1425. if (k === 1) {
  1426. return [0, 0, 0, alpha];
  1427. }
  1428. r = c >= 1 ? 0 : 255 * (1 - c) * (1 - k);
  1429. g = m >= 1 ? 0 : 255 * (1 - m) * (1 - k);
  1430. b = y >= 1 ? 0 : 255 * (1 - y) * (1 - k);
  1431. return [r, g, b, alpha];
  1432. };
  1433. _input.cmyk = function() {
  1434. return cmyk2rgb(unpack(arguments));
  1435. };
  1436. chroma.cmyk = function() {
  1437. return (function(func, args, ctor) {
  1438. ctor.prototype = func.prototype;
  1439. var child = new ctor, result = func.apply(child, args);
  1440. return Object(result) === result ? result : child;
  1441. })(Color, slice.call(arguments).concat(['cmyk']), function(){});
  1442. };
  1443. Color.prototype.cmyk = function() {
  1444. return rgb2cmyk(this._rgb);
  1445. };
  1446. _input.gl = function() {
  1447. var i, k, o, rgb, v;
  1448. rgb = (function() {
  1449. var ref, results;
  1450. ref = unpack(arguments);
  1451. results = [];
  1452. for (k in ref) {
  1453. v = ref[k];
  1454. results.push(v);
  1455. }
  1456. return results;
  1457. }).apply(this, arguments);
  1458. for (i = o = 0; o <= 2; i = ++o) {
  1459. rgb[i] *= 255;
  1460. }
  1461. return rgb;
  1462. };
  1463. chroma.gl = function() {
  1464. return (function(func, args, ctor) {
  1465. ctor.prototype = func.prototype;
  1466. var child = new ctor, result = func.apply(child, args);
  1467. return Object(result) === result ? result : child;
  1468. })(Color, slice.call(arguments).concat(['gl']), function(){});
  1469. };
  1470. Color.prototype.gl = function() {
  1471. var rgb;
  1472. rgb = this._rgb;
  1473. return [rgb[0] / 255, rgb[1] / 255, rgb[2] / 255, rgb[3]];
  1474. };
  1475. rgb2luminance = function(r, g, b) {
  1476. var ref;
  1477. ref = unpack(arguments), r = ref[0], g = ref[1], b = ref[2];
  1478. r = luminance_x(r);
  1479. g = luminance_x(g);
  1480. b = luminance_x(b);
  1481. return 0.2126 * r + 0.7152 * g + 0.0722 * b;
  1482. };
  1483. luminance_x = function(x) {
  1484. x /= 255;
  1485. if (x <= 0.03928) {
  1486. return x / 12.92;
  1487. } else {
  1488. return pow((x + 0.055) / 1.055, 2.4);
  1489. }
  1490. };
  1491. interpolate_rgb = function(col1, col2, f, m) {
  1492. var xyz0, xyz1;
  1493. xyz0 = col1._rgb;
  1494. xyz1 = col2._rgb;
  1495. return new Color(xyz0[0] + f * (xyz1[0] - xyz0[0]), xyz0[1] + f * (xyz1[1] - xyz0[1]), xyz0[2] + f * (xyz1[2] - xyz0[2]), m);
  1496. };
  1497. _interpolators.push(['rgb', interpolate_rgb]);
  1498. Color.prototype.luminance = function(lum, mode) {
  1499. var cur_lum, eps, max_iter, rgba, test;
  1500. if (mode == null) {
  1501. mode = 'rgb';
  1502. }
  1503. if (!arguments.length) {
  1504. return rgb2luminance(this._rgb);
  1505. }
  1506. rgba = this._rgb;
  1507. if (lum === 0) {
  1508. rgba = [0, 0, 0, this._rgb[3]];
  1509. } else if (lum === 1) {
  1510. rgba = [255, 255, 255, this[3]];
  1511. } else {
  1512. cur_lum = rgb2luminance(this._rgb);
  1513. eps = 1e-7;
  1514. max_iter = 20;
  1515. test = function(l, h) {
  1516. var lm, m;
  1517. m = l.interpolate(h, 0.5, mode);
  1518. lm = m.luminance();
  1519. if (Math.abs(lum - lm) < eps || !max_iter--) {
  1520. return m;
  1521. }
  1522. if (lm > lum) {
  1523. return test(l, m);
  1524. }
  1525. return test(m, h);
  1526. };
  1527. if (cur_lum > lum) {
  1528. rgba = test(chroma('black'), this).rgba();
  1529. } else {
  1530. rgba = test(this, chroma('white')).rgba();
  1531. }
  1532. }
  1533. return chroma(rgba).alpha(this.alpha());
  1534. };
  1535. temperature2rgb = function(kelvin) {
  1536. var b, g, r, temp;
  1537. temp = kelvin / 100;
  1538. if (temp < 66) {
  1539. r = 255;
  1540. g = -155.25485562709179 - 0.44596950469579133 * (g = temp - 2) + 104.49216199393888 * log(g);
  1541. b = temp < 20 ? 0 : -254.76935184120902 + 0.8274096064007395 * (b = temp - 10) + 115.67994401066147 * log(b);
  1542. } else {
  1543. r = 351.97690566805693 + 0.114206453784165 * (r = temp - 55) - 40.25366309332127 * log(r);
  1544. g = 325.4494125711974 + 0.07943456536662342 * (g = temp - 50) - 28.0852963507957 * log(g);
  1545. b = 255;
  1546. }
  1547. return [r, g, b];
  1548. };
  1549. rgb2temperature = function() {
  1550. var b, eps, g, maxTemp, minTemp, r, ref, rgb, temp;
  1551. ref = unpack(arguments), r = ref[0], g = ref[1], b = ref[2];
  1552. minTemp = 1000;
  1553. maxTemp = 40000;
  1554. eps = 0.4;
  1555. while (maxTemp - minTemp > eps) {
  1556. temp = (maxTemp + minTemp) * 0.5;
  1557. rgb = temperature2rgb(temp);
  1558. if ((rgb[2] / rgb[0]) >= (b / r)) {
  1559. maxTemp = temp;
  1560. } else {
  1561. minTemp = temp;
  1562. }
  1563. }
  1564. return round(temp);
  1565. };
  1566. chroma.temperature = chroma.kelvin = function() {
  1567. return (function(func, args, ctor) {
  1568. ctor.prototype = func.prototype;
  1569. var child = new ctor, result = func.apply(child, args);
  1570. return Object(result) === result ? result : child;
  1571. })(Color, slice.call(arguments).concat(['temperature']), function(){});
  1572. };
  1573. _input.temperature = _input.kelvin = _input.K = temperature2rgb;
  1574. Color.prototype.temperature = function() {
  1575. return rgb2temperature(this._rgb);
  1576. };
  1577. Color.prototype.kelvin = Color.prototype.temperature;
  1578. chroma.contrast = function(a, b) {
  1579. var l1, l2, ref, ref1;
  1580. if ((ref = type(a)) === 'string' || ref === 'number') {
  1581. a = new Color(a);
  1582. }
  1583. if ((ref1 = type(b)) === 'string' || ref1 === 'number') {
  1584. b = new Color(b);
  1585. }
  1586. l1 = a.luminance();
  1587. l2 = b.luminance();
  1588. if (l1 > l2) {
  1589. return (l1 + 0.05) / (l2 + 0.05);
  1590. } else {
  1591. return (l2 + 0.05) / (l1 + 0.05);
  1592. }
  1593. };
  1594. chroma.distance = function(a, b, mode) {
  1595. var d, i, l1, l2, ref, ref1, sum_sq;
  1596. if (mode == null) {
  1597. mode = 'lab';
  1598. }
  1599. if ((ref = type(a)) === 'string' || ref === 'number') {
  1600. a = new Color(a);
  1601. }
  1602. if ((ref1 = type(b)) === 'string' || ref1 === 'number') {
  1603. b = new Color(b);
  1604. }
  1605. l1 = a.get(mode);
  1606. l2 = b.get(mode);
  1607. sum_sq = 0;
  1608. for (i in l1) {
  1609. d = (l1[i] || 0) - (l2[i] || 0);
  1610. sum_sq += d * d;
  1611. }
  1612. return Math.sqrt(sum_sq);
  1613. };
  1614. chroma.deltaE = function(a, b, L, C) {
  1615. var L1, L2, a1, a2, b1, b2, c1, c2, c4, dH2, delA, delB, delC, delL, f, h1, ref, ref1, ref2, ref3, sc, sh, sl, t, v1, v2, v3;
  1616. if (L == null) {
  1617. L = 1;
  1618. }
  1619. if (C == null) {
  1620. C = 1;
  1621. }
  1622. if ((ref = type(a)) === 'string' || ref === 'number') {
  1623. a = new Color(a);
  1624. }
  1625. if ((ref1 = type(b)) === 'string' || ref1 === 'number') {
  1626. b = new Color(b);
  1627. }
  1628. ref2 = a.lab(), L1 = ref2[0], a1 = ref2[1], b1 = ref2[2];
  1629. ref3 = b.lab(), L2 = ref3[0], a2 = ref3[1], b2 = ref3[2];
  1630. c1 = sqrt(a1 * a1 + b1 * b1);
  1631. c2 = sqrt(a2 * a2 + b2 * b2);
  1632. sl = L1 < 16.0 ? 0.511 : (0.040975 * L1) / (1.0 + 0.01765 * L1);
  1633. sc = (0.0638 * c1) / (1.0 + 0.0131 * c1) + 0.638;
  1634. h1 = c1 < 0.000001 ? 0.0 : (atan2(b1, a1) * 180.0) / PI;
  1635. while (h1 < 0) {
  1636. h1 += 360;
  1637. }
  1638. while (h1 >= 360) {
  1639. h1 -= 360;
  1640. }
  1641. t = (h1 >= 164.0) && (h1 <= 345.0) ? 0.56 + abs(0.2 * cos((PI * (h1 + 168.0)) / 180.0)) : 0.36 + abs(0.4 * cos((PI * (h1 + 35.0)) / 180.0));
  1642. c4 = c1 * c1 * c1 * c1;
  1643. f = sqrt(c4 / (c4 + 1900.0));
  1644. sh = sc * (f * t + 1.0 - f);
  1645. delL = L1 - L2;
  1646. delC = c1 - c2;
  1647. delA = a1 - a2;
  1648. delB = b1 - b2;
  1649. dH2 = delA * delA + delB * delB - delC * delC;
  1650. v1 = delL / (L * sl);
  1651. v2 = delC / (C * sc);
  1652. v3 = sh;
  1653. return sqrt(v1 * v1 + v2 * v2 + (dH2 / (v3 * v3)));
  1654. };
  1655. Color.prototype.get = function(modechan) {
  1656. var channel, i, me, mode, ref, src;
  1657. me = this;
  1658. ref = modechan.split('.'), mode = ref[0], channel = ref[1];
  1659. src = me[mode]();
  1660. if (channel) {
  1661. i = mode.indexOf(channel);
  1662. if (i > -1) {
  1663. return src[i];
  1664. } else {
  1665. return console.warn('unknown channel ' + channel + ' in mode ' + mode);
  1666. }
  1667. } else {
  1668. return src;
  1669. }
  1670. };
  1671. Color.prototype.set = function(modechan, value) {
  1672. var channel, i, me, mode, ref, src;
  1673. me = this;
  1674. ref = modechan.split('.'), mode = ref[0], channel = ref[1];
  1675. if (channel) {
  1676. src = me[mode]();
  1677. i = mode.indexOf(channel);
  1678. if (i > -1) {
  1679. if (type(value) === 'string') {
  1680. switch (value.charAt(0)) {
  1681. case '+':
  1682. src[i] += +value;
  1683. break;
  1684. case '-':
  1685. src[i] += +value;
  1686. break;
  1687. case '*':
  1688. src[i] *= +(value.substr(1));
  1689. break;
  1690. case '/':
  1691. src[i] /= +(value.substr(1));
  1692. break;
  1693. default:
  1694. src[i] = +value;
  1695. }
  1696. } else {
  1697. src[i] = value;
  1698. }
  1699. } else {
  1700. console.warn('unknown channel ' + channel + ' in mode ' + mode);
  1701. }
  1702. } else {
  1703. src = value;
  1704. }
  1705. return chroma(src, mode).alpha(me.alpha());
  1706. };
  1707. Color.prototype.clipped = function() {
  1708. return this._rgb._clipped || false;
  1709. };
  1710. Color.prototype.alpha = function(a) {
  1711. if (arguments.length) {
  1712. return chroma.rgb([this._rgb[0], this._rgb[1], this._rgb[2], a]);
  1713. }
  1714. return this._rgb[3];
  1715. };
  1716. Color.prototype.darken = function(amount) {
  1717. var lab, me;
  1718. if (amount == null) {
  1719. amount = 1;
  1720. }
  1721. me = this;
  1722. lab = me.lab();
  1723. lab[0] -= LAB_CONSTANTS.Kn * amount;
  1724. return chroma.lab(lab).alpha(me.alpha());
  1725. };
  1726. Color.prototype.brighten = function(amount) {
  1727. if (amount == null) {
  1728. amount = 1;
  1729. }
  1730. return this.darken(-amount);
  1731. };
  1732. Color.prototype.darker = Color.prototype.darken;
  1733. Color.prototype.brighter = Color.prototype.brighten;
  1734. Color.prototype.saturate = function(amount) {
  1735. var lch, me;
  1736. if (amount == null) {
  1737. amount = 1;
  1738. }
  1739. me = this;
  1740. lch = me.lch();
  1741. lch[1] += amount * LAB_CONSTANTS.Kn;
  1742. if (lch[1] < 0) {
  1743. lch[1] = 0;
  1744. }
  1745. return chroma.lch(lch).alpha(me.alpha());
  1746. };
  1747. Color.prototype.desaturate = function(amount) {
  1748. if (amount == null) {
  1749. amount = 1;
  1750. }
  1751. return this.saturate(-amount);
  1752. };
  1753. Color.prototype.premultiply = function() {
  1754. var a, rgb;
  1755. rgb = this.rgb();
  1756. a = this.alpha();
  1757. return chroma(rgb[0] * a, rgb[1] * a, rgb[2] * a, a);
  1758. };
  1759. blend = function(bottom, top, mode) {
  1760. if (!blend[mode]) {
  1761. throw 'unknown blend mode ' + mode;
  1762. }
  1763. return blend[mode](bottom, top);
  1764. };
  1765. blend_f = function(f) {
  1766. return function(bottom, top) {
  1767. var c0, c1;
  1768. c0 = chroma(top).rgb();
  1769. c1 = chroma(bottom).rgb();
  1770. return chroma(f(c0, c1), 'rgb');
  1771. };
  1772. };
  1773. each = function(f) {
  1774. return function(c0, c1) {
  1775. var i, o, out;
  1776. out = [];
  1777. for (i = o = 0; o <= 3; i = ++o) {
  1778. out[i] = f(c0[i], c1[i]);
  1779. }
  1780. return out;
  1781. };
  1782. };
  1783. normal = function(a, b) {
  1784. return a;
  1785. };
  1786. multiply = function(a, b) {
  1787. return a * b / 255;
  1788. };
  1789. darken = function(a, b) {
  1790. if (a > b) {
  1791. return b;
  1792. } else {
  1793. return a;
  1794. }
  1795. };
  1796. lighten = function(a, b) {
  1797. if (a > b) {
  1798. return a;
  1799. } else {
  1800. return b;
  1801. }
  1802. };
  1803. screen = function(a, b) {
  1804. return 255 * (1 - (1 - a / 255) * (1 - b / 255));
  1805. };
  1806. overlay = function(a, b) {
  1807. if (b < 128) {
  1808. return 2 * a * b / 255;
  1809. } else {
  1810. return 255 * (1 - 2 * (1 - a / 255) * (1 - b / 255));
  1811. }
  1812. };
  1813. burn = function(a, b) {
  1814. return 255 * (1 - (1 - b / 255) / (a / 255));
  1815. };
  1816. dodge = function(a, b) {
  1817. if (a === 255) {
  1818. return 255;
  1819. }
  1820. a = 255 * (b / 255) / (1 - a / 255);
  1821. if (a > 255) {
  1822. return 255;
  1823. } else {
  1824. return a;
  1825. }
  1826. };
  1827. blend.normal = blend_f(each(normal));
  1828. blend.multiply = blend_f(each(multiply));
  1829. blend.screen = blend_f(each(screen));
  1830. blend.overlay = blend_f(each(overlay));
  1831. blend.darken = blend_f(each(darken));
  1832. blend.lighten = blend_f(each(lighten));
  1833. blend.dodge = blend_f(each(dodge));
  1834. blend.burn = blend_f(each(burn));
  1835. chroma.blend = blend;
  1836. chroma.analyze = function(data) {
  1837. var len, o, r, val;
  1838. r = {
  1839. min: Number.MAX_VALUE,
  1840. max: Number.MAX_VALUE * -1,
  1841. sum: 0,
  1842. values: [],
  1843. count: 0
  1844. };
  1845. for (o = 0, len = data.length; o < len; o++) {
  1846. val = data[o];
  1847. if ((val != null) && !isNaN(val)) {
  1848. r.values.push(val);
  1849. r.sum += val;
  1850. if (val < r.min) {
  1851. r.min = val;
  1852. }
  1853. if (val > r.max) {
  1854. r.max = val;
  1855. }
  1856. r.count += 1;
  1857. }
  1858. }
  1859. r.domain = [r.min, r.max];
  1860. r.limits = function(mode, num) {
  1861. return chroma.limits(r, mode, num);
  1862. };
  1863. return r;
  1864. };
  1865. chroma.scale = function(colors, positions) {
  1866. var _classes, _colorCache, _colors, _correctLightness, _domain, _fixed, _gamma, _max, _min, _mode, _nacol, _out, _padding, _pos, _spread, _useCache, classifyValue, f, getClass, getColor, resetCache, setColors, tmap;
  1867. _mode = 'rgb';
  1868. _nacol = chroma('#ccc');
  1869. _spread = 0;
  1870. _fixed = false;
  1871. _domain = [0, 1];
  1872. _pos = [];
  1873. _padding = [0, 0];
  1874. _classes = false;
  1875. _colors = [];
  1876. _out = false;
  1877. _min = 0;
  1878. _max = 1;
  1879. _correctLightness = false;
  1880. _colorCache = {};
  1881. _useCache = true;
  1882. _gamma = 1;
  1883. setColors = function(colors) {
  1884. var c, col, o, ref, ref1, w;
  1885. if (colors == null) {
  1886. colors = ['#fff', '#000'];
  1887. }
  1888. if ((colors != null) && type(colors) === 'string' && (chroma.brewer != null)) {
  1889. colors = chroma.brewer[colors] || chroma.brewer[colors.toLowerCase()] || colors;
  1890. }
  1891. if (type(colors) === 'array') {
  1892. if (colors.length === 1) {
  1893. colors = [colors[0], colors[0]];
  1894. }
  1895. colors = colors.slice(0);
  1896. for (c = o = 0, ref = colors.length - 1; 0 <= ref ? o <= ref : o >= ref; c = 0 <= ref ? ++o : --o) {
  1897. col = colors[c];
  1898. if (type(col) === "string") {
  1899. colors[c] = chroma(col);
  1900. }
  1901. }
  1902. _pos.length = 0;
  1903. for (c = w = 0, ref1 = colors.length - 1; 0 <= ref1 ? w <= ref1 : w >= ref1; c = 0 <= ref1 ? ++w : --w) {
  1904. _pos.push(c / (colors.length - 1));
  1905. }
  1906. }
  1907. resetCache();
  1908. return _colors = colors;
  1909. };
  1910. getClass = function(value) {
  1911. var i, n;
  1912. if (_classes != null) {
  1913. n = _classes.length - 1;
  1914. i = 0;
  1915. while (i < n && value >= _classes[i]) {
  1916. i++;
  1917. }
  1918. return i - 1;
  1919. }
  1920. return 0;
  1921. };
  1922. tmap = function(t) {
  1923. return t;
  1924. };
  1925. classifyValue = function(value) {
  1926. var i, maxc, minc, n, val;
  1927. val = value;
  1928. if (_classes.length > 2) {
  1929. n = _classes.length - 1;
  1930. i = getClass(value);
  1931. minc = _classes[0] + (_classes[1] - _classes[0]) * (0 + _spread * 0.5);
  1932. maxc = _classes[n - 1] + (_classes[n] - _classes[n - 1]) * (1 - _spread * 0.5);
  1933. val = _min + ((_classes[i] + (_classes[i + 1] - _classes[i]) * 0.5 - minc) / (maxc - minc)) * (_max - _min);
  1934. }
  1935. return val;
  1936. };
  1937. getColor = function(val, bypassMap) {
  1938. var c, col, i, k, o, p, ref, t;
  1939. if (bypassMap == null) {
  1940. bypassMap = false;
  1941. }
  1942. if (isNaN(val)) {
  1943. return _nacol;
  1944. }
  1945. if (!bypassMap) {
  1946. if (_classes && _classes.length > 2) {
  1947. c = getClass(val);
  1948. t = c / (_classes.length - 2);
  1949. } else if (_max !== _min) {
  1950. t = (val - _min) / (_max - _min);
  1951. } else {
  1952. t = 1;
  1953. }
  1954. } else {
  1955. t = val;
  1956. }
  1957. if (!bypassMap) {
  1958. t = tmap(t);
  1959. }
  1960. if (_gamma !== 1) {
  1961. t = pow(t, _gamma);
  1962. }
  1963. t = _padding[0] + (t * (1 - _padding[0] - _padding[1]));
  1964. t = Math.min(1, Math.max(0, t));
  1965. k = Math.floor(t * 10000);
  1966. if (_useCache && _colorCache[k]) {
  1967. col = _colorCache[k];
  1968. } else {
  1969. if (type(_colors) === 'array') {
  1970. for (i = o = 0, ref = _pos.length - 1; 0 <= ref ? o <= ref : o >= ref; i = 0 <= ref ? ++o : --o) {
  1971. p = _pos[i];
  1972. if (t <= p) {
  1973. col = _colors[i];
  1974. break;
  1975. }
  1976. if (t >= p && i === _pos.length - 1) {
  1977. col = _colors[i];
  1978. break;
  1979. }
  1980. if (t > p && t < _pos[i + 1]) {
  1981. t = (t - p) / (_pos[i + 1] - p);
  1982. col = chroma.interpolate(_colors[i], _colors[i + 1], t, _mode);
  1983. break;
  1984. }
  1985. }
  1986. } else if (type(_colors) === 'function') {
  1987. col = _colors(t);
  1988. }
  1989. if (_useCache) {
  1990. _colorCache[k] = col;
  1991. }
  1992. }
  1993. return col;
  1994. };
  1995. resetCache = function() {
  1996. return _colorCache = {};
  1997. };
  1998. setColors(colors);
  1999. f = function(v) {
  2000. var c;
  2001. c = chroma(getColor(v));
  2002. if (_out && c[_out]) {
  2003. return c[_out]();
  2004. } else {
  2005. return c;
  2006. }
  2007. };
  2008. f.classes = function(classes) {
  2009. var d;
  2010. if (classes != null) {
  2011. if (type(classes) === 'array') {
  2012. _classes = classes;
  2013. _domain = [classes[0], classes[classes.length - 1]];
  2014. } else {
  2015. d = chroma.analyze(_domain);
  2016. if (classes === 0) {
  2017. _classes = [d.min, d.max];
  2018. } else {
  2019. _classes = chroma.limits(d, 'e', classes);
  2020. }
  2021. }
  2022. return f;
  2023. }
  2024. return _classes;
  2025. };
  2026. f.domain = function(domain) {
  2027. var c, d, k, len, o, ref, w;
  2028. if (!arguments.length) {
  2029. return _domain;
  2030. }
  2031. _min = domain[0];
  2032. _max = domain[domain.length - 1];
  2033. _pos = [];
  2034. k = _colors.length;
  2035. if (domain.length === k && _min !== _max) {
  2036. for (o = 0, len = domain.length; o < len; o++) {
  2037. d = domain[o];
  2038. _pos.push((d - _min) / (_max - _min));
  2039. }
  2040. } else {
  2041. for (c = w = 0, ref = k - 1; 0 <= ref ? w <= ref : w >= ref; c = 0 <= ref ? ++w : --w) {
  2042. _pos.push(c / (k - 1));
  2043. }
  2044. }
  2045. _domain = [_min, _max];
  2046. return f;
  2047. };
  2048. f.mode = function(_m) {
  2049. if (!arguments.length) {
  2050. return _mode;
  2051. }
  2052. _mode = _m;
  2053. resetCache();
  2054. return f;
  2055. };
  2056. f.range = function(colors, _pos) {
  2057. setColors(colors, _pos);
  2058. return f;
  2059. };
  2060. f.out = function(_o) {
  2061. _out = _o;
  2062. return f;
  2063. };
  2064. f.spread = function(val) {
  2065. if (!arguments.length) {
  2066. return _spread;
  2067. }
  2068. _spread = val;
  2069. return f;
  2070. };
  2071. f.correctLightness = function(v) {
  2072. if (v == null) {
  2073. v = true;
  2074. }
  2075. _correctLightness = v;
  2076. resetCache();
  2077. if (_correctLightness) {
  2078. tmap = function(t) {
  2079. var L0, L1, L_actual, L_diff, L_ideal, max_iter, pol, t0, t1;
  2080. L0 = getColor(0, true).lab()[0];
  2081. L1 = getColor(1, true).lab()[0];
  2082. pol = L0 > L1;
  2083. L_actual = getColor(t, true).lab()[0];
  2084. L_ideal = L0 + (L1 - L0) * t;
  2085. L_diff = L_actual - L_ideal;
  2086. t0 = 0;
  2087. t1 = 1;
  2088. max_iter = 20;
  2089. while (Math.abs(L_diff) > 1e-2 && max_iter-- > 0) {
  2090. (function() {
  2091. if (pol) {
  2092. L_diff *= -1;
  2093. }
  2094. if (L_diff < 0) {
  2095. t0 = t;
  2096. t += (t1 - t) * 0.5;
  2097. } else {
  2098. t1 = t;
  2099. t += (t0 - t) * 0.5;
  2100. }
  2101. L_actual = getColor(t, true).lab()[0];
  2102. return L_diff = L_actual - L_ideal;
  2103. })();
  2104. }
  2105. return t;
  2106. };
  2107. } else {
  2108. tmap = function(t) {
  2109. return t;
  2110. };
  2111. }
  2112. return f;
  2113. };
  2114. f.padding = function(p) {
  2115. if (p != null) {
  2116. if (type(p) === 'number') {
  2117. p = [p, p];
  2118. }
  2119. _padding = p;
  2120. return f;
  2121. } else {
  2122. return _padding;
  2123. }
  2124. };
  2125. f.colors = function(numColors, out) {
  2126. var dd, dm, i, o, ref, result, results, samples, w;
  2127. if (arguments.length < 2) {
  2128. out = 'hex';
  2129. }
  2130. result = [];
  2131. if (arguments.length === 0) {
  2132. result = _colors.slice(0);
  2133. } else if (numColors === 1) {
  2134. result = [f(0.5)];
  2135. } else if (numColors > 1) {
  2136. dm = _domain[0];
  2137. dd = _domain[1] - dm;
  2138. result = (function() {
  2139. results = [];
  2140. for (var o = 0; 0 <= numColors ? o < numColors : o > numColors; 0 <= numColors ? o++ : o--){ results.push(o); }
  2141. return results;
  2142. }).apply(this).map(function(i) {
  2143. return f(dm + i / (numColors - 1) * dd);
  2144. });
  2145. } else {
  2146. colors = [];
  2147. samples = [];
  2148. if (_classes && _classes.length > 2) {
  2149. for (i = w = 1, ref = _classes.length; 1 <= ref ? w < ref : w > ref; i = 1 <= ref ? ++w : --w) {
  2150. samples.push((_classes[i - 1] + _classes[i]) * 0.5);
  2151. }
  2152. } else {
  2153. samples = _domain;
  2154. }
  2155. result = samples.map(function(v) {
  2156. return f(v);
  2157. });
  2158. }
  2159. if (chroma[out]) {
  2160. result = result.map(function(c) {
  2161. return c[out]();
  2162. });
  2163. }
  2164. return result;
  2165. };
  2166. f.cache = function(c) {
  2167. if (c != null) {
  2168. _useCache = c;
  2169. return f;
  2170. } else {
  2171. return _useCache;
  2172. }
  2173. };
  2174. f.gamma = function(g) {
  2175. if (g != null) {
  2176. _gamma = g;
  2177. return f;
  2178. } else {
  2179. return _gamma;
  2180. }
  2181. };
  2182. return f;
  2183. };
  2184. if (chroma.scales == null) {
  2185. chroma.scales = {};
  2186. }
  2187. chroma.scales.cool = function() {
  2188. return chroma.scale([chroma.hsl(180, 1, .9), chroma.hsl(250, .7, .4)]);
  2189. };
  2190. chroma.scales.hot = function() {
  2191. return chroma.scale(['#000', '#f00', '#ff0', '#fff'], [0, .25, .75, 1]).mode('rgb');
  2192. };
  2193. chroma.analyze = function(data, key, filter) {
  2194. var add, k, len, o, r, val, visit;
  2195. r = {
  2196. min: Number.MAX_VALUE,
  2197. max: Number.MAX_VALUE * -1,
  2198. sum: 0,
  2199. values: [],
  2200. count: 0
  2201. };
  2202. if (filter == null) {
  2203. filter = function() {
  2204. return true;
  2205. };
  2206. }
  2207. add = function(val) {
  2208. if ((val != null) && !isNaN(val)) {
  2209. r.values.push(val);
  2210. r.sum += val;
  2211. if (val < r.min) {
  2212. r.min = val;
  2213. }
  2214. if (val > r.max) {
  2215. r.max = val;
  2216. }
  2217. r.count += 1;
  2218. }
  2219. };
  2220. visit = function(val, k) {
  2221. if (filter(val, k)) {
  2222. if ((key != null) && type(key) === 'function') {
  2223. return add(key(val));
  2224. } else if ((key != null) && type(key) === 'string' || type(key) === 'number') {
  2225. return add(val[key]);
  2226. } else {
  2227. return add(val);
  2228. }
  2229. }
  2230. };
  2231. if (type(data) === 'array') {
  2232. for (o = 0, len = data.length; o < len; o++) {
  2233. val = data[o];
  2234. visit(val);
  2235. }
  2236. } else {
  2237. for (k in data) {
  2238. val = data[k];
  2239. visit(val, k);
  2240. }
  2241. }
  2242. r.domain = [r.min, r.max];
  2243. r.limits = function(mode, num) {
  2244. return chroma.limits(r, mode, num);
  2245. };
  2246. return r;
  2247. };
  2248. chroma.limits = function(data, mode, num) {
  2249. var aa, ab, ac, ad, ae, af, ag, ah, ai, aj, ak, al, am, assignments, best, centroids, cluster, clusterSizes, dist, i, j, kClusters, limits, max_log, min, min_log, mindist, n, nb_iters, newCentroids, o, p, pb, pr, ref, ref1, ref10, ref11, ref12, ref13, ref14, ref2, ref3, ref4, ref5, ref6, ref7, ref8, ref9, repeat, sum, tmpKMeansBreaks, v, value, values, w;
  2250. if (mode == null) {
  2251. mode = 'equal';
  2252. }
  2253. if (num == null) {
  2254. num = 7;
  2255. }
  2256. if (type(data) === 'array') {
  2257. data = chroma.analyze(data);
  2258. }
  2259. min = data.min;
  2260. max = data.max;
  2261. sum = data.sum;
  2262. values = data.values.sort(function(a, b) {
  2263. return a - b;
  2264. });
  2265. if (num === 1) {
  2266. return [min, max];
  2267. }
  2268. limits = [];
  2269. if (mode.substr(0, 1) === 'c') {
  2270. limits.push(min);
  2271. limits.push(max);
  2272. }
  2273. if (mode.substr(0, 1) === 'e') {
  2274. limits.push(min);
  2275. for (i = o = 1, ref = num - 1; 1 <= ref ? o <= ref : o >= ref; i = 1 <= ref ? ++o : --o) {
  2276. limits.push(min + (i / num) * (max - min));
  2277. }
  2278. limits.push(max);
  2279. } else if (mode.substr(0, 1) === 'l') {
  2280. if (min <= 0) {
  2281. throw 'Logarithmic scales are only possible for values > 0';
  2282. }
  2283. min_log = Math.LOG10E * log(min);
  2284. max_log = Math.LOG10E * log(max);
  2285. limits.push(min);
  2286. for (i = w = 1, ref1 = num - 1; 1 <= ref1 ? w <= ref1 : w >= ref1; i = 1 <= ref1 ? ++w : --w) {
  2287. limits.push(pow(10, min_log + (i / num) * (max_log - min_log)));
  2288. }
  2289. limits.push(max);
  2290. } else if (mode.substr(0, 1) === 'q') {
  2291. limits.push(min);
  2292. for (i = aa = 1, ref2 = num - 1; 1 <= ref2 ? aa <= ref2 : aa >= ref2; i = 1 <= ref2 ? ++aa : --aa) {
  2293. p = (values.length - 1) * i / num;
  2294. pb = floor(p);
  2295. if (pb === p) {
  2296. limits.push(values[pb]);
  2297. } else {
  2298. pr = p - pb;
  2299. limits.push(values[pb] * (1 - pr) + values[pb + 1] * pr);
  2300. }
  2301. }
  2302. limits.push(max);
  2303. } else if (mode.substr(0, 1) === 'k') {
  2304. /*
  2305. implementation based on
  2306. http://code.google.com/p/figue/source/browse/trunk/figue.js#336
  2307. simplified for 1-d input values
  2308. */
  2309. n = values.length;
  2310. assignments = new Array(n);
  2311. clusterSizes = new Array(num);
  2312. repeat = true;
  2313. nb_iters = 0;
  2314. centroids = null;
  2315. centroids = [];
  2316. centroids.push(min);
  2317. for (i = ab = 1, ref3 = num - 1; 1 <= ref3 ? ab <= ref3 : ab >= ref3; i = 1 <= ref3 ? ++ab : --ab) {
  2318. centroids.push(min + (i / num) * (max - min));
  2319. }
  2320. centroids.push(max);
  2321. while (repeat) {
  2322. for (j = ac = 0, ref4 = num - 1; 0 <= ref4 ? ac <= ref4 : ac >= ref4; j = 0 <= ref4 ? ++ac : --ac) {
  2323. clusterSizes[j] = 0;
  2324. }
  2325. for (i = ad = 0, ref5 = n - 1; 0 <= ref5 ? ad <= ref5 : ad >= ref5; i = 0 <= ref5 ? ++ad : --ad) {
  2326. value = values[i];
  2327. mindist = Number.MAX_VALUE;
  2328. for (j = ae = 0, ref6 = num - 1; 0 <= ref6 ? ae <= ref6 : ae >= ref6; j = 0 <= ref6 ? ++ae : --ae) {
  2329. dist = abs(centroids[j] - value);
  2330. if (dist < mindist) {
  2331. mindist = dist;
  2332. best = j;
  2333. }
  2334. }
  2335. clusterSizes[best]++;
  2336. assignments[i] = best;
  2337. }
  2338. newCentroids = new Array(num);
  2339. for (j = af = 0, ref7 = num - 1; 0 <= ref7 ? af <= ref7 : af >= ref7; j = 0 <= ref7 ? ++af : --af) {
  2340. newCentroids[j] = null;
  2341. }
  2342. for (i = ag = 0, ref8 = n - 1; 0 <= ref8 ? ag <= ref8 : ag >= ref8; i = 0 <= ref8 ? ++ag : --ag) {
  2343. cluster = assignments[i];
  2344. if (newCentroids[cluster] === null) {
  2345. newCentroids[cluster] = values[i];
  2346. } else {
  2347. newCentroids[cluster] += values[i];
  2348. }
  2349. }
  2350. for (j = ah = 0, ref9 = num - 1; 0 <= ref9 ? ah <= ref9 : ah >= ref9; j = 0 <= ref9 ? ++ah : --ah) {
  2351. newCentroids[j] *= 1 / clusterSizes[j];
  2352. }
  2353. repeat = false;
  2354. for (j = ai = 0, ref10 = num - 1; 0 <= ref10 ? ai <= ref10 : ai >= ref10; j = 0 <= ref10 ? ++ai : --ai) {
  2355. if (newCentroids[j] !== centroids[i]) {
  2356. repeat = true;
  2357. break;
  2358. }
  2359. }
  2360. centroids = newCentroids;
  2361. nb_iters++;
  2362. if (nb_iters > 200) {
  2363. repeat = false;
  2364. }
  2365. }
  2366. kClusters = {};
  2367. for (j = aj = 0, ref11 = num - 1; 0 <= ref11 ? aj <= ref11 : aj >= ref11; j = 0 <= ref11 ? ++aj : --aj) {
  2368. kClusters[j] = [];
  2369. }
  2370. for (i = ak = 0, ref12 = n - 1; 0 <= ref12 ? ak <= ref12 : ak >= ref12; i = 0 <= ref12 ? ++ak : --ak) {
  2371. cluster = assignments[i];
  2372. kClusters[cluster].push(values[i]);
  2373. }
  2374. tmpKMeansBreaks = [];
  2375. for (j = al = 0, ref13 = num - 1; 0 <= ref13 ? al <= ref13 : al >= ref13; j = 0 <= ref13 ? ++al : --al) {
  2376. tmpKMeansBreaks.push(kClusters[j][0]);
  2377. tmpKMeansBreaks.push(kClusters[j][kClusters[j].length - 1]);
  2378. }
  2379. tmpKMeansBreaks = tmpKMeansBreaks.sort(function(a, b) {
  2380. return a - b;
  2381. });
  2382. limits.push(tmpKMeansBreaks[0]);
  2383. for (i = am = 1, ref14 = tmpKMeansBreaks.length - 1; am <= ref14; i = am += 2) {
  2384. v = tmpKMeansBreaks[i];
  2385. if (!isNaN(v) && limits.indexOf(v) === -1) {
  2386. limits.push(v);
  2387. }
  2388. }
  2389. }
  2390. return limits;
  2391. };
  2392. hsi2rgb = function(h, s, i) {
  2393. /*
  2394. borrowed from here:
  2395. http://hummer.stanford.edu/museinfo/doc/examples/humdrum/keyscape2/hsi2rgb.cpp
  2396. */
  2397. var args, b, g, r;
  2398. args = unpack(arguments);
  2399. h = args[0], s = args[1], i = args[2];
  2400. if (isNaN(h)) {
  2401. h = 0;
  2402. }
  2403. h /= 360;
  2404. if (h < 1 / 3) {
  2405. b = (1 - s) / 3;
  2406. r = (1 + s * cos(TWOPI * h) / cos(PITHIRD - TWOPI * h)) / 3;
  2407. g = 1 - (b + r);
  2408. } else if (h < 2 / 3) {
  2409. h -= 1 / 3;
  2410. r = (1 - s) / 3;
  2411. g = (1 + s * cos(TWOPI * h) / cos(PITHIRD - TWOPI * h)) / 3;
  2412. b = 1 - (r + g);
  2413. } else {
  2414. h -= 2 / 3;
  2415. g = (1 - s) / 3;
  2416. b = (1 + s * cos(TWOPI * h) / cos(PITHIRD - TWOPI * h)) / 3;
  2417. r = 1 - (g + b);
  2418. }
  2419. r = limit(i * r * 3);
  2420. g = limit(i * g * 3);
  2421. b = limit(i * b * 3);
  2422. return [r * 255, g * 255, b * 255, args.length > 3 ? args[3] : 1];
  2423. };
  2424. rgb2hsi = function() {
  2425. /*
  2426. borrowed from here:
  2427. http://hummer.stanford.edu/museinfo/doc/examples/humdrum/keyscape2/rgb2hsi.cpp
  2428. */
  2429. var b, g, h, i, min, r, ref, s;
  2430. ref = unpack(arguments), r = ref[0], g = ref[1], b = ref[2];
  2431. TWOPI = Math.PI * 2;
  2432. r /= 255;
  2433. g /= 255;
  2434. b /= 255;
  2435. min = Math.min(r, g, b);
  2436. i = (r + g + b) / 3;
  2437. s = 1 - min / i;
  2438. if (s === 0) {
  2439. h = 0;
  2440. } else {
  2441. h = ((r - g) + (r - b)) / 2;
  2442. h /= Math.sqrt((r - g) * (r - g) + (r - b) * (g - b));
  2443. h = Math.acos(h);
  2444. if (b > g) {
  2445. h = TWOPI - h;
  2446. }
  2447. h /= TWOPI;
  2448. }
  2449. return [h * 360, s, i];
  2450. };
  2451. chroma.hsi = function() {
  2452. return (function(func, args, ctor) {
  2453. ctor.prototype = func.prototype;
  2454. var child = new ctor, result = func.apply(child, args);
  2455. return Object(result) === result ? result : child;
  2456. })(Color, slice.call(arguments).concat(['hsi']), function(){});
  2457. };
  2458. _input.hsi = hsi2rgb;
  2459. Color.prototype.hsi = function() {
  2460. return rgb2hsi(this._rgb);
  2461. };
  2462. interpolate_hsx = function(col1, col2, f, m) {
  2463. var dh, hue, hue0, hue1, lbv, lbv0, lbv1, res, sat, sat0, sat1, xyz0, xyz1;
  2464. if (m === 'hsl') {
  2465. xyz0 = col1.hsl();
  2466. xyz1 = col2.hsl();
  2467. } else if (m === 'hsv') {
  2468. xyz0 = col1.hsv();
  2469. xyz1 = col2.hsv();
  2470. } else if (m === 'hcg') {
  2471. xyz0 = col1.hcg();
  2472. xyz1 = col2.hcg();
  2473. } else if (m === 'hsi') {
  2474. xyz0 = col1.hsi();
  2475. xyz1 = col2.hsi();
  2476. } else if (m === 'lch' || m === 'hcl') {
  2477. m = 'hcl';
  2478. xyz0 = col1.hcl();
  2479. xyz1 = col2.hcl();
  2480. }
  2481. if (m.substr(0, 1) === 'h') {
  2482. hue0 = xyz0[0], sat0 = xyz0[1], lbv0 = xyz0[2];
  2483. hue1 = xyz1[0], sat1 = xyz1[1], lbv1 = xyz1[2];
  2484. }
  2485. if (!isNaN(hue0) && !isNaN(hue1)) {
  2486. if (hue1 > hue0 && hue1 - hue0 > 180) {
  2487. dh = hue1 - (hue0 + 360);
  2488. } else if (hue1 < hue0 && hue0 - hue1 > 180) {
  2489. dh = hue1 + 360 - hue0;
  2490. } else {
  2491. dh = hue1 - hue0;
  2492. }
  2493. hue = hue0 + f * dh;
  2494. } else if (!isNaN(hue0)) {
  2495. hue = hue0;
  2496. if ((lbv1 === 1 || lbv1 === 0) && m !== 'hsv') {
  2497. sat = sat0;
  2498. }
  2499. } else if (!isNaN(hue1)) {
  2500. hue = hue1;
  2501. if ((lbv0 === 1 || lbv0 === 0) && m !== 'hsv') {
  2502. sat = sat1;
  2503. }
  2504. } else {
  2505. hue = Number.NaN;
  2506. }
  2507. if (sat == null) {
  2508. sat = sat0 + f * (sat1 - sat0);
  2509. }
  2510. lbv = lbv0 + f * (lbv1 - lbv0);
  2511. return res = chroma[m](hue, sat, lbv);
  2512. };
  2513. _interpolators = _interpolators.concat((function() {
  2514. var len, o, ref, results;
  2515. ref = ['hsv', 'hsl', 'hsi', 'hcl', 'lch', 'hcg'];
  2516. results = [];
  2517. for (o = 0, len = ref.length; o < len; o++) {
  2518. m = ref[o];
  2519. results.push([m, interpolate_hsx]);
  2520. }
  2521. return results;
  2522. })());
  2523. interpolate_num = function(col1, col2, f, m) {
  2524. var n1, n2;
  2525. n1 = col1.num();
  2526. n2 = col2.num();
  2527. return chroma.num(n1 + (n2 - n1) * f, 'num');
  2528. };
  2529. _interpolators.push(['num', interpolate_num]);
  2530. interpolate_lab = function(col1, col2, f, m) {
  2531. var res, xyz0, xyz1;
  2532. xyz0 = col1.lab();
  2533. xyz1 = col2.lab();
  2534. return res = new Color(xyz0[0] + f * (xyz1[0] - xyz0[0]), xyz0[1] + f * (xyz1[1] - xyz0[1]), xyz0[2] + f * (xyz1[2] - xyz0[2]), m);
  2535. };
  2536. _interpolators.push(['lab', interpolate_lab]);
  2537. }).call(this);