chartkick 3.0.2 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of chartkick might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cf89880ad6a8a0c9b53e5228728c822f44be3182161fa05b495fe52cfd026029
4
- data.tar.gz: f725a4ee00ab4112736f57df142a2658e8cca93eaec60c18a6df726a2a9999ac
3
+ metadata.gz: a982e8c986d3bfdf50bc533ccd2f4530a364a6155362b1d5ffa7217831be045c
4
+ data.tar.gz: 97a4e2f859c84942009fd98995ccbcf26b8c28d5c56a88727599c51a07f36352
5
5
  SHA512:
6
- metadata.gz: 2372401acb6103efb5f9a56694551a4c38dc29c9c1a1e6215f3f0f0e5fdadda37598047ec46fb6deee5a63a805d1c23bada4bfcd8b869ee4c036035722880285
7
- data.tar.gz: 99d1b491efc0e52dc94ee4737838b35059c60682bbd8bbf2c2edfb5fffdb1e78d84dd5aed1080217c5f4b93b70b6243f79bbf8b1141956af91ba2a2772a896d5
6
+ metadata.gz: cb7b936da97bd3bef92748667af865c21944759d14b7d95ae8c0b03f576502b85107ff866f538166c159ca0f23ad33390f9583d574bb672d1d49385369422ae8
7
+ data.tar.gz: 5cb58d1991fb3ad47d8ac2b3a90b78d9292a1ad82dada7ad27ee112039b41de5427ae794e5ddcf7a7260af6aadcf0e80be60587b4b42883ca927cb7dabcbcdae
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 3.1.0
2
+
3
+ - Updated Chartkick.js to 3.1.0
4
+ - Updated Chart.js to 2.8.0
5
+
1
6
  ## 3.0.2
2
7
 
3
8
  - Fixed error with `nonce` option with Secure Headers and Rails < 5.2
data/CONTRIBUTING.md CHANGED
@@ -33,6 +33,7 @@ If you’d like to discuss a new feature, create an issue and start the title wi
33
33
 
34
34
  Fork the project and create a pull request. A few tips:
35
35
 
36
+ - Submit JavaScript changes to the [Chartkick.js](https://github.com/ankane/chartkick.js) repo.
36
37
  - Keep changes to a minimum. If you have multiple features or fixes, submit multiple pull requests.
37
38
  - Follow the existing style. The code should read like it’s written by a single person.
38
39
 
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2013-2018 Andrew Kane
1
+ Copyright (c) 2013-2019 Andrew Kane
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -16,14 +16,29 @@ Add this line to your application's Gemfile:
16
16
  gem "chartkick"
17
17
  ```
18
18
 
19
- In `application.js`, add:
19
+ For Rails 6 / Webpacker, run:
20
+
21
+ ```sh
22
+ yarn add chartkick chart.js
23
+ ```
24
+
25
+ And in `app/javascript/packs/application.js`, add:
26
+
27
+ ```js
28
+ require("chartkick")
29
+ require("chart.js")
30
+ ```
31
+
32
+ > You can also use `import` instead of `require()`
33
+
34
+ For Rails 5 / Sprockets, in `app/assets/javascripts/application.js`, add:
20
35
 
21
36
  ```js
22
- //= require Chart.bundle
23
37
  //= require chartkick
38
+ //= require Chart.bundle
24
39
  ```
25
40
 
26
- This sets up Chartkick with Chart.js. For other charting libaries, see [detailed instructions](#installation).
41
+ This sets up Chartkick with Chart.js. For other charting libraries, see [detailed instructions](#installation).
27
42
 
28
43
  ## Charts
29
44
 
@@ -133,6 +148,12 @@ Min and max values
133
148
 
134
149
  `min` defaults to 0 for charts with non-negative values. Use `nil` to let the charting library decide.
135
150
 
151
+ Min and max for x-axis - *Chart.js* [master]
152
+
153
+ ```erb
154
+ <%= line_chart data, xmin: "2018-01-01", xmax: "2019-01-01" %>
155
+ ```
156
+
136
157
  Colors
137
158
 
138
159
  ```erb
@@ -274,18 +295,19 @@ You capture the JavaScript in a content block with:
274
295
  Chartkick.options[:content_for] = :charts_js
275
296
  ```
276
297
 
277
- Then, in your layout:
298
+ Then, in your layout, use:
278
299
 
279
300
  ```erb
280
- <%= yield :charts_js %> <!-- Rails -->
281
- <%= yield_content :charts_js %> <!-- Padrino -->
301
+ <%= yield :charts_js %>
282
302
  ```
283
303
 
304
+ > For Padrino, use `yield_content` instead of `yield`
305
+
284
306
  This is great for including all of your JavaScript at the bottom of the page.
285
307
 
286
308
  ### Data
287
309
 
288
- Pass data as a Hash or Array
310
+ Pass data as a hash or array
289
311
 
290
312
  ```erb
291
313
  <%= pie_chart({"Football" => 10, "Basketball" => 5}) %>
@@ -328,7 +350,7 @@ If you want to use the charting library directly, get the code with:
328
350
 
329
351
  The code will be logged to the JavaScript console.
330
352
 
331
- **Note:** JavaScript functions cannot be logged, so it may not be identical.
353
+ > JavaScript functions cannot be logged, so it may not be identical.
332
354
 
333
355
  ### Download Charts
334
356
 
@@ -340,20 +362,26 @@ Give users the ability to download charts. It all happens in the browser - no se
340
362
  <%= line_chart data, download: true %>
341
363
  ```
342
364
 
365
+ > Safari will open the image in a new window instead of downloading.
366
+
343
367
  Set the filename
344
368
 
345
369
  ```erb
346
370
  <%= line_chart data, download: {filename: "boom"} %>
347
371
  ```
348
372
 
349
- **Note:** Safari will open the image in a new window instead of downloading.
350
-
351
373
  Set the background color
352
374
 
353
375
  ```erb
354
376
  <%= line_chart data, download: {background: "#ffffff"} %>
355
377
  ```
356
378
 
379
+ Set title
380
+
381
+ ```erb
382
+ <%= line_chart data, title: "Awesome chart" %>
383
+ ```
384
+
357
385
  ## Installation
358
386
 
359
387
  Add this line to your application's Gemfile:
@@ -364,92 +392,97 @@ gem "chartkick"
364
392
 
365
393
  Next, choose your charting library.
366
394
 
367
- ### Charting Libraries
395
+ - [Chart.js](#chart-js)
396
+ - [Google Charts](#google-charts)
397
+ - [Highcharts](#highcharts)
368
398
 
369
- **Note:** In the instructions below, `application.js` must be included **before** the charts in your views, unless using the `:content_for` option.
399
+ > In the instructions below, `application.js` must be included **before** the charts in your views, unless using the `:content_for` option.
370
400
 
371
- #### Chart.js
401
+ ### Chart.js
372
402
 
373
- In `application.js`, add:
403
+ For Rails 6 / Webpacker, run:
374
404
 
375
- ```js
376
- //= require Chart.bundle
377
- //= require chartkick
405
+ ```sh
406
+ yarn add chartkick chart.js
378
407
  ```
379
408
 
380
- #### Google Charts
409
+ And in `app/javascript/packs/application.js`, add:
381
410
 
382
- In `application.js`, add:
411
+ ```js
412
+ require("chartkick")
413
+ require("chart.js")
414
+ ```
415
+
416
+ For Rails 5 / Sprockets, in `app/assets/javascripts/application.js`, add:
383
417
 
384
418
  ```js
385
419
  //= require chartkick
420
+ //= require Chart.bundle
386
421
  ```
387
422
 
388
- In your views, before `application.js`, add:
423
+ ### Google Charts
424
+
425
+ In your layout or views, add:
389
426
 
390
427
  ```erb
391
428
  <%= javascript_include_tag "https://www.gstatic.com/charts/loader.js" %>
392
429
  ```
393
430
 
394
- #### Highcharts
431
+ For Rails 6 / Webpacker, run:
395
432
 
396
- Download [highcharts.js](https://code.highcharts.com/highcharts.js) into `vendor/assets/javascripts` (or use `yarn add highcharts` in Rails 5.1+).
433
+ ```sh
434
+ yarn add chartkick
435
+ ```
397
436
 
398
- In `application.js`, add:
437
+ And in `app/javascript/packs/application.js`, add:
399
438
 
400
439
  ```js
401
- //= require highcharts
402
- //= require chartkick
440
+ require("chartkick")
403
441
  ```
404
442
 
405
- Works with Highcharts 2.1+
443
+ For Rails 5 / Sprockets, in `app/assets/javascripts/application.js`, add:
406
444
 
407
- ### Webpacker
445
+ ```js
446
+ //= require chartkick
447
+ ```
408
448
 
409
- For Webpacker, use Yarn to install the JavaScript libraries:
449
+ To specify a language or Google Maps API key, use:
410
450
 
411
- ```sh
412
- yarn add chartkick chart.js # or highcharts
451
+ ```js
452
+ Chartkick.configure({language: "de", mapsApiKey: "..."})
413
453
  ```
414
454
 
415
- Then include them in your pack.
416
-
417
- ```es6
418
- import Chartkick from "chartkick";
419
- window.Chartkick = Chartkick;
455
+ before your charts.
420
456
 
421
- // for Chart.js
422
- import Chart from "chart.js";
423
- Chartkick.addAdapter(Chart);
457
+ ### Highcharts
424
458
 
425
- // for Highcharts
426
- import Highcharts from "highcharts";
427
- Chartkick.addAdapter(Highcharts);
459
+ For Rails 6 / Webpacker, run:
428
460
 
429
- // for Google Charts
430
- // just include https://www.gstatic.com/charts/loader.js in your views
461
+ ```sh
462
+ yarn add chartkick highcharts
431
463
  ```
432
464
 
433
- You pack must be included **before** the charts in your views, unless using the `:content_for` option.
465
+ And in `app/javascript/packs/application.js`, add:
434
466
 
435
- ### Sinatra and Padrino
467
+ ```js
468
+ require("chartkick").use(require("highcharts"))
469
+ ```
436
470
 
437
- You must include `chartkick.js` manually. [Download it here](https://raw.githubusercontent.com/ankane/chartkick/master/vendor/assets/javascripts/chartkick.js)
471
+ For Rails 5 / Sprockets, download [highcharts.js](https://code.highcharts.com/highcharts.js) into `vendor/assets/javascripts` (or use `yarn add highcharts` in Rails 5.1+), and in `app/assets/javascripts/application.js`, add:
438
472
 
439
- ```html
440
- <script src="chartkick.js"></script>
473
+ ```js
474
+ //= require chartkick
475
+ //= require highcharts
441
476
  ```
442
477
 
443
- ### Localization
478
+ ### Sinatra and Padrino
444
479
 
445
- To specify a language for Google Charts, add:
480
+ Download [chartkick.js](https://raw.githubusercontent.com/ankane/chartkick/master/vendor/assets/javascripts/chartkick.js) and include it manually.
446
481
 
447
- ```javascript
448
- Chartkick.configure({language: "de"});
482
+ ```html
483
+ <script src="chartkick.js"></script>
449
484
  ```
450
485
 
451
- after the JavaScript files and before your charts.
452
-
453
486
  ### Multiple Libraries
454
487
 
455
488
  If more than one charting library is loaded, choose between them with:
@@ -526,6 +559,7 @@ Check out [chartkick.js](https://github.com/ankane/chartkick.js)
526
559
  ## Tutorials
527
560
 
528
561
  - [Charts with Chartkick and Groupdate](https://gorails.com/episodes/charts-with-chartkick-and-groupdate)
562
+ - [Creando gráficos en Ruby on Rails con Chartkick y Chart.js](https://www.youtube.com/watch?v=W92AlkwQn3M)
529
563
  - [Make Easy Graphs and Charts on Rails with Chartkick](https://www.sitepoint.com/make-easy-graphs-and-charts-on-rails-with-chartkick/)
530
564
  - [Practical Graphs on Rails: Chartkick in Practice](https://www.sitepoint.com/graphs-on-rails-chartkick-in-practice/)
531
565
 
@@ -1,3 +1,3 @@
1
1
  module Chartkick
2
- VERSION = "3.0.2"
2
+ VERSION = "3.1.0"
3
3
  end
@@ -1,1264 +1,556 @@
1
1
  /*!
2
- * Chart.js
3
- * http://chartjs.org/
4
- * Version: 2.7.3
5
- *
6
- * Copyright 2018 Chart.js Contributors
7
- * Released under the MIT license
8
- * https://github.com/chartjs/Chart.js/blob/master/LICENSE.md
2
+ * Chart.js v2.8.0
3
+ * https://www.chartjs.org
4
+ * (c) 2019 Chart.js Contributors
5
+ * Released under the MIT License
9
6
  */
10
- (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Chart = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
11
- /* MIT license */
12
- var colorNames = require(5);
7
+ (function (global, factory) {
8
+ typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
9
+ typeof define === 'function' && define.amd ? define(factory) :
10
+ (global.Chart = factory());
11
+ }(this, (function () { 'use strict';
13
12
 
14
- module.exports = {
15
- getRgba: getRgba,
16
- getHsla: getHsla,
17
- getRgb: getRgb,
18
- getHsl: getHsl,
19
- getHwb: getHwb,
20
- getAlpha: getAlpha,
13
+ /* MIT license */
21
14
 
22
- hexString: hexString,
23
- rgbString: rgbString,
24
- rgbaString: rgbaString,
25
- percentString: percentString,
26
- percentaString: percentaString,
27
- hslString: hslString,
28
- hslaString: hslaString,
29
- hwbString: hwbString,
30
- keyword: keyword
31
- }
15
+ var conversions = {
16
+ rgb2hsl: rgb2hsl,
17
+ rgb2hsv: rgb2hsv,
18
+ rgb2hwb: rgb2hwb,
19
+ rgb2cmyk: rgb2cmyk,
20
+ rgb2keyword: rgb2keyword,
21
+ rgb2xyz: rgb2xyz,
22
+ rgb2lab: rgb2lab,
23
+ rgb2lch: rgb2lch,
32
24
 
33
- function getRgba(string) {
34
- if (!string) {
35
- return;
36
- }
37
- var abbr = /^#([a-fA-F0-9]{3})$/i,
38
- hex = /^#([a-fA-F0-9]{6})$/i,
39
- rgba = /^rgba?\(\s*([+-]?\d+)\s*,\s*([+-]?\d+)\s*,\s*([+-]?\d+)\s*(?:,\s*([+-]?[\d\.]+)\s*)?\)$/i,
40
- per = /^rgba?\(\s*([+-]?[\d\.]+)\%\s*,\s*([+-]?[\d\.]+)\%\s*,\s*([+-]?[\d\.]+)\%\s*(?:,\s*([+-]?[\d\.]+)\s*)?\)$/i,
41
- keyword = /(\w+)/;
25
+ hsl2rgb: hsl2rgb,
26
+ hsl2hsv: hsl2hsv,
27
+ hsl2hwb: hsl2hwb,
28
+ hsl2cmyk: hsl2cmyk,
29
+ hsl2keyword: hsl2keyword,
42
30
 
43
- var rgb = [0, 0, 0],
44
- a = 1,
45
- match = string.match(abbr);
46
- if (match) {
47
- match = match[1];
48
- for (var i = 0; i < rgb.length; i++) {
49
- rgb[i] = parseInt(match[i] + match[i], 16);
50
- }
51
- }
52
- else if (match = string.match(hex)) {
53
- match = match[1];
54
- for (var i = 0; i < rgb.length; i++) {
55
- rgb[i] = parseInt(match.slice(i * 2, i * 2 + 2), 16);
56
- }
57
- }
58
- else if (match = string.match(rgba)) {
59
- for (var i = 0; i < rgb.length; i++) {
60
- rgb[i] = parseInt(match[i + 1]);
61
- }
62
- a = parseFloat(match[4]);
63
- }
64
- else if (match = string.match(per)) {
65
- for (var i = 0; i < rgb.length; i++) {
66
- rgb[i] = Math.round(parseFloat(match[i + 1]) * 2.55);
67
- }
68
- a = parseFloat(match[4]);
69
- }
70
- else if (match = string.match(keyword)) {
71
- if (match[1] == "transparent") {
72
- return [0, 0, 0, 0];
73
- }
74
- rgb = colorNames[match[1]];
75
- if (!rgb) {
76
- return;
77
- }
78
- }
31
+ hsv2rgb: hsv2rgb,
32
+ hsv2hsl: hsv2hsl,
33
+ hsv2hwb: hsv2hwb,
34
+ hsv2cmyk: hsv2cmyk,
35
+ hsv2keyword: hsv2keyword,
79
36
 
80
- for (var i = 0; i < rgb.length; i++) {
81
- rgb[i] = scale(rgb[i], 0, 255);
82
- }
83
- if (!a && a != 0) {
84
- a = 1;
85
- }
86
- else {
87
- a = scale(a, 0, 1);
88
- }
89
- rgb[3] = a;
90
- return rgb;
91
- }
37
+ hwb2rgb: hwb2rgb,
38
+ hwb2hsl: hwb2hsl,
39
+ hwb2hsv: hwb2hsv,
40
+ hwb2cmyk: hwb2cmyk,
41
+ hwb2keyword: hwb2keyword,
92
42
 
93
- function getHsla(string) {
94
- if (!string) {
95
- return;
96
- }
97
- var hsl = /^hsla?\(\s*([+-]?\d+)(?:deg)?\s*,\s*([+-]?[\d\.]+)%\s*,\s*([+-]?[\d\.]+)%\s*(?:,\s*([+-]?[\d\.]+)\s*)?\)/;
98
- var match = string.match(hsl);
99
- if (match) {
100
- var alpha = parseFloat(match[4]);
101
- var h = scale(parseInt(match[1]), 0, 360),
102
- s = scale(parseFloat(match[2]), 0, 100),
103
- l = scale(parseFloat(match[3]), 0, 100),
104
- a = scale(isNaN(alpha) ? 1 : alpha, 0, 1);
105
- return [h, s, l, a];
106
- }
107
- }
43
+ cmyk2rgb: cmyk2rgb,
44
+ cmyk2hsl: cmyk2hsl,
45
+ cmyk2hsv: cmyk2hsv,
46
+ cmyk2hwb: cmyk2hwb,
47
+ cmyk2keyword: cmyk2keyword,
108
48
 
109
- function getHwb(string) {
110
- if (!string) {
111
- return;
112
- }
113
- var hwb = /^hwb\(\s*([+-]?\d+)(?:deg)?\s*,\s*([+-]?[\d\.]+)%\s*,\s*([+-]?[\d\.]+)%\s*(?:,\s*([+-]?[\d\.]+)\s*)?\)/;
114
- var match = string.match(hwb);
115
- if (match) {
116
- var alpha = parseFloat(match[4]);
117
- var h = scale(parseInt(match[1]), 0, 360),
118
- w = scale(parseFloat(match[2]), 0, 100),
119
- b = scale(parseFloat(match[3]), 0, 100),
120
- a = scale(isNaN(alpha) ? 1 : alpha, 0, 1);
121
- return [h, w, b, a];
122
- }
123
- }
49
+ keyword2rgb: keyword2rgb,
50
+ keyword2hsl: keyword2hsl,
51
+ keyword2hsv: keyword2hsv,
52
+ keyword2hwb: keyword2hwb,
53
+ keyword2cmyk: keyword2cmyk,
54
+ keyword2lab: keyword2lab,
55
+ keyword2xyz: keyword2xyz,
124
56
 
125
- function getRgb(string) {
126
- var rgba = getRgba(string);
127
- return rgba && rgba.slice(0, 3);
128
- }
57
+ xyz2rgb: xyz2rgb,
58
+ xyz2lab: xyz2lab,
59
+ xyz2lch: xyz2lch,
129
60
 
130
- function getHsl(string) {
131
- var hsla = getHsla(string);
132
- return hsla && hsla.slice(0, 3);
133
- }
61
+ lab2xyz: lab2xyz,
62
+ lab2rgb: lab2rgb,
63
+ lab2lch: lab2lch,
134
64
 
135
- function getAlpha(string) {
136
- var vals = getRgba(string);
137
- if (vals) {
138
- return vals[3];
139
- }
140
- else if (vals = getHsla(string)) {
141
- return vals[3];
142
- }
143
- else if (vals = getHwb(string)) {
144
- return vals[3];
145
- }
146
- }
65
+ lch2lab: lch2lab,
66
+ lch2xyz: lch2xyz,
67
+ lch2rgb: lch2rgb
68
+ };
147
69
 
148
- // generators
149
- function hexString(rgb) {
150
- return "#" + hexDouble(rgb[0]) + hexDouble(rgb[1])
151
- + hexDouble(rgb[2]);
152
- }
153
70
 
154
- function rgbString(rgba, alpha) {
155
- if (alpha < 1 || (rgba[3] && rgba[3] < 1)) {
156
- return rgbaString(rgba, alpha);
157
- }
158
- return "rgb(" + rgba[0] + ", " + rgba[1] + ", " + rgba[2] + ")";
159
- }
71
+ function rgb2hsl(rgb) {
72
+ var r = rgb[0]/255,
73
+ g = rgb[1]/255,
74
+ b = rgb[2]/255,
75
+ min = Math.min(r, g, b),
76
+ max = Math.max(r, g, b),
77
+ delta = max - min,
78
+ h, s, l;
160
79
 
161
- function rgbaString(rgba, alpha) {
162
- if (alpha === undefined) {
163
- alpha = (rgba[3] !== undefined ? rgba[3] : 1);
164
- }
165
- return "rgba(" + rgba[0] + ", " + rgba[1] + ", " + rgba[2]
166
- + ", " + alpha + ")";
167
- }
80
+ if (max == min)
81
+ h = 0;
82
+ else if (r == max)
83
+ h = (g - b) / delta;
84
+ else if (g == max)
85
+ h = 2 + (b - r) / delta;
86
+ else if (b == max)
87
+ h = 4 + (r - g)/ delta;
168
88
 
169
- function percentString(rgba, alpha) {
170
- if (alpha < 1 || (rgba[3] && rgba[3] < 1)) {
171
- return percentaString(rgba, alpha);
172
- }
173
- var r = Math.round(rgba[0]/255 * 100),
174
- g = Math.round(rgba[1]/255 * 100),
175
- b = Math.round(rgba[2]/255 * 100);
89
+ h = Math.min(h * 60, 360);
176
90
 
177
- return "rgb(" + r + "%, " + g + "%, " + b + "%)";
178
- }
91
+ if (h < 0)
92
+ h += 360;
179
93
 
180
- function percentaString(rgba, alpha) {
181
- var r = Math.round(rgba[0]/255 * 100),
182
- g = Math.round(rgba[1]/255 * 100),
183
- b = Math.round(rgba[2]/255 * 100);
184
- return "rgba(" + r + "%, " + g + "%, " + b + "%, " + (alpha || rgba[3] || 1) + ")";
185
- }
94
+ l = (min + max) / 2;
186
95
 
187
- function hslString(hsla, alpha) {
188
- if (alpha < 1 || (hsla[3] && hsla[3] < 1)) {
189
- return hslaString(hsla, alpha);
190
- }
191
- return "hsl(" + hsla[0] + ", " + hsla[1] + "%, " + hsla[2] + "%)";
192
- }
96
+ if (max == min)
97
+ s = 0;
98
+ else if (l <= 0.5)
99
+ s = delta / (max + min);
100
+ else
101
+ s = delta / (2 - max - min);
193
102
 
194
- function hslaString(hsla, alpha) {
195
- if (alpha === undefined) {
196
- alpha = (hsla[3] !== undefined ? hsla[3] : 1);
197
- }
198
- return "hsla(" + hsla[0] + ", " + hsla[1] + "%, " + hsla[2] + "%, "
199
- + alpha + ")";
103
+ return [h, s * 100, l * 100];
200
104
  }
201
105
 
202
- // hwb is a bit different than rgb(a) & hsl(a) since there is no alpha specific syntax
203
- // (hwb have alpha optional & 1 is default value)
204
- function hwbString(hwb, alpha) {
205
- if (alpha === undefined) {
206
- alpha = (hwb[3] !== undefined ? hwb[3] : 1);
207
- }
208
- return "hwb(" + hwb[0] + ", " + hwb[1] + "%, " + hwb[2] + "%"
209
- + (alpha !== undefined && alpha !== 1 ? ", " + alpha : "") + ")";
210
- }
106
+ function rgb2hsv(rgb) {
107
+ var r = rgb[0],
108
+ g = rgb[1],
109
+ b = rgb[2],
110
+ min = Math.min(r, g, b),
111
+ max = Math.max(r, g, b),
112
+ delta = max - min,
113
+ h, s, v;
211
114
 
212
- function keyword(rgb) {
213
- return reverseNames[rgb.slice(0, 3)];
214
- }
115
+ if (max == 0)
116
+ s = 0;
117
+ else
118
+ s = (delta/max * 1000)/10;
215
119
 
216
- // helpers
217
- function scale(num, min, max) {
218
- return Math.min(Math.max(min, num), max);
120
+ if (max == min)
121
+ h = 0;
122
+ else if (r == max)
123
+ h = (g - b) / delta;
124
+ else if (g == max)
125
+ h = 2 + (b - r) / delta;
126
+ else if (b == max)
127
+ h = 4 + (r - g) / delta;
128
+
129
+ h = Math.min(h * 60, 360);
130
+
131
+ if (h < 0)
132
+ h += 360;
133
+
134
+ v = ((max / 255) * 1000) / 10;
135
+
136
+ return [h, s, v];
219
137
  }
220
138
 
221
- function hexDouble(num) {
222
- var str = num.toString(16).toUpperCase();
223
- return (str.length < 2) ? "0" + str : str;
139
+ function rgb2hwb(rgb) {
140
+ var r = rgb[0],
141
+ g = rgb[1],
142
+ b = rgb[2],
143
+ h = rgb2hsl(rgb)[0],
144
+ w = 1/255 * Math.min(r, Math.min(g, b)),
145
+ b = 1 - 1/255 * Math.max(r, Math.max(g, b));
146
+
147
+ return [h, w * 100, b * 100];
224
148
  }
225
149
 
150
+ function rgb2cmyk(rgb) {
151
+ var r = rgb[0] / 255,
152
+ g = rgb[1] / 255,
153
+ b = rgb[2] / 255,
154
+ c, m, y, k;
226
155
 
227
- //create a list of reverse color names
228
- var reverseNames = {};
229
- for (var name in colorNames) {
230
- reverseNames[colorNames[name]] = name;
156
+ k = Math.min(1 - r, 1 - g, 1 - b);
157
+ c = (1 - r - k) / (1 - k) || 0;
158
+ m = (1 - g - k) / (1 - k) || 0;
159
+ y = (1 - b - k) / (1 - k) || 0;
160
+ return [c * 100, m * 100, y * 100, k * 100];
231
161
  }
232
162
 
233
- },{"5":5}],2:[function(require,module,exports){
234
- /* MIT license */
235
- var convert = require(4);
236
- var string = require(1);
163
+ function rgb2keyword(rgb) {
164
+ return reverseKeywords[JSON.stringify(rgb)];
165
+ }
237
166
 
238
- var Color = function (obj) {
239
- if (obj instanceof Color) {
240
- return obj;
241
- }
242
- if (!(this instanceof Color)) {
243
- return new Color(obj);
244
- }
167
+ function rgb2xyz(rgb) {
168
+ var r = rgb[0] / 255,
169
+ g = rgb[1] / 255,
170
+ b = rgb[2] / 255;
245
171
 
246
- this.valid = false;
247
- this.values = {
248
- rgb: [0, 0, 0],
249
- hsl: [0, 0, 0],
250
- hsv: [0, 0, 0],
251
- hwb: [0, 0, 0],
252
- cmyk: [0, 0, 0, 0],
253
- alpha: 1
254
- };
172
+ // assume sRGB
173
+ r = r > 0.04045 ? Math.pow(((r + 0.055) / 1.055), 2.4) : (r / 12.92);
174
+ g = g > 0.04045 ? Math.pow(((g + 0.055) / 1.055), 2.4) : (g / 12.92);
175
+ b = b > 0.04045 ? Math.pow(((b + 0.055) / 1.055), 2.4) : (b / 12.92);
255
176
 
256
- // parse Color() argument
257
- var vals;
258
- if (typeof obj === 'string') {
259
- vals = string.getRgba(obj);
260
- if (vals) {
261
- this.setValues('rgb', vals);
262
- } else if (vals = string.getHsla(obj)) {
263
- this.setValues('hsl', vals);
264
- } else if (vals = string.getHwb(obj)) {
265
- this.setValues('hwb', vals);
266
- }
267
- } else if (typeof obj === 'object') {
268
- vals = obj;
269
- if (vals.r !== undefined || vals.red !== undefined) {
270
- this.setValues('rgb', vals);
271
- } else if (vals.l !== undefined || vals.lightness !== undefined) {
272
- this.setValues('hsl', vals);
273
- } else if (vals.v !== undefined || vals.value !== undefined) {
274
- this.setValues('hsv', vals);
275
- } else if (vals.w !== undefined || vals.whiteness !== undefined) {
276
- this.setValues('hwb', vals);
277
- } else if (vals.c !== undefined || vals.cyan !== undefined) {
278
- this.setValues('cmyk', vals);
279
- }
280
- }
281
- };
177
+ var x = (r * 0.4124) + (g * 0.3576) + (b * 0.1805);
178
+ var y = (r * 0.2126) + (g * 0.7152) + (b * 0.0722);
179
+ var z = (r * 0.0193) + (g * 0.1192) + (b * 0.9505);
282
180
 
283
- Color.prototype = {
284
- isValid: function () {
285
- return this.valid;
286
- },
287
- rgb: function () {
288
- return this.setSpace('rgb', arguments);
289
- },
290
- hsl: function () {
291
- return this.setSpace('hsl', arguments);
292
- },
293
- hsv: function () {
294
- return this.setSpace('hsv', arguments);
295
- },
296
- hwb: function () {
297
- return this.setSpace('hwb', arguments);
298
- },
299
- cmyk: function () {
300
- return this.setSpace('cmyk', arguments);
301
- },
181
+ return [x * 100, y *100, z * 100];
182
+ }
302
183
 
303
- rgbArray: function () {
304
- return this.values.rgb;
305
- },
306
- hslArray: function () {
307
- return this.values.hsl;
308
- },
309
- hsvArray: function () {
310
- return this.values.hsv;
311
- },
312
- hwbArray: function () {
313
- var values = this.values;
314
- if (values.alpha !== 1) {
315
- return values.hwb.concat([values.alpha]);
316
- }
317
- return values.hwb;
318
- },
319
- cmykArray: function () {
320
- return this.values.cmyk;
321
- },
322
- rgbaArray: function () {
323
- var values = this.values;
324
- return values.rgb.concat([values.alpha]);
325
- },
326
- hslaArray: function () {
327
- var values = this.values;
328
- return values.hsl.concat([values.alpha]);
329
- },
330
- alpha: function (val) {
331
- if (val === undefined) {
332
- return this.values.alpha;
333
- }
334
- this.setValues('alpha', val);
335
- return this;
336
- },
184
+ function rgb2lab(rgb) {
185
+ var xyz = rgb2xyz(rgb),
186
+ x = xyz[0],
187
+ y = xyz[1],
188
+ z = xyz[2],
189
+ l, a, b;
337
190
 
338
- red: function (val) {
339
- return this.setChannel('rgb', 0, val);
340
- },
341
- green: function (val) {
342
- return this.setChannel('rgb', 1, val);
343
- },
344
- blue: function (val) {
345
- return this.setChannel('rgb', 2, val);
346
- },
347
- hue: function (val) {
348
- if (val) {
349
- val %= 360;
350
- val = val < 0 ? 360 + val : val;
351
- }
352
- return this.setChannel('hsl', 0, val);
353
- },
354
- saturation: function (val) {
355
- return this.setChannel('hsl', 1, val);
356
- },
357
- lightness: function (val) {
358
- return this.setChannel('hsl', 2, val);
359
- },
360
- saturationv: function (val) {
361
- return this.setChannel('hsv', 1, val);
362
- },
363
- whiteness: function (val) {
364
- return this.setChannel('hwb', 1, val);
365
- },
366
- blackness: function (val) {
367
- return this.setChannel('hwb', 2, val);
368
- },
369
- value: function (val) {
370
- return this.setChannel('hsv', 2, val);
371
- },
372
- cyan: function (val) {
373
- return this.setChannel('cmyk', 0, val);
374
- },
375
- magenta: function (val) {
376
- return this.setChannel('cmyk', 1, val);
377
- },
378
- yellow: function (val) {
379
- return this.setChannel('cmyk', 2, val);
380
- },
381
- black: function (val) {
382
- return this.setChannel('cmyk', 3, val);
383
- },
191
+ x /= 95.047;
192
+ y /= 100;
193
+ z /= 108.883;
384
194
 
385
- hexString: function () {
386
- return string.hexString(this.values.rgb);
387
- },
388
- rgbString: function () {
389
- return string.rgbString(this.values.rgb, this.values.alpha);
390
- },
391
- rgbaString: function () {
392
- return string.rgbaString(this.values.rgb, this.values.alpha);
393
- },
394
- percentString: function () {
395
- return string.percentString(this.values.rgb, this.values.alpha);
396
- },
397
- hslString: function () {
398
- return string.hslString(this.values.hsl, this.values.alpha);
399
- },
400
- hslaString: function () {
401
- return string.hslaString(this.values.hsl, this.values.alpha);
402
- },
403
- hwbString: function () {
404
- return string.hwbString(this.values.hwb, this.values.alpha);
405
- },
406
- keyword: function () {
407
- return string.keyword(this.values.rgb, this.values.alpha);
408
- },
195
+ x = x > 0.008856 ? Math.pow(x, 1/3) : (7.787 * x) + (16 / 116);
196
+ y = y > 0.008856 ? Math.pow(y, 1/3) : (7.787 * y) + (16 / 116);
197
+ z = z > 0.008856 ? Math.pow(z, 1/3) : (7.787 * z) + (16 / 116);
409
198
 
410
- rgbNumber: function () {
411
- var rgb = this.values.rgb;
412
- return (rgb[0] << 16) | (rgb[1] << 8) | rgb[2];
413
- },
199
+ l = (116 * y) - 16;
200
+ a = 500 * (x - y);
201
+ b = 200 * (y - z);
414
202
 
415
- luminosity: function () {
416
- // http://www.w3.org/TR/WCAG20/#relativeluminancedef
417
- var rgb = this.values.rgb;
418
- var lum = [];
419
- for (var i = 0; i < rgb.length; i++) {
420
- var chan = rgb[i] / 255;
421
- lum[i] = (chan <= 0.03928) ? chan / 12.92 : Math.pow(((chan + 0.055) / 1.055), 2.4);
422
- }
423
- return 0.2126 * lum[0] + 0.7152 * lum[1] + 0.0722 * lum[2];
424
- },
203
+ return [l, a, b];
204
+ }
425
205
 
426
- contrast: function (color2) {
427
- // http://www.w3.org/TR/WCAG20/#contrast-ratiodef
428
- var lum1 = this.luminosity();
429
- var lum2 = color2.luminosity();
430
- if (lum1 > lum2) {
431
- return (lum1 + 0.05) / (lum2 + 0.05);
432
- }
433
- return (lum2 + 0.05) / (lum1 + 0.05);
434
- },
435
-
436
- level: function (color2) {
437
- var contrastRatio = this.contrast(color2);
438
- if (contrastRatio >= 7.1) {
439
- return 'AAA';
440
- }
206
+ function rgb2lch(args) {
207
+ return lab2lch(rgb2lab(args));
208
+ }
441
209
 
442
- return (contrastRatio >= 4.5) ? 'AA' : '';
443
- },
210
+ function hsl2rgb(hsl) {
211
+ var h = hsl[0] / 360,
212
+ s = hsl[1] / 100,
213
+ l = hsl[2] / 100,
214
+ t1, t2, t3, rgb, val;
444
215
 
445
- dark: function () {
446
- // YIQ equation from http://24ways.org/2010/calculating-color-contrast
447
- var rgb = this.values.rgb;
448
- var yiq = (rgb[0] * 299 + rgb[1] * 587 + rgb[2] * 114) / 1000;
449
- return yiq < 128;
450
- },
216
+ if (s == 0) {
217
+ val = l * 255;
218
+ return [val, val, val];
219
+ }
451
220
 
452
- light: function () {
453
- return !this.dark();
454
- },
221
+ if (l < 0.5)
222
+ t2 = l * (1 + s);
223
+ else
224
+ t2 = l + s - l * s;
225
+ t1 = 2 * l - t2;
455
226
 
456
- negate: function () {
457
- var rgb = [];
458
- for (var i = 0; i < 3; i++) {
459
- rgb[i] = 255 - this.values.rgb[i];
460
- }
461
- this.setValues('rgb', rgb);
462
- return this;
463
- },
227
+ rgb = [0, 0, 0];
228
+ for (var i = 0; i < 3; i++) {
229
+ t3 = h + 1 / 3 * - (i - 1);
230
+ t3 < 0 && t3++;
231
+ t3 > 1 && t3--;
464
232
 
465
- lighten: function (ratio) {
466
- var hsl = this.values.hsl;
467
- hsl[2] += hsl[2] * ratio;
468
- this.setValues('hsl', hsl);
469
- return this;
470
- },
233
+ if (6 * t3 < 1)
234
+ val = t1 + (t2 - t1) * 6 * t3;
235
+ else if (2 * t3 < 1)
236
+ val = t2;
237
+ else if (3 * t3 < 2)
238
+ val = t1 + (t2 - t1) * (2 / 3 - t3) * 6;
239
+ else
240
+ val = t1;
471
241
 
472
- darken: function (ratio) {
473
- var hsl = this.values.hsl;
474
- hsl[2] -= hsl[2] * ratio;
475
- this.setValues('hsl', hsl);
476
- return this;
477
- },
242
+ rgb[i] = val * 255;
243
+ }
478
244
 
479
- saturate: function (ratio) {
480
- var hsl = this.values.hsl;
481
- hsl[1] += hsl[1] * ratio;
482
- this.setValues('hsl', hsl);
483
- return this;
484
- },
245
+ return rgb;
246
+ }
485
247
 
486
- desaturate: function (ratio) {
487
- var hsl = this.values.hsl;
488
- hsl[1] -= hsl[1] * ratio;
489
- this.setValues('hsl', hsl);
490
- return this;
491
- },
248
+ function hsl2hsv(hsl) {
249
+ var h = hsl[0],
250
+ s = hsl[1] / 100,
251
+ l = hsl[2] / 100,
252
+ sv, v;
492
253
 
493
- whiten: function (ratio) {
494
- var hwb = this.values.hwb;
495
- hwb[1] += hwb[1] * ratio;
496
- this.setValues('hwb', hwb);
497
- return this;
498
- },
254
+ if(l === 0) {
255
+ // no need to do calc on black
256
+ // also avoids divide by 0 error
257
+ return [0, 0, 0];
258
+ }
499
259
 
500
- blacken: function (ratio) {
501
- var hwb = this.values.hwb;
502
- hwb[2] += hwb[2] * ratio;
503
- this.setValues('hwb', hwb);
504
- return this;
505
- },
260
+ l *= 2;
261
+ s *= (l <= 1) ? l : 2 - l;
262
+ v = (l + s) / 2;
263
+ sv = (2 * s) / (l + s);
264
+ return [h, sv * 100, v * 100];
265
+ }
506
266
 
507
- greyscale: function () {
508
- var rgb = this.values.rgb;
509
- // http://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale
510
- var val = rgb[0] * 0.3 + rgb[1] * 0.59 + rgb[2] * 0.11;
511
- this.setValues('rgb', [val, val, val]);
512
- return this;
513
- },
267
+ function hsl2hwb(args) {
268
+ return rgb2hwb(hsl2rgb(args));
269
+ }
514
270
 
515
- clearer: function (ratio) {
516
- var alpha = this.values.alpha;
517
- this.setValues('alpha', alpha - (alpha * ratio));
518
- return this;
519
- },
271
+ function hsl2cmyk(args) {
272
+ return rgb2cmyk(hsl2rgb(args));
273
+ }
520
274
 
521
- opaquer: function (ratio) {
522
- var alpha = this.values.alpha;
523
- this.setValues('alpha', alpha + (alpha * ratio));
524
- return this;
525
- },
275
+ function hsl2keyword(args) {
276
+ return rgb2keyword(hsl2rgb(args));
277
+ }
526
278
 
527
- rotate: function (degrees) {
528
- var hsl = this.values.hsl;
529
- var hue = (hsl[0] + degrees) % 360;
530
- hsl[0] = hue < 0 ? 360 + hue : hue;
531
- this.setValues('hsl', hsl);
532
- return this;
533
- },
534
279
 
535
- /**
536
- * Ported from sass implementation in C
537
- * https://github.com/sass/libsass/blob/0e6b4a2850092356aa3ece07c6b249f0221caced/functions.cpp#L209
538
- */
539
- mix: function (mixinColor, weight) {
540
- var color1 = this;
541
- var color2 = mixinColor;
542
- var p = weight === undefined ? 0.5 : weight;
280
+ function hsv2rgb(hsv) {
281
+ var h = hsv[0] / 60,
282
+ s = hsv[1] / 100,
283
+ v = hsv[2] / 100,
284
+ hi = Math.floor(h) % 6;
543
285
 
544
- var w = 2 * p - 1;
545
- var a = color1.alpha() - color2.alpha();
286
+ var f = h - Math.floor(h),
287
+ p = 255 * v * (1 - s),
288
+ q = 255 * v * (1 - (s * f)),
289
+ t = 255 * v * (1 - (s * (1 - f))),
290
+ v = 255 * v;
546
291
 
547
- var w1 = (((w * a === -1) ? w : (w + a) / (1 + w * a)) + 1) / 2.0;
548
- var w2 = 1 - w1;
292
+ switch(hi) {
293
+ case 0:
294
+ return [v, t, p];
295
+ case 1:
296
+ return [q, v, p];
297
+ case 2:
298
+ return [p, v, t];
299
+ case 3:
300
+ return [p, q, v];
301
+ case 4:
302
+ return [t, p, v];
303
+ case 5:
304
+ return [v, p, q];
305
+ }
306
+ }
549
307
 
550
- return this
551
- .rgb(
552
- w1 * color1.red() + w2 * color2.red(),
553
- w1 * color1.green() + w2 * color2.green(),
554
- w1 * color1.blue() + w2 * color2.blue()
555
- )
556
- .alpha(color1.alpha() * p + color2.alpha() * (1 - p));
557
- },
308
+ function hsv2hsl(hsv) {
309
+ var h = hsv[0],
310
+ s = hsv[1] / 100,
311
+ v = hsv[2] / 100,
312
+ sl, l;
558
313
 
559
- toJSON: function () {
560
- return this.rgb();
561
- },
314
+ l = (2 - s) * v;
315
+ sl = s * v;
316
+ sl /= (l <= 1) ? l : 2 - l;
317
+ sl = sl || 0;
318
+ l /= 2;
319
+ return [h, sl * 100, l * 100];
320
+ }
562
321
 
563
- clone: function () {
564
- // NOTE(SB): using node-clone creates a dependency to Buffer when using browserify,
565
- // making the final build way to big to embed in Chart.js. So let's do it manually,
566
- // assuming that values to clone are 1 dimension arrays containing only numbers,
567
- // except 'alpha' which is a number.
568
- var result = new Color();
569
- var source = this.values;
570
- var target = result.values;
571
- var value, type;
322
+ function hsv2hwb(args) {
323
+ return rgb2hwb(hsv2rgb(args))
324
+ }
572
325
 
573
- for (var prop in source) {
574
- if (source.hasOwnProperty(prop)) {
575
- value = source[prop];
576
- type = ({}).toString.call(value);
577
- if (type === '[object Array]') {
578
- target[prop] = value.slice(0);
579
- } else if (type === '[object Number]') {
580
- target[prop] = value;
581
- } else {
582
- console.error('unexpected color value:', value);
583
- }
584
- }
585
- }
326
+ function hsv2cmyk(args) {
327
+ return rgb2cmyk(hsv2rgb(args));
328
+ }
586
329
 
587
- return result;
588
- }
589
- };
330
+ function hsv2keyword(args) {
331
+ return rgb2keyword(hsv2rgb(args));
332
+ }
590
333
 
591
- Color.prototype.spaces = {
592
- rgb: ['red', 'green', 'blue'],
593
- hsl: ['hue', 'saturation', 'lightness'],
594
- hsv: ['hue', 'saturation', 'value'],
595
- hwb: ['hue', 'whiteness', 'blackness'],
596
- cmyk: ['cyan', 'magenta', 'yellow', 'black']
597
- };
334
+ // http://dev.w3.org/csswg/css-color/#hwb-to-rgb
335
+ function hwb2rgb(hwb) {
336
+ var h = hwb[0] / 360,
337
+ wh = hwb[1] / 100,
338
+ bl = hwb[2] / 100,
339
+ ratio = wh + bl,
340
+ i, v, f, n;
598
341
 
599
- Color.prototype.maxes = {
600
- rgb: [255, 255, 255],
601
- hsl: [360, 100, 100],
602
- hsv: [360, 100, 100],
603
- hwb: [360, 100, 100],
604
- cmyk: [100, 100, 100, 100]
605
- };
606
-
607
- Color.prototype.getValues = function (space) {
608
- var values = this.values;
609
- var vals = {};
610
-
611
- for (var i = 0; i < space.length; i++) {
612
- vals[space.charAt(i)] = values[space][i];
613
- }
614
-
615
- if (values.alpha !== 1) {
616
- vals.a = values.alpha;
617
- }
342
+ // wh + bl cant be > 1
343
+ if (ratio > 1) {
344
+ wh /= ratio;
345
+ bl /= ratio;
346
+ }
618
347
 
619
- // {r: 255, g: 255, b: 255, a: 0.4}
620
- return vals;
621
- };
348
+ i = Math.floor(6 * h);
349
+ v = 1 - bl;
350
+ f = 6 * h - i;
351
+ if ((i & 0x01) != 0) {
352
+ f = 1 - f;
353
+ }
354
+ n = wh + f * (v - wh); // linear interpolation
622
355
 
623
- Color.prototype.setValues = function (space, vals) {
624
- var values = this.values;
625
- var spaces = this.spaces;
626
- var maxes = this.maxes;
627
- var alpha = 1;
628
- var i;
356
+ switch (i) {
357
+ default:
358
+ case 6:
359
+ case 0: r = v; g = n; b = wh; break;
360
+ case 1: r = n; g = v; b = wh; break;
361
+ case 2: r = wh; g = v; b = n; break;
362
+ case 3: r = wh; g = n; b = v; break;
363
+ case 4: r = n; g = wh; b = v; break;
364
+ case 5: r = v; g = wh; b = n; break;
365
+ }
629
366
 
630
- this.valid = true;
367
+ return [r * 255, g * 255, b * 255];
368
+ }
631
369
 
632
- if (space === 'alpha') {
633
- alpha = vals;
634
- } else if (vals.length) {
635
- // [10, 10, 10]
636
- values[space] = vals.slice(0, space.length);
637
- alpha = vals[space.length];
638
- } else if (vals[space.charAt(0)] !== undefined) {
639
- // {r: 10, g: 10, b: 10}
640
- for (i = 0; i < space.length; i++) {
641
- values[space][i] = vals[space.charAt(i)];
642
- }
370
+ function hwb2hsl(args) {
371
+ return rgb2hsl(hwb2rgb(args));
372
+ }
643
373
 
644
- alpha = vals.a;
645
- } else if (vals[spaces[space][0]] !== undefined) {
646
- // {red: 10, green: 10, blue: 10}
647
- var chans = spaces[space];
374
+ function hwb2hsv(args) {
375
+ return rgb2hsv(hwb2rgb(args));
376
+ }
648
377
 
649
- for (i = 0; i < space.length; i++) {
650
- values[space][i] = vals[chans[i]];
651
- }
378
+ function hwb2cmyk(args) {
379
+ return rgb2cmyk(hwb2rgb(args));
380
+ }
652
381
 
653
- alpha = vals.alpha;
654
- }
382
+ function hwb2keyword(args) {
383
+ return rgb2keyword(hwb2rgb(args));
384
+ }
655
385
 
656
- values.alpha = Math.max(0, Math.min(1, (alpha === undefined ? values.alpha : alpha)));
386
+ function cmyk2rgb(cmyk) {
387
+ var c = cmyk[0] / 100,
388
+ m = cmyk[1] / 100,
389
+ y = cmyk[2] / 100,
390
+ k = cmyk[3] / 100,
391
+ r, g, b;
657
392
 
658
- if (space === 'alpha') {
659
- return false;
660
- }
393
+ r = 1 - Math.min(1, c * (1 - k) + k);
394
+ g = 1 - Math.min(1, m * (1 - k) + k);
395
+ b = 1 - Math.min(1, y * (1 - k) + k);
396
+ return [r * 255, g * 255, b * 255];
397
+ }
661
398
 
662
- var capped;
399
+ function cmyk2hsl(args) {
400
+ return rgb2hsl(cmyk2rgb(args));
401
+ }
663
402
 
664
- // cap values of the space prior converting all values
665
- for (i = 0; i < space.length; i++) {
666
- capped = Math.max(0, Math.min(maxes[space][i], values[space][i]));
667
- values[space][i] = Math.round(capped);
668
- }
403
+ function cmyk2hsv(args) {
404
+ return rgb2hsv(cmyk2rgb(args));
405
+ }
669
406
 
670
- // convert to all the other color spaces
671
- for (var sname in spaces) {
672
- if (sname !== space) {
673
- values[sname] = convert[space][sname](values[space]);
674
- }
675
- }
407
+ function cmyk2hwb(args) {
408
+ return rgb2hwb(cmyk2rgb(args));
409
+ }
676
410
 
677
- return true;
678
- };
411
+ function cmyk2keyword(args) {
412
+ return rgb2keyword(cmyk2rgb(args));
413
+ }
679
414
 
680
- Color.prototype.setSpace = function (space, args) {
681
- var vals = args[0];
682
415
 
683
- if (vals === undefined) {
684
- // color.rgb()
685
- return this.getValues(space);
686
- }
416
+ function xyz2rgb(xyz) {
417
+ var x = xyz[0] / 100,
418
+ y = xyz[1] / 100,
419
+ z = xyz[2] / 100,
420
+ r, g, b;
687
421
 
688
- // color.rgb(10, 10, 10)
689
- if (typeof vals === 'number') {
690
- vals = Array.prototype.slice.call(args);
691
- }
422
+ r = (x * 3.2406) + (y * -1.5372) + (z * -0.4986);
423
+ g = (x * -0.9689) + (y * 1.8758) + (z * 0.0415);
424
+ b = (x * 0.0557) + (y * -0.2040) + (z * 1.0570);
692
425
 
693
- this.setValues(space, vals);
694
- return this;
695
- };
426
+ // assume sRGB
427
+ r = r > 0.0031308 ? ((1.055 * Math.pow(r, 1.0 / 2.4)) - 0.055)
428
+ : r = (r * 12.92);
696
429
 
697
- Color.prototype.setChannel = function (space, index, val) {
698
- var svalues = this.values[space];
699
- if (val === undefined) {
700
- // color.red()
701
- return svalues[index];
702
- } else if (val === svalues[index]) {
703
- // color.red(color.red())
704
- return this;
705
- }
430
+ g = g > 0.0031308 ? ((1.055 * Math.pow(g, 1.0 / 2.4)) - 0.055)
431
+ : g = (g * 12.92);
706
432
 
707
- // color.red(100)
708
- svalues[index] = val;
709
- this.setValues(space, svalues);
433
+ b = b > 0.0031308 ? ((1.055 * Math.pow(b, 1.0 / 2.4)) - 0.055)
434
+ : b = (b * 12.92);
710
435
 
711
- return this;
712
- };
436
+ r = Math.min(Math.max(0, r), 1);
437
+ g = Math.min(Math.max(0, g), 1);
438
+ b = Math.min(Math.max(0, b), 1);
713
439
 
714
- if (typeof window !== 'undefined') {
715
- window.Color = Color;
440
+ return [r * 255, g * 255, b * 255];
716
441
  }
717
442
 
718
- module.exports = Color;
443
+ function xyz2lab(xyz) {
444
+ var x = xyz[0],
445
+ y = xyz[1],
446
+ z = xyz[2],
447
+ l, a, b;
719
448
 
720
- },{"1":1,"4":4}],3:[function(require,module,exports){
721
- /* MIT license */
449
+ x /= 95.047;
450
+ y /= 100;
451
+ z /= 108.883;
722
452
 
723
- module.exports = {
724
- rgb2hsl: rgb2hsl,
725
- rgb2hsv: rgb2hsv,
726
- rgb2hwb: rgb2hwb,
727
- rgb2cmyk: rgb2cmyk,
728
- rgb2keyword: rgb2keyword,
729
- rgb2xyz: rgb2xyz,
730
- rgb2lab: rgb2lab,
731
- rgb2lch: rgb2lch,
453
+ x = x > 0.008856 ? Math.pow(x, 1/3) : (7.787 * x) + (16 / 116);
454
+ y = y > 0.008856 ? Math.pow(y, 1/3) : (7.787 * y) + (16 / 116);
455
+ z = z > 0.008856 ? Math.pow(z, 1/3) : (7.787 * z) + (16 / 116);
732
456
 
733
- hsl2rgb: hsl2rgb,
734
- hsl2hsv: hsl2hsv,
735
- hsl2hwb: hsl2hwb,
736
- hsl2cmyk: hsl2cmyk,
737
- hsl2keyword: hsl2keyword,
457
+ l = (116 * y) - 16;
458
+ a = 500 * (x - y);
459
+ b = 200 * (y - z);
738
460
 
739
- hsv2rgb: hsv2rgb,
740
- hsv2hsl: hsv2hsl,
741
- hsv2hwb: hsv2hwb,
742
- hsv2cmyk: hsv2cmyk,
743
- hsv2keyword: hsv2keyword,
461
+ return [l, a, b];
462
+ }
744
463
 
745
- hwb2rgb: hwb2rgb,
746
- hwb2hsl: hwb2hsl,
747
- hwb2hsv: hwb2hsv,
748
- hwb2cmyk: hwb2cmyk,
749
- hwb2keyword: hwb2keyword,
464
+ function xyz2lch(args) {
465
+ return lab2lch(xyz2lab(args));
466
+ }
750
467
 
751
- cmyk2rgb: cmyk2rgb,
752
- cmyk2hsl: cmyk2hsl,
753
- cmyk2hsv: cmyk2hsv,
754
- cmyk2hwb: cmyk2hwb,
755
- cmyk2keyword: cmyk2keyword,
468
+ function lab2xyz(lab) {
469
+ var l = lab[0],
470
+ a = lab[1],
471
+ b = lab[2],
472
+ x, y, z, y2;
756
473
 
757
- keyword2rgb: keyword2rgb,
758
- keyword2hsl: keyword2hsl,
759
- keyword2hsv: keyword2hsv,
760
- keyword2hwb: keyword2hwb,
761
- keyword2cmyk: keyword2cmyk,
762
- keyword2lab: keyword2lab,
763
- keyword2xyz: keyword2xyz,
474
+ if (l <= 8) {
475
+ y = (l * 100) / 903.3;
476
+ y2 = (7.787 * (y / 100)) + (16 / 116);
477
+ } else {
478
+ y = 100 * Math.pow((l + 16) / 116, 3);
479
+ y2 = Math.pow(y / 100, 1/3);
480
+ }
764
481
 
765
- xyz2rgb: xyz2rgb,
766
- xyz2lab: xyz2lab,
767
- xyz2lch: xyz2lch,
482
+ x = x / 95.047 <= 0.008856 ? x = (95.047 * ((a / 500) + y2 - (16 / 116))) / 7.787 : 95.047 * Math.pow((a / 500) + y2, 3);
768
483
 
769
- lab2xyz: lab2xyz,
770
- lab2rgb: lab2rgb,
771
- lab2lch: lab2lch,
484
+ z = z / 108.883 <= 0.008859 ? z = (108.883 * (y2 - (b / 200) - (16 / 116))) / 7.787 : 108.883 * Math.pow(y2 - (b / 200), 3);
772
485
 
773
- lch2lab: lch2lab,
774
- lch2xyz: lch2xyz,
775
- lch2rgb: lch2rgb
486
+ return [x, y, z];
776
487
  }
777
488
 
489
+ function lab2lch(lab) {
490
+ var l = lab[0],
491
+ a = lab[1],
492
+ b = lab[2],
493
+ hr, h, c;
778
494
 
779
- function rgb2hsl(rgb) {
780
- var r = rgb[0]/255,
781
- g = rgb[1]/255,
782
- b = rgb[2]/255,
783
- min = Math.min(r, g, b),
784
- max = Math.max(r, g, b),
785
- delta = max - min,
786
- h, s, l;
787
-
788
- if (max == min)
789
- h = 0;
790
- else if (r == max)
791
- h = (g - b) / delta;
792
- else if (g == max)
793
- h = 2 + (b - r) / delta;
794
- else if (b == max)
795
- h = 4 + (r - g)/ delta;
796
-
797
- h = Math.min(h * 60, 360);
798
-
799
- if (h < 0)
800
- h += 360;
801
-
802
- l = (min + max) / 2;
803
-
804
- if (max == min)
805
- s = 0;
806
- else if (l <= 0.5)
807
- s = delta / (max + min);
808
- else
809
- s = delta / (2 - max - min);
810
-
811
- return [h, s * 100, l * 100];
812
- }
813
-
814
- function rgb2hsv(rgb) {
815
- var r = rgb[0],
816
- g = rgb[1],
817
- b = rgb[2],
818
- min = Math.min(r, g, b),
819
- max = Math.max(r, g, b),
820
- delta = max - min,
821
- h, s, v;
822
-
823
- if (max == 0)
824
- s = 0;
825
- else
826
- s = (delta/max * 1000)/10;
827
-
828
- if (max == min)
829
- h = 0;
830
- else if (r == max)
831
- h = (g - b) / delta;
832
- else if (g == max)
833
- h = 2 + (b - r) / delta;
834
- else if (b == max)
835
- h = 4 + (r - g) / delta;
836
-
837
- h = Math.min(h * 60, 360);
838
-
839
- if (h < 0)
495
+ hr = Math.atan2(b, a);
496
+ h = hr * 360 / 2 / Math.PI;
497
+ if (h < 0) {
840
498
  h += 360;
841
-
842
- v = ((max / 255) * 1000) / 10;
843
-
844
- return [h, s, v];
499
+ }
500
+ c = Math.sqrt(a * a + b * b);
501
+ return [l, c, h];
845
502
  }
846
503
 
847
- function rgb2hwb(rgb) {
848
- var r = rgb[0],
849
- g = rgb[1],
850
- b = rgb[2],
851
- h = rgb2hsl(rgb)[0],
852
- w = 1/255 * Math.min(r, Math.min(g, b)),
853
- b = 1 - 1/255 * Math.max(r, Math.max(g, b));
854
-
855
- return [h, w * 100, b * 100];
504
+ function lab2rgb(args) {
505
+ return xyz2rgb(lab2xyz(args));
856
506
  }
857
507
 
858
- function rgb2cmyk(rgb) {
859
- var r = rgb[0] / 255,
860
- g = rgb[1] / 255,
861
- b = rgb[2] / 255,
862
- c, m, y, k;
863
-
864
- k = Math.min(1 - r, 1 - g, 1 - b);
865
- c = (1 - r - k) / (1 - k) || 0;
866
- m = (1 - g - k) / (1 - k) || 0;
867
- y = (1 - b - k) / (1 - k) || 0;
868
- return [c * 100, m * 100, y * 100, k * 100];
869
- }
508
+ function lch2lab(lch) {
509
+ var l = lch[0],
510
+ c = lch[1],
511
+ h = lch[2],
512
+ a, b, hr;
870
513
 
871
- function rgb2keyword(rgb) {
872
- return reverseKeywords[JSON.stringify(rgb)];
514
+ hr = h / 360 * 2 * Math.PI;
515
+ a = c * Math.cos(hr);
516
+ b = c * Math.sin(hr);
517
+ return [l, a, b];
873
518
  }
874
519
 
875
- function rgb2xyz(rgb) {
876
- var r = rgb[0] / 255,
877
- g = rgb[1] / 255,
878
- b = rgb[2] / 255;
879
-
880
- // assume sRGB
881
- r = r > 0.04045 ? Math.pow(((r + 0.055) / 1.055), 2.4) : (r / 12.92);
882
- g = g > 0.04045 ? Math.pow(((g + 0.055) / 1.055), 2.4) : (g / 12.92);
883
- b = b > 0.04045 ? Math.pow(((b + 0.055) / 1.055), 2.4) : (b / 12.92);
884
-
885
- var x = (r * 0.4124) + (g * 0.3576) + (b * 0.1805);
886
- var y = (r * 0.2126) + (g * 0.7152) + (b * 0.0722);
887
- var z = (r * 0.0193) + (g * 0.1192) + (b * 0.9505);
888
-
889
- return [x * 100, y *100, z * 100];
520
+ function lch2xyz(args) {
521
+ return lab2xyz(lch2lab(args));
890
522
  }
891
523
 
892
- function rgb2lab(rgb) {
893
- var xyz = rgb2xyz(rgb),
894
- x = xyz[0],
895
- y = xyz[1],
896
- z = xyz[2],
897
- l, a, b;
898
-
899
- x /= 95.047;
900
- y /= 100;
901
- z /= 108.883;
902
-
903
- x = x > 0.008856 ? Math.pow(x, 1/3) : (7.787 * x) + (16 / 116);
904
- y = y > 0.008856 ? Math.pow(y, 1/3) : (7.787 * y) + (16 / 116);
905
- z = z > 0.008856 ? Math.pow(z, 1/3) : (7.787 * z) + (16 / 116);
906
-
907
- l = (116 * y) - 16;
908
- a = 500 * (x - y);
909
- b = 200 * (y - z);
910
-
911
- return [l, a, b];
524
+ function lch2rgb(args) {
525
+ return lab2rgb(lch2lab(args));
912
526
  }
913
527
 
914
- function rgb2lch(args) {
915
- return lab2lch(rgb2lab(args));
528
+ function keyword2rgb(keyword) {
529
+ return cssKeywords[keyword];
916
530
  }
917
531
 
918
- function hsl2rgb(hsl) {
919
- var h = hsl[0] / 360,
920
- s = hsl[1] / 100,
921
- l = hsl[2] / 100,
922
- t1, t2, t3, rgb, val;
923
-
924
- if (s == 0) {
925
- val = l * 255;
926
- return [val, val, val];
927
- }
928
-
929
- if (l < 0.5)
930
- t2 = l * (1 + s);
931
- else
932
- t2 = l + s - l * s;
933
- t1 = 2 * l - t2;
934
-
935
- rgb = [0, 0, 0];
936
- for (var i = 0; i < 3; i++) {
937
- t3 = h + 1 / 3 * - (i - 1);
938
- t3 < 0 && t3++;
939
- t3 > 1 && t3--;
940
-
941
- if (6 * t3 < 1)
942
- val = t1 + (t2 - t1) * 6 * t3;
943
- else if (2 * t3 < 1)
944
- val = t2;
945
- else if (3 * t3 < 2)
946
- val = t1 + (t2 - t1) * (2 / 3 - t3) * 6;
947
- else
948
- val = t1;
949
-
950
- rgb[i] = val * 255;
951
- }
952
-
953
- return rgb;
532
+ function keyword2hsl(args) {
533
+ return rgb2hsl(keyword2rgb(args));
954
534
  }
955
535
 
956
- function hsl2hsv(hsl) {
957
- var h = hsl[0],
958
- s = hsl[1] / 100,
959
- l = hsl[2] / 100,
960
- sv, v;
961
-
962
- if(l === 0) {
963
- // no need to do calc on black
964
- // also avoids divide by 0 error
965
- return [0, 0, 0];
966
- }
967
-
968
- l *= 2;
969
- s *= (l <= 1) ? l : 2 - l;
970
- v = (l + s) / 2;
971
- sv = (2 * s) / (l + s);
972
- return [h, sv * 100, v * 100];
536
+ function keyword2hsv(args) {
537
+ return rgb2hsv(keyword2rgb(args));
973
538
  }
974
539
 
975
- function hsl2hwb(args) {
976
- return rgb2hwb(hsl2rgb(args));
540
+ function keyword2hwb(args) {
541
+ return rgb2hwb(keyword2rgb(args));
977
542
  }
978
543
 
979
- function hsl2cmyk(args) {
980
- return rgb2cmyk(hsl2rgb(args));
544
+ function keyword2cmyk(args) {
545
+ return rgb2cmyk(keyword2rgb(args));
981
546
  }
982
547
 
983
- function hsl2keyword(args) {
984
- return rgb2keyword(hsl2rgb(args));
548
+ function keyword2lab(args) {
549
+ return rgb2lab(keyword2rgb(args));
985
550
  }
986
551
 
987
-
988
- function hsv2rgb(hsv) {
989
- var h = hsv[0] / 60,
990
- s = hsv[1] / 100,
991
- v = hsv[2] / 100,
992
- hi = Math.floor(h) % 6;
993
-
994
- var f = h - Math.floor(h),
995
- p = 255 * v * (1 - s),
996
- q = 255 * v * (1 - (s * f)),
997
- t = 255 * v * (1 - (s * (1 - f))),
998
- v = 255 * v;
999
-
1000
- switch(hi) {
1001
- case 0:
1002
- return [v, t, p];
1003
- case 1:
1004
- return [q, v, p];
1005
- case 2:
1006
- return [p, v, t];
1007
- case 3:
1008
- return [p, q, v];
1009
- case 4:
1010
- return [t, p, v];
1011
- case 5:
1012
- return [v, p, q];
1013
- }
1014
- }
1015
-
1016
- function hsv2hsl(hsv) {
1017
- var h = hsv[0],
1018
- s = hsv[1] / 100,
1019
- v = hsv[2] / 100,
1020
- sl, l;
1021
-
1022
- l = (2 - s) * v;
1023
- sl = s * v;
1024
- sl /= (l <= 1) ? l : 2 - l;
1025
- sl = sl || 0;
1026
- l /= 2;
1027
- return [h, sl * 100, l * 100];
1028
- }
1029
-
1030
- function hsv2hwb(args) {
1031
- return rgb2hwb(hsv2rgb(args))
1032
- }
1033
-
1034
- function hsv2cmyk(args) {
1035
- return rgb2cmyk(hsv2rgb(args));
1036
- }
1037
-
1038
- function hsv2keyword(args) {
1039
- return rgb2keyword(hsv2rgb(args));
1040
- }
1041
-
1042
- // http://dev.w3.org/csswg/css-color/#hwb-to-rgb
1043
- function hwb2rgb(hwb) {
1044
- var h = hwb[0] / 360,
1045
- wh = hwb[1] / 100,
1046
- bl = hwb[2] / 100,
1047
- ratio = wh + bl,
1048
- i, v, f, n;
1049
-
1050
- // wh + bl cant be > 1
1051
- if (ratio > 1) {
1052
- wh /= ratio;
1053
- bl /= ratio;
1054
- }
1055
-
1056
- i = Math.floor(6 * h);
1057
- v = 1 - bl;
1058
- f = 6 * h - i;
1059
- if ((i & 0x01) != 0) {
1060
- f = 1 - f;
1061
- }
1062
- n = wh + f * (v - wh); // linear interpolation
1063
-
1064
- switch (i) {
1065
- default:
1066
- case 6:
1067
- case 0: r = v; g = n; b = wh; break;
1068
- case 1: r = n; g = v; b = wh; break;
1069
- case 2: r = wh; g = v; b = n; break;
1070
- case 3: r = wh; g = n; b = v; break;
1071
- case 4: r = n; g = wh; b = v; break;
1072
- case 5: r = v; g = wh; b = n; break;
1073
- }
1074
-
1075
- return [r * 255, g * 255, b * 255];
1076
- }
1077
-
1078
- function hwb2hsl(args) {
1079
- return rgb2hsl(hwb2rgb(args));
1080
- }
1081
-
1082
- function hwb2hsv(args) {
1083
- return rgb2hsv(hwb2rgb(args));
1084
- }
1085
-
1086
- function hwb2cmyk(args) {
1087
- return rgb2cmyk(hwb2rgb(args));
1088
- }
1089
-
1090
- function hwb2keyword(args) {
1091
- return rgb2keyword(hwb2rgb(args));
1092
- }
1093
-
1094
- function cmyk2rgb(cmyk) {
1095
- var c = cmyk[0] / 100,
1096
- m = cmyk[1] / 100,
1097
- y = cmyk[2] / 100,
1098
- k = cmyk[3] / 100,
1099
- r, g, b;
1100
-
1101
- r = 1 - Math.min(1, c * (1 - k) + k);
1102
- g = 1 - Math.min(1, m * (1 - k) + k);
1103
- b = 1 - Math.min(1, y * (1 - k) + k);
1104
- return [r * 255, g * 255, b * 255];
1105
- }
1106
-
1107
- function cmyk2hsl(args) {
1108
- return rgb2hsl(cmyk2rgb(args));
1109
- }
1110
-
1111
- function cmyk2hsv(args) {
1112
- return rgb2hsv(cmyk2rgb(args));
1113
- }
1114
-
1115
- function cmyk2hwb(args) {
1116
- return rgb2hwb(cmyk2rgb(args));
1117
- }
1118
-
1119
- function cmyk2keyword(args) {
1120
- return rgb2keyword(cmyk2rgb(args));
1121
- }
1122
-
1123
-
1124
- function xyz2rgb(xyz) {
1125
- var x = xyz[0] / 100,
1126
- y = xyz[1] / 100,
1127
- z = xyz[2] / 100,
1128
- r, g, b;
1129
-
1130
- r = (x * 3.2406) + (y * -1.5372) + (z * -0.4986);
1131
- g = (x * -0.9689) + (y * 1.8758) + (z * 0.0415);
1132
- b = (x * 0.0557) + (y * -0.2040) + (z * 1.0570);
1133
-
1134
- // assume sRGB
1135
- r = r > 0.0031308 ? ((1.055 * Math.pow(r, 1.0 / 2.4)) - 0.055)
1136
- : r = (r * 12.92);
1137
-
1138
- g = g > 0.0031308 ? ((1.055 * Math.pow(g, 1.0 / 2.4)) - 0.055)
1139
- : g = (g * 12.92);
1140
-
1141
- b = b > 0.0031308 ? ((1.055 * Math.pow(b, 1.0 / 2.4)) - 0.055)
1142
- : b = (b * 12.92);
1143
-
1144
- r = Math.min(Math.max(0, r), 1);
1145
- g = Math.min(Math.max(0, g), 1);
1146
- b = Math.min(Math.max(0, b), 1);
1147
-
1148
- return [r * 255, g * 255, b * 255];
1149
- }
1150
-
1151
- function xyz2lab(xyz) {
1152
- var x = xyz[0],
1153
- y = xyz[1],
1154
- z = xyz[2],
1155
- l, a, b;
1156
-
1157
- x /= 95.047;
1158
- y /= 100;
1159
- z /= 108.883;
1160
-
1161
- x = x > 0.008856 ? Math.pow(x, 1/3) : (7.787 * x) + (16 / 116);
1162
- y = y > 0.008856 ? Math.pow(y, 1/3) : (7.787 * y) + (16 / 116);
1163
- z = z > 0.008856 ? Math.pow(z, 1/3) : (7.787 * z) + (16 / 116);
1164
-
1165
- l = (116 * y) - 16;
1166
- a = 500 * (x - y);
1167
- b = 200 * (y - z);
1168
-
1169
- return [l, a, b];
1170
- }
1171
-
1172
- function xyz2lch(args) {
1173
- return lab2lch(xyz2lab(args));
1174
- }
1175
-
1176
- function lab2xyz(lab) {
1177
- var l = lab[0],
1178
- a = lab[1],
1179
- b = lab[2],
1180
- x, y, z, y2;
1181
-
1182
- if (l <= 8) {
1183
- y = (l * 100) / 903.3;
1184
- y2 = (7.787 * (y / 100)) + (16 / 116);
1185
- } else {
1186
- y = 100 * Math.pow((l + 16) / 116, 3);
1187
- y2 = Math.pow(y / 100, 1/3);
1188
- }
1189
-
1190
- x = x / 95.047 <= 0.008856 ? x = (95.047 * ((a / 500) + y2 - (16 / 116))) / 7.787 : 95.047 * Math.pow((a / 500) + y2, 3);
1191
-
1192
- z = z / 108.883 <= 0.008859 ? z = (108.883 * (y2 - (b / 200) - (16 / 116))) / 7.787 : 108.883 * Math.pow(y2 - (b / 200), 3);
1193
-
1194
- return [x, y, z];
1195
- }
1196
-
1197
- function lab2lch(lab) {
1198
- var l = lab[0],
1199
- a = lab[1],
1200
- b = lab[2],
1201
- hr, h, c;
1202
-
1203
- hr = Math.atan2(b, a);
1204
- h = hr * 360 / 2 / Math.PI;
1205
- if (h < 0) {
1206
- h += 360;
1207
- }
1208
- c = Math.sqrt(a * a + b * b);
1209
- return [l, c, h];
1210
- }
1211
-
1212
- function lab2rgb(args) {
1213
- return xyz2rgb(lab2xyz(args));
1214
- }
1215
-
1216
- function lch2lab(lch) {
1217
- var l = lch[0],
1218
- c = lch[1],
1219
- h = lch[2],
1220
- a, b, hr;
1221
-
1222
- hr = h / 360 * 2 * Math.PI;
1223
- a = c * Math.cos(hr);
1224
- b = c * Math.sin(hr);
1225
- return [l, a, b];
1226
- }
1227
-
1228
- function lch2xyz(args) {
1229
- return lab2xyz(lch2lab(args));
1230
- }
1231
-
1232
- function lch2rgb(args) {
1233
- return lab2rgb(lch2lab(args));
1234
- }
1235
-
1236
- function keyword2rgb(keyword) {
1237
- return cssKeywords[keyword];
1238
- }
1239
-
1240
- function keyword2hsl(args) {
1241
- return rgb2hsl(keyword2rgb(args));
1242
- }
1243
-
1244
- function keyword2hsv(args) {
1245
- return rgb2hsv(keyword2rgb(args));
1246
- }
1247
-
1248
- function keyword2hwb(args) {
1249
- return rgb2hwb(keyword2rgb(args));
1250
- }
1251
-
1252
- function keyword2cmyk(args) {
1253
- return rgb2cmyk(keyword2rgb(args));
1254
- }
1255
-
1256
- function keyword2lab(args) {
1257
- return rgb2lab(keyword2rgb(args));
1258
- }
1259
-
1260
- function keyword2xyz(args) {
1261
- return rgb2xyz(keyword2rgb(args));
552
+ function keyword2xyz(args) {
553
+ return rgb2xyz(keyword2rgb(args));
1262
554
  }
1263
555
 
1264
556
  var cssKeywords = {
@@ -1417,12 +709,9 @@ for (var key in cssKeywords) {
1417
709
  reverseKeywords[JSON.stringify(cssKeywords[key])] = key;
1418
710
  }
1419
711
 
1420
- },{}],4:[function(require,module,exports){
1421
- var conversions = require(3);
1422
-
1423
712
  var convert = function() {
1424
713
  return new Converter();
1425
- }
714
+ };
1426
715
 
1427
716
  for (var func in conversions) {
1428
717
  // export Raw versions
@@ -1506,14 +795,12 @@ Converter.prototype.getValues = function(space) {
1506
795
  ["rgb", "hsl", "hsv", "cmyk", "keyword"].forEach(function(space) {
1507
796
  Converter.prototype[space] = function(vals) {
1508
797
  return this.routeSpace(space, arguments);
1509
- }
798
+ };
1510
799
  });
1511
800
 
1512
- module.exports = convert;
1513
- },{"3":3}],5:[function(require,module,exports){
1514
- 'use strict'
1515
-
1516
- module.exports = {
801
+ var colorConvert = convert;
802
+
803
+ var colorName = {
1517
804
  "aliceblue": [240, 248, 255],
1518
805
  "antiquewhite": [250, 235, 215],
1519
806
  "aqua": [0, 255, 255],
@@ -1662,17301 +949,18340 @@ module.exports = {
1662
949
  "whitesmoke": [245, 245, 245],
1663
950
  "yellow": [255, 255, 0],
1664
951
  "yellowgreen": [154, 205, 50]
1665
- };
952
+ };
1666
953
 
1667
- },{}],6:[function(require,module,exports){
1668
- //! moment.js
954
+ /* MIT license */
1669
955
 
1670
- ;(function (global, factory) {
1671
- typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
1672
- typeof define === 'function' && define.amd ? define(factory) :
1673
- global.moment = factory()
1674
- }(this, (function () { 'use strict';
1675
956
 
1676
- var hookCallback;
957
+ var colorString = {
958
+ getRgba: getRgba,
959
+ getHsla: getHsla,
960
+ getRgb: getRgb,
961
+ getHsl: getHsl,
962
+ getHwb: getHwb,
963
+ getAlpha: getAlpha,
1677
964
 
1678
- function hooks () {
1679
- return hookCallback.apply(null, arguments);
1680
- }
1681
-
1682
- // This is done to register the method called with moment()
1683
- // without creating circular dependencies.
1684
- function setHookCallback (callback) {
1685
- hookCallback = callback;
1686
- }
965
+ hexString: hexString,
966
+ rgbString: rgbString,
967
+ rgbaString: rgbaString,
968
+ percentString: percentString,
969
+ percentaString: percentaString,
970
+ hslString: hslString,
971
+ hslaString: hslaString,
972
+ hwbString: hwbString,
973
+ keyword: keyword
974
+ };
1687
975
 
1688
- function isArray(input) {
1689
- return input instanceof Array || Object.prototype.toString.call(input) === '[object Array]';
1690
- }
976
+ function getRgba(string) {
977
+ if (!string) {
978
+ return;
979
+ }
980
+ var abbr = /^#([a-fA-F0-9]{3,4})$/i,
981
+ hex = /^#([a-fA-F0-9]{6}([a-fA-F0-9]{2})?)$/i,
982
+ rgba = /^rgba?\(\s*([+-]?\d+)\s*,\s*([+-]?\d+)\s*,\s*([+-]?\d+)\s*(?:,\s*([+-]?[\d\.]+)\s*)?\)$/i,
983
+ per = /^rgba?\(\s*([+-]?[\d\.]+)\%\s*,\s*([+-]?[\d\.]+)\%\s*,\s*([+-]?[\d\.]+)\%\s*(?:,\s*([+-]?[\d\.]+)\s*)?\)$/i,
984
+ keyword = /(\w+)/;
1691
985
 
1692
- function isObject(input) {
1693
- // IE8 will treat undefined and null as object if it wasn't for
1694
- // input != null
1695
- return input != null && Object.prototype.toString.call(input) === '[object Object]';
1696
- }
986
+ var rgb = [0, 0, 0],
987
+ a = 1,
988
+ match = string.match(abbr),
989
+ hexAlpha = "";
990
+ if (match) {
991
+ match = match[1];
992
+ hexAlpha = match[3];
993
+ for (var i = 0; i < rgb.length; i++) {
994
+ rgb[i] = parseInt(match[i] + match[i], 16);
995
+ }
996
+ if (hexAlpha) {
997
+ a = Math.round((parseInt(hexAlpha + hexAlpha, 16) / 255) * 100) / 100;
998
+ }
999
+ }
1000
+ else if (match = string.match(hex)) {
1001
+ hexAlpha = match[2];
1002
+ match = match[1];
1003
+ for (var i = 0; i < rgb.length; i++) {
1004
+ rgb[i] = parseInt(match.slice(i * 2, i * 2 + 2), 16);
1005
+ }
1006
+ if (hexAlpha) {
1007
+ a = Math.round((parseInt(hexAlpha, 16) / 255) * 100) / 100;
1008
+ }
1009
+ }
1010
+ else if (match = string.match(rgba)) {
1011
+ for (var i = 0; i < rgb.length; i++) {
1012
+ rgb[i] = parseInt(match[i + 1]);
1013
+ }
1014
+ a = parseFloat(match[4]);
1015
+ }
1016
+ else if (match = string.match(per)) {
1017
+ for (var i = 0; i < rgb.length; i++) {
1018
+ rgb[i] = Math.round(parseFloat(match[i + 1]) * 2.55);
1019
+ }
1020
+ a = parseFloat(match[4]);
1021
+ }
1022
+ else if (match = string.match(keyword)) {
1023
+ if (match[1] == "transparent") {
1024
+ return [0, 0, 0, 0];
1025
+ }
1026
+ rgb = colorName[match[1]];
1027
+ if (!rgb) {
1028
+ return;
1029
+ }
1030
+ }
1697
1031
 
1698
- function isObjectEmpty(obj) {
1699
- if (Object.getOwnPropertyNames) {
1700
- return (Object.getOwnPropertyNames(obj).length === 0);
1701
- } else {
1702
- var k;
1703
- for (k in obj) {
1704
- if (obj.hasOwnProperty(k)) {
1705
- return false;
1706
- }
1707
- }
1708
- return true;
1709
- }
1710
- }
1032
+ for (var i = 0; i < rgb.length; i++) {
1033
+ rgb[i] = scale(rgb[i], 0, 255);
1034
+ }
1035
+ if (!a && a != 0) {
1036
+ a = 1;
1037
+ }
1038
+ else {
1039
+ a = scale(a, 0, 1);
1040
+ }
1041
+ rgb[3] = a;
1042
+ return rgb;
1043
+ }
1711
1044
 
1712
- function isUndefined(input) {
1713
- return input === void 0;
1714
- }
1045
+ function getHsla(string) {
1046
+ if (!string) {
1047
+ return;
1048
+ }
1049
+ var hsl = /^hsla?\(\s*([+-]?\d+)(?:deg)?\s*,\s*([+-]?[\d\.]+)%\s*,\s*([+-]?[\d\.]+)%\s*(?:,\s*([+-]?[\d\.]+)\s*)?\)/;
1050
+ var match = string.match(hsl);
1051
+ if (match) {
1052
+ var alpha = parseFloat(match[4]);
1053
+ var h = scale(parseInt(match[1]), 0, 360),
1054
+ s = scale(parseFloat(match[2]), 0, 100),
1055
+ l = scale(parseFloat(match[3]), 0, 100),
1056
+ a = scale(isNaN(alpha) ? 1 : alpha, 0, 1);
1057
+ return [h, s, l, a];
1058
+ }
1059
+ }
1715
1060
 
1716
- function isNumber(input) {
1717
- return typeof input === 'number' || Object.prototype.toString.call(input) === '[object Number]';
1718
- }
1061
+ function getHwb(string) {
1062
+ if (!string) {
1063
+ return;
1064
+ }
1065
+ var hwb = /^hwb\(\s*([+-]?\d+)(?:deg)?\s*,\s*([+-]?[\d\.]+)%\s*,\s*([+-]?[\d\.]+)%\s*(?:,\s*([+-]?[\d\.]+)\s*)?\)/;
1066
+ var match = string.match(hwb);
1067
+ if (match) {
1068
+ var alpha = parseFloat(match[4]);
1069
+ var h = scale(parseInt(match[1]), 0, 360),
1070
+ w = scale(parseFloat(match[2]), 0, 100),
1071
+ b = scale(parseFloat(match[3]), 0, 100),
1072
+ a = scale(isNaN(alpha) ? 1 : alpha, 0, 1);
1073
+ return [h, w, b, a];
1074
+ }
1075
+ }
1719
1076
 
1720
- function isDate(input) {
1721
- return input instanceof Date || Object.prototype.toString.call(input) === '[object Date]';
1722
- }
1077
+ function getRgb(string) {
1078
+ var rgba = getRgba(string);
1079
+ return rgba && rgba.slice(0, 3);
1080
+ }
1723
1081
 
1724
- function map(arr, fn) {
1725
- var res = [], i;
1726
- for (i = 0; i < arr.length; ++i) {
1727
- res.push(fn(arr[i], i));
1728
- }
1729
- return res;
1730
- }
1082
+ function getHsl(string) {
1083
+ var hsla = getHsla(string);
1084
+ return hsla && hsla.slice(0, 3);
1085
+ }
1731
1086
 
1732
- function hasOwnProp(a, b) {
1733
- return Object.prototype.hasOwnProperty.call(a, b);
1734
- }
1087
+ function getAlpha(string) {
1088
+ var vals = getRgba(string);
1089
+ if (vals) {
1090
+ return vals[3];
1091
+ }
1092
+ else if (vals = getHsla(string)) {
1093
+ return vals[3];
1094
+ }
1095
+ else if (vals = getHwb(string)) {
1096
+ return vals[3];
1097
+ }
1098
+ }
1735
1099
 
1736
- function extend(a, b) {
1737
- for (var i in b) {
1738
- if (hasOwnProp(b, i)) {
1739
- a[i] = b[i];
1740
- }
1741
- }
1100
+ // generators
1101
+ function hexString(rgba, a) {
1102
+ var a = (a !== undefined && rgba.length === 3) ? a : rgba[3];
1103
+ return "#" + hexDouble(rgba[0])
1104
+ + hexDouble(rgba[1])
1105
+ + hexDouble(rgba[2])
1106
+ + (
1107
+ (a >= 0 && a < 1)
1108
+ ? hexDouble(Math.round(a * 255))
1109
+ : ""
1110
+ );
1111
+ }
1742
1112
 
1743
- if (hasOwnProp(b, 'toString')) {
1744
- a.toString = b.toString;
1745
- }
1113
+ function rgbString(rgba, alpha) {
1114
+ if (alpha < 1 || (rgba[3] && rgba[3] < 1)) {
1115
+ return rgbaString(rgba, alpha);
1116
+ }
1117
+ return "rgb(" + rgba[0] + ", " + rgba[1] + ", " + rgba[2] + ")";
1118
+ }
1746
1119
 
1747
- if (hasOwnProp(b, 'valueOf')) {
1748
- a.valueOf = b.valueOf;
1749
- }
1120
+ function rgbaString(rgba, alpha) {
1121
+ if (alpha === undefined) {
1122
+ alpha = (rgba[3] !== undefined ? rgba[3] : 1);
1123
+ }
1124
+ return "rgba(" + rgba[0] + ", " + rgba[1] + ", " + rgba[2]
1125
+ + ", " + alpha + ")";
1126
+ }
1750
1127
 
1751
- return a;
1752
- }
1128
+ function percentString(rgba, alpha) {
1129
+ if (alpha < 1 || (rgba[3] && rgba[3] < 1)) {
1130
+ return percentaString(rgba, alpha);
1131
+ }
1132
+ var r = Math.round(rgba[0]/255 * 100),
1133
+ g = Math.round(rgba[1]/255 * 100),
1134
+ b = Math.round(rgba[2]/255 * 100);
1753
1135
 
1754
- function createUTC (input, format, locale, strict) {
1755
- return createLocalOrUTC(input, format, locale, strict, true).utc();
1756
- }
1136
+ return "rgb(" + r + "%, " + g + "%, " + b + "%)";
1137
+ }
1757
1138
 
1758
- function defaultParsingFlags() {
1759
- // We need to deep clone this object.
1760
- return {
1761
- empty : false,
1762
- unusedTokens : [],
1763
- unusedInput : [],
1764
- overflow : -2,
1765
- charsLeftOver : 0,
1766
- nullInput : false,
1767
- invalidMonth : null,
1768
- invalidFormat : false,
1769
- userInvalidated : false,
1770
- iso : false,
1771
- parsedDateParts : [],
1772
- meridiem : null,
1773
- rfc2822 : false,
1774
- weekdayMismatch : false
1775
- };
1776
- }
1139
+ function percentaString(rgba, alpha) {
1140
+ var r = Math.round(rgba[0]/255 * 100),
1141
+ g = Math.round(rgba[1]/255 * 100),
1142
+ b = Math.round(rgba[2]/255 * 100);
1143
+ return "rgba(" + r + "%, " + g + "%, " + b + "%, " + (alpha || rgba[3] || 1) + ")";
1144
+ }
1777
1145
 
1778
- function getParsingFlags(m) {
1779
- if (m._pf == null) {
1780
- m._pf = defaultParsingFlags();
1781
- }
1782
- return m._pf;
1783
- }
1146
+ function hslString(hsla, alpha) {
1147
+ if (alpha < 1 || (hsla[3] && hsla[3] < 1)) {
1148
+ return hslaString(hsla, alpha);
1149
+ }
1150
+ return "hsl(" + hsla[0] + ", " + hsla[1] + "%, " + hsla[2] + "%)";
1151
+ }
1784
1152
 
1785
- var some;
1786
- if (Array.prototype.some) {
1787
- some = Array.prototype.some;
1788
- } else {
1789
- some = function (fun) {
1790
- var t = Object(this);
1791
- var len = t.length >>> 0;
1153
+ function hslaString(hsla, alpha) {
1154
+ if (alpha === undefined) {
1155
+ alpha = (hsla[3] !== undefined ? hsla[3] : 1);
1156
+ }
1157
+ return "hsla(" + hsla[0] + ", " + hsla[1] + "%, " + hsla[2] + "%, "
1158
+ + alpha + ")";
1159
+ }
1792
1160
 
1793
- for (var i = 0; i < len; i++) {
1794
- if (i in t && fun.call(this, t[i], i, t)) {
1795
- return true;
1796
- }
1797
- }
1161
+ // hwb is a bit different than rgb(a) & hsl(a) since there is no alpha specific syntax
1162
+ // (hwb have alpha optional & 1 is default value)
1163
+ function hwbString(hwb, alpha) {
1164
+ if (alpha === undefined) {
1165
+ alpha = (hwb[3] !== undefined ? hwb[3] : 1);
1166
+ }
1167
+ return "hwb(" + hwb[0] + ", " + hwb[1] + "%, " + hwb[2] + "%"
1168
+ + (alpha !== undefined && alpha !== 1 ? ", " + alpha : "") + ")";
1169
+ }
1170
+
1171
+ function keyword(rgb) {
1172
+ return reverseNames[rgb.slice(0, 3)];
1173
+ }
1174
+
1175
+ // helpers
1176
+ function scale(num, min, max) {
1177
+ return Math.min(Math.max(min, num), max);
1178
+ }
1179
+
1180
+ function hexDouble(num) {
1181
+ var str = num.toString(16).toUpperCase();
1182
+ return (str.length < 2) ? "0" + str : str;
1183
+ }
1184
+
1185
+
1186
+ //create a list of reverse color names
1187
+ var reverseNames = {};
1188
+ for (var name in colorName) {
1189
+ reverseNames[colorName[name]] = name;
1190
+ }
1191
+
1192
+ /* MIT license */
1193
+
1194
+
1195
+
1196
+ var Color = function (obj) {
1197
+ if (obj instanceof Color) {
1198
+ return obj;
1199
+ }
1200
+ if (!(this instanceof Color)) {
1201
+ return new Color(obj);
1202
+ }
1203
+
1204
+ this.valid = false;
1205
+ this.values = {
1206
+ rgb: [0, 0, 0],
1207
+ hsl: [0, 0, 0],
1208
+ hsv: [0, 0, 0],
1209
+ hwb: [0, 0, 0],
1210
+ cmyk: [0, 0, 0, 0],
1211
+ alpha: 1
1212
+ };
1213
+
1214
+ // parse Color() argument
1215
+ var vals;
1216
+ if (typeof obj === 'string') {
1217
+ vals = colorString.getRgba(obj);
1218
+ if (vals) {
1219
+ this.setValues('rgb', vals);
1220
+ } else if (vals = colorString.getHsla(obj)) {
1221
+ this.setValues('hsl', vals);
1222
+ } else if (vals = colorString.getHwb(obj)) {
1223
+ this.setValues('hwb', vals);
1224
+ }
1225
+ } else if (typeof obj === 'object') {
1226
+ vals = obj;
1227
+ if (vals.r !== undefined || vals.red !== undefined) {
1228
+ this.setValues('rgb', vals);
1229
+ } else if (vals.l !== undefined || vals.lightness !== undefined) {
1230
+ this.setValues('hsl', vals);
1231
+ } else if (vals.v !== undefined || vals.value !== undefined) {
1232
+ this.setValues('hsv', vals);
1233
+ } else if (vals.w !== undefined || vals.whiteness !== undefined) {
1234
+ this.setValues('hwb', vals);
1235
+ } else if (vals.c !== undefined || vals.cyan !== undefined) {
1236
+ this.setValues('cmyk', vals);
1237
+ }
1238
+ }
1239
+ };
1240
+
1241
+ Color.prototype = {
1242
+ isValid: function () {
1243
+ return this.valid;
1244
+ },
1245
+ rgb: function () {
1246
+ return this.setSpace('rgb', arguments);
1247
+ },
1248
+ hsl: function () {
1249
+ return this.setSpace('hsl', arguments);
1250
+ },
1251
+ hsv: function () {
1252
+ return this.setSpace('hsv', arguments);
1253
+ },
1254
+ hwb: function () {
1255
+ return this.setSpace('hwb', arguments);
1256
+ },
1257
+ cmyk: function () {
1258
+ return this.setSpace('cmyk', arguments);
1259
+ },
1260
+
1261
+ rgbArray: function () {
1262
+ return this.values.rgb;
1263
+ },
1264
+ hslArray: function () {
1265