@ctrl/tinycolor 3.6.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.numberInputToObject = exports.parseIntFromHex = exports.convertHexToDecimal = exports.convertDecimalToHex = exports.rgbaToArgbHex = exports.rgbaToHex = exports.rgbToHex = exports.hsvToRgb = exports.rgbToHsv = exports.hslToRgb = exports.rgbToHsl = exports.rgbToRgb = void 0;
4
- var util_1 = require("./util");
4
+ const util_js_1 = require("./util.js");
5
5
  // `rgbToHsl`, `rgbToHsv`, `hslToRgb`, `hsvToRgb` modified from:
6
6
  // <http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript>
7
7
  /**
@@ -12,9 +12,9 @@ var util_1 = require("./util");
12
12
  */
13
13
  function rgbToRgb(r, g, b) {
14
14
  return {
15
- r: (0, util_1.bound01)(r, 255) * 255,
16
- g: (0, util_1.bound01)(g, 255) * 255,
17
- b: (0, util_1.bound01)(b, 255) * 255,
15
+ r: (0, util_js_1.bound01)(r, 255) * 255,
16
+ g: (0, util_js_1.bound01)(g, 255) * 255,
17
+ b: (0, util_js_1.bound01)(b, 255) * 255,
18
18
  };
19
19
  }
20
20
  exports.rgbToRgb = rgbToRgb;
@@ -24,20 +24,20 @@ exports.rgbToRgb = rgbToRgb;
24
24
  * *Returns:* { h, s, l } in [0,1]
25
25
  */
26
26
  function rgbToHsl(r, g, b) {
27
- r = (0, util_1.bound01)(r, 255);
28
- g = (0, util_1.bound01)(g, 255);
29
- b = (0, util_1.bound01)(b, 255);
30
- var max = Math.max(r, g, b);
31
- var min = Math.min(r, g, b);
32
- var h = 0;
33
- var s = 0;
34
- var l = (max + min) / 2;
27
+ r = (0, util_js_1.bound01)(r, 255);
28
+ g = (0, util_js_1.bound01)(g, 255);
29
+ b = (0, util_js_1.bound01)(b, 255);
30
+ const max = Math.max(r, g, b);
31
+ const min = Math.min(r, g, b);
32
+ let h = 0;
33
+ let s = 0;
34
+ const l = (max + min) / 2;
35
35
  if (max === min) {
36
36
  s = 0;
37
37
  h = 0; // achromatic
38
38
  }
39
39
  else {
40
- var d = max - min;
40
+ const d = max - min;
41
41
  s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
42
42
  switch (max) {
43
43
  case r:
@@ -54,7 +54,7 @@ function rgbToHsl(r, g, b) {
54
54
  }
55
55
  h /= 6;
56
56
  }
57
- return { h: h, s: s, l: l };
57
+ return { h, s, l };
58
58
  }
59
59
  exports.rgbToHsl = rgbToHsl;
60
60
  function hue2rgb(p, q, t) {
@@ -82,12 +82,12 @@ function hue2rgb(p, q, t) {
82
82
  * *Returns:* { r, g, b } in the set [0, 255]
83
83
  */
84
84
  function hslToRgb(h, s, l) {
85
- var r;
86
- var g;
87
- var b;
88
- h = (0, util_1.bound01)(h, 360);
89
- s = (0, util_1.bound01)(s, 100);
90
- l = (0, util_1.bound01)(l, 100);
85
+ let r;
86
+ let g;
87
+ let b;
88
+ h = (0, util_js_1.bound01)(h, 360);
89
+ s = (0, util_js_1.bound01)(s, 100);
90
+ l = (0, util_js_1.bound01)(l, 100);
91
91
  if (s === 0) {
92
92
  // achromatic
93
93
  g = l;
@@ -95,8 +95,8 @@ function hslToRgb(h, s, l) {
95
95
  r = l;
96
96
  }
97
97
  else {
98
- var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
99
- var p = 2 * l - q;
98
+ const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
99
+ const p = 2 * l - q;
100
100
  r = hue2rgb(p, q, h + 1 / 3);
101
101
  g = hue2rgb(p, q, h);
102
102
  b = hue2rgb(p, q, h - 1 / 3);
@@ -111,15 +111,15 @@ exports.hslToRgb = hslToRgb;
111
111
  * *Returns:* { h, s, v } in [0,1]
112
112
  */
113
113
  function rgbToHsv(r, g, b) {
114
- r = (0, util_1.bound01)(r, 255);
115
- g = (0, util_1.bound01)(g, 255);
116
- b = (0, util_1.bound01)(b, 255);
117
- var max = Math.max(r, g, b);
118
- var min = Math.min(r, g, b);
119
- var h = 0;
120
- var v = max;
121
- var d = max - min;
122
- var s = max === 0 ? 0 : d / max;
114
+ r = (0, util_js_1.bound01)(r, 255);
115
+ g = (0, util_js_1.bound01)(g, 255);
116
+ b = (0, util_js_1.bound01)(b, 255);
117
+ const max = Math.max(r, g, b);
118
+ const min = Math.min(r, g, b);
119
+ let h = 0;
120
+ const v = max;
121
+ const d = max - min;
122
+ const s = max === 0 ? 0 : d / max;
123
123
  if (max === min) {
124
124
  h = 0; // achromatic
125
125
  }
@@ -139,7 +139,7 @@ function rgbToHsv(r, g, b) {
139
139
  }
140
140
  h /= 6;
141
141
  }
142
- return { h: h, s: s, v: v };
142
+ return { h, s, v };
143
143
  }
144
144
  exports.rgbToHsv = rgbToHsv;
145
145
  /**
@@ -149,18 +149,18 @@ exports.rgbToHsv = rgbToHsv;
149
149
  * *Returns:* { r, g, b } in the set [0, 255]
150
150
  */
151
151
  function hsvToRgb(h, s, v) {
152
- h = (0, util_1.bound01)(h, 360) * 6;
153
- s = (0, util_1.bound01)(s, 100);
154
- v = (0, util_1.bound01)(v, 100);
155
- var i = Math.floor(h);
156
- var f = h - i;
157
- var p = v * (1 - s);
158
- var q = v * (1 - f * s);
159
- var t = v * (1 - (1 - f) * s);
160
- var mod = i % 6;
161
- var r = [v, q, p, p, t, v][mod];
162
- var g = [t, v, v, q, p, p][mod];
163
- var b = [p, p, t, v, v, q][mod];
152
+ h = (0, util_js_1.bound01)(h, 360) * 6;
153
+ s = (0, util_js_1.bound01)(s, 100);
154
+ v = (0, util_js_1.bound01)(v, 100);
155
+ const i = Math.floor(h);
156
+ const f = h - i;
157
+ const p = v * (1 - s);
158
+ const q = v * (1 - f * s);
159
+ const t = v * (1 - (1 - f) * s);
160
+ const mod = i % 6;
161
+ const r = [v, q, p, p, t, v][mod];
162
+ const g = [t, v, v, q, p, p][mod];
163
+ const b = [p, p, t, v, v, q][mod];
164
164
  return { r: r * 255, g: g * 255, b: b * 255 };
165
165
  }
166
166
  exports.hsvToRgb = hsvToRgb;
@@ -171,10 +171,10 @@ exports.hsvToRgb = hsvToRgb;
171
171
  * Returns a 3 or 6 character hex
172
172
  */
173
173
  function rgbToHex(r, g, b, allow3Char) {
174
- var hex = [
175
- (0, util_1.pad2)(Math.round(r).toString(16)),
176
- (0, util_1.pad2)(Math.round(g).toString(16)),
177
- (0, util_1.pad2)(Math.round(b).toString(16)),
174
+ const hex = [
175
+ (0, util_js_1.pad2)(Math.round(r).toString(16)),
176
+ (0, util_js_1.pad2)(Math.round(g).toString(16)),
177
+ (0, util_js_1.pad2)(Math.round(b).toString(16)),
178
178
  ];
179
179
  // Return a 3 character hex if possible
180
180
  if (allow3Char &&
@@ -194,11 +194,11 @@ exports.rgbToHex = rgbToHex;
194
194
  */
195
195
  // eslint-disable-next-line max-params
196
196
  function rgbaToHex(r, g, b, a, allow4Char) {
197
- var hex = [
198
- (0, util_1.pad2)(Math.round(r).toString(16)),
199
- (0, util_1.pad2)(Math.round(g).toString(16)),
200
- (0, util_1.pad2)(Math.round(b).toString(16)),
201
- (0, util_1.pad2)(convertDecimalToHex(a)),
197
+ const hex = [
198
+ (0, util_js_1.pad2)(Math.round(r).toString(16)),
199
+ (0, util_js_1.pad2)(Math.round(g).toString(16)),
200
+ (0, util_js_1.pad2)(Math.round(b).toString(16)),
201
+ (0, util_js_1.pad2)(convertDecimalToHex(a)),
202
202
  ];
203
203
  // Return a 4 character hex if possible
204
204
  if (allow4Char &&
@@ -216,11 +216,11 @@ exports.rgbaToHex = rgbaToHex;
216
216
  * Rarely used, but required for "toFilter()"
217
217
  */
218
218
  function rgbaToArgbHex(r, g, b, a) {
219
- var hex = [
220
- (0, util_1.pad2)(convertDecimalToHex(a)),
221
- (0, util_1.pad2)(Math.round(r).toString(16)),
222
- (0, util_1.pad2)(Math.round(g).toString(16)),
223
- (0, util_1.pad2)(Math.round(b).toString(16)),
219
+ const hex = [
220
+ (0, util_js_1.pad2)(convertDecimalToHex(a)),
221
+ (0, util_js_1.pad2)(Math.round(r).toString(16)),
222
+ (0, util_js_1.pad2)(Math.round(g).toString(16)),
223
+ (0, util_js_1.pad2)(Math.round(b).toString(16)),
224
224
  ];
225
225
  return hex.join('');
226
226
  }
@@ -1,4 +1,4 @@
1
- import { HSL, HSLA, HSV, HSVA, RGB, RGBA } from './interfaces';
1
+ import { HSL, HSLA, HSV, HSVA, RGB, RGBA } from './interfaces.js';
2
2
  /**
3
3
  * Given a string or object, convert that input to RGB
4
4
  *
@@ -1,10 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.isValidCSSUnit = exports.stringInputToObject = exports.inputToRGB = void 0;
4
- /* eslint-disable @typescript-eslint/no-redundant-type-constituents */
5
- var conversion_1 = require("./conversion");
6
- var css_color_names_1 = require("./css-color-names");
7
- var util_1 = require("./util");
4
+ const conversion_js_1 = require("./conversion.js");
5
+ const css_color_names_js_1 = require("./css-color-names.js");
6
+ const util_js_1 = require("./util.js");
8
7
  /**
9
8
  * Given a string or object, convert that input to RGB
10
9
  *
@@ -24,33 +23,33 @@ var util_1 = require("./util");
24
23
  * ```
25
24
  */
26
25
  function inputToRGB(color) {
27
- var rgb = { r: 0, g: 0, b: 0 };
28
- var a = 1;
29
- var s = null;
30
- var v = null;
31
- var l = null;
32
- var ok = false;
33
- var format = false;
26
+ let rgb = { r: 0, g: 0, b: 0 };
27
+ let a = 1;
28
+ let s = null;
29
+ let v = null;
30
+ let l = null;
31
+ let ok = false;
32
+ let format = false;
34
33
  if (typeof color === 'string') {
35
34
  color = stringInputToObject(color);
36
35
  }
37
36
  if (typeof color === 'object') {
38
37
  if (isValidCSSUnit(color.r) && isValidCSSUnit(color.g) && isValidCSSUnit(color.b)) {
39
- rgb = (0, conversion_1.rgbToRgb)(color.r, color.g, color.b);
38
+ rgb = (0, conversion_js_1.rgbToRgb)(color.r, color.g, color.b);
40
39
  ok = true;
41
40
  format = String(color.r).substr(-1) === '%' ? 'prgb' : 'rgb';
42
41
  }
43
42
  else if (isValidCSSUnit(color.h) && isValidCSSUnit(color.s) && isValidCSSUnit(color.v)) {
44
- s = (0, util_1.convertToPercentage)(color.s);
45
- v = (0, util_1.convertToPercentage)(color.v);
46
- rgb = (0, conversion_1.hsvToRgb)(color.h, s, v);
43
+ s = (0, util_js_1.convertToPercentage)(color.s);
44
+ v = (0, util_js_1.convertToPercentage)(color.v);
45
+ rgb = (0, conversion_js_1.hsvToRgb)(color.h, s, v);
47
46
  ok = true;
48
47
  format = 'hsv';
49
48
  }
50
49
  else if (isValidCSSUnit(color.h) && isValidCSSUnit(color.s) && isValidCSSUnit(color.l)) {
51
- s = (0, util_1.convertToPercentage)(color.s);
52
- l = (0, util_1.convertToPercentage)(color.l);
53
- rgb = (0, conversion_1.hslToRgb)(color.h, s, l);
50
+ s = (0, util_js_1.convertToPercentage)(color.s);
51
+ l = (0, util_js_1.convertToPercentage)(color.l);
52
+ rgb = (0, conversion_js_1.hslToRgb)(color.h, s, l);
54
53
  ok = true;
55
54
  format = 'hsl';
56
55
  }
@@ -58,29 +57,29 @@ function inputToRGB(color) {
58
57
  a = color.a;
59
58
  }
60
59
  }
61
- a = (0, util_1.boundAlpha)(a);
60
+ a = (0, util_js_1.boundAlpha)(a);
62
61
  return {
63
- ok: ok,
62
+ ok,
64
63
  format: color.format || format,
65
64
  r: Math.min(255, Math.max(rgb.r, 0)),
66
65
  g: Math.min(255, Math.max(rgb.g, 0)),
67
66
  b: Math.min(255, Math.max(rgb.b, 0)),
68
- a: a,
67
+ a,
69
68
  };
70
69
  }
71
70
  exports.inputToRGB = inputToRGB;
72
71
  // <http://www.w3.org/TR/css3-values/#integers>
73
- var CSS_INTEGER = '[-\\+]?\\d+%?';
72
+ const CSS_INTEGER = '[-\\+]?\\d+%?';
74
73
  // <http://www.w3.org/TR/css3-values/#number-value>
75
- var CSS_NUMBER = '[-\\+]?\\d*\\.\\d+%?';
74
+ const CSS_NUMBER = '[-\\+]?\\d*\\.\\d+%?';
76
75
  // Allow positive/negative integer/number. Don't capture the either/or, just the entire outcome.
77
- var CSS_UNIT = "(?:".concat(CSS_NUMBER, ")|(?:").concat(CSS_INTEGER, ")");
76
+ const CSS_UNIT = `(?:${CSS_NUMBER})|(?:${CSS_INTEGER})`;
78
77
  // Actual matching.
79
78
  // Parentheses and commas are optional, but not required.
80
79
  // Whitespace can take the place of commas or opening paren
81
- var PERMISSIVE_MATCH3 = "[\\s|\\(]+(".concat(CSS_UNIT, ")[,|\\s]+(").concat(CSS_UNIT, ")[,|\\s]+(").concat(CSS_UNIT, ")\\s*\\)?");
82
- var PERMISSIVE_MATCH4 = "[\\s|\\(]+(".concat(CSS_UNIT, ")[,|\\s]+(").concat(CSS_UNIT, ")[,|\\s]+(").concat(CSS_UNIT, ")[,|\\s]+(").concat(CSS_UNIT, ")\\s*\\)?");
83
- var matchers = {
80
+ const PERMISSIVE_MATCH3 = `[\\s|\\(]+(${CSS_UNIT})[,|\\s]+(${CSS_UNIT})[,|\\s]+(${CSS_UNIT})\\s*\\)?`;
81
+ const PERMISSIVE_MATCH4 = `[\\s|\\(]+(${CSS_UNIT})[,|\\s]+(${CSS_UNIT})[,|\\s]+(${CSS_UNIT})[,|\\s]+(${CSS_UNIT})\\s*\\)?`;
82
+ const matchers = {
84
83
  CSS_UNIT: new RegExp(CSS_UNIT),
85
84
  rgb: new RegExp('rgb' + PERMISSIVE_MATCH3),
86
85
  rgba: new RegExp('rgba' + PERMISSIVE_MATCH4),
@@ -102,9 +101,9 @@ function stringInputToObject(color) {
102
101
  if (color.length === 0) {
103
102
  return false;
104
103
  }
105
- var named = false;
106
- if (css_color_names_1.names[color]) {
107
- color = css_color_names_1.names[color];
104
+ let named = false;
105
+ if (css_color_names_js_1.names[color]) {
106
+ color = css_color_names_js_1.names[color];
108
107
  named = true;
109
108
  }
110
109
  else if (color === 'transparent') {
@@ -114,7 +113,7 @@ function stringInputToObject(color) {
114
113
  // Keep most of the number bounding out of this function - don't worry about [0,1] or [0,100] or [0,360]
115
114
  // Just return an object and let the conversion functions handle that.
116
115
  // This way the result will be the same whether the tinycolor is initialized with string or object.
117
- var match = matchers.rgb.exec(color);
116
+ let match = matchers.rgb.exec(color);
118
117
  if (match) {
119
118
  return { r: match[1], g: match[2], b: match[3] };
120
119
  }
@@ -141,38 +140,38 @@ function stringInputToObject(color) {
141
140
  match = matchers.hex8.exec(color);
142
141
  if (match) {
143
142
  return {
144
- r: (0, conversion_1.parseIntFromHex)(match[1]),
145
- g: (0, conversion_1.parseIntFromHex)(match[2]),
146
- b: (0, conversion_1.parseIntFromHex)(match[3]),
147
- a: (0, conversion_1.convertHexToDecimal)(match[4]),
143
+ r: (0, conversion_js_1.parseIntFromHex)(match[1]),
144
+ g: (0, conversion_js_1.parseIntFromHex)(match[2]),
145
+ b: (0, conversion_js_1.parseIntFromHex)(match[3]),
146
+ a: (0, conversion_js_1.convertHexToDecimal)(match[4]),
148
147
  format: named ? 'name' : 'hex8',
149
148
  };
150
149
  }
151
150
  match = matchers.hex6.exec(color);
152
151
  if (match) {
153
152
  return {
154
- r: (0, conversion_1.parseIntFromHex)(match[1]),
155
- g: (0, conversion_1.parseIntFromHex)(match[2]),
156
- b: (0, conversion_1.parseIntFromHex)(match[3]),
153
+ r: (0, conversion_js_1.parseIntFromHex)(match[1]),
154
+ g: (0, conversion_js_1.parseIntFromHex)(match[2]),
155
+ b: (0, conversion_js_1.parseIntFromHex)(match[3]),
157
156
  format: named ? 'name' : 'hex',
158
157
  };
159
158
  }
160
159
  match = matchers.hex4.exec(color);
161
160
  if (match) {
162
161
  return {
163
- r: (0, conversion_1.parseIntFromHex)(match[1] + match[1]),
164
- g: (0, conversion_1.parseIntFromHex)(match[2] + match[2]),
165
- b: (0, conversion_1.parseIntFromHex)(match[3] + match[3]),
166
- a: (0, conversion_1.convertHexToDecimal)(match[4] + match[4]),
162
+ r: (0, conversion_js_1.parseIntFromHex)(match[1] + match[1]),
163
+ g: (0, conversion_js_1.parseIntFromHex)(match[2] + match[2]),
164
+ b: (0, conversion_js_1.parseIntFromHex)(match[3] + match[3]),
165
+ a: (0, conversion_js_1.convertHexToDecimal)(match[4] + match[4]),
167
166
  format: named ? 'name' : 'hex8',
168
167
  };
169
168
  }
170
169
  match = matchers.hex3.exec(color);
171
170
  if (match) {
172
171
  return {
173
- r: (0, conversion_1.parseIntFromHex)(match[1] + match[1]),
174
- g: (0, conversion_1.parseIntFromHex)(match[2] + match[2]),
175
- b: (0, conversion_1.parseIntFromHex)(match[3] + match[3]),
172
+ r: (0, conversion_js_1.parseIntFromHex)(match[1] + match[1]),
173
+ g: (0, conversion_js_1.parseIntFromHex)(match[2] + match[2]),
174
+ b: (0, conversion_js_1.parseIntFromHex)(match[3] + match[3]),
176
175
  format: named ? 'name' : 'hex',
177
176
  };
178
177
  }
@@ -1,4 +1,4 @@
1
- import { TinyColor } from './index';
1
+ import { TinyColor } from './index.js';
2
2
  export interface RatioInput {
3
3
  r: number | string;
4
4
  g: number | string;
@@ -1,27 +1,27 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.legacyRandom = exports.fromRatio = void 0;
4
- var index_1 = require("./index");
5
- var util_1 = require("./util");
4
+ const index_js_1 = require("./index.js");
5
+ const util_js_1 = require("./util.js");
6
6
  /**
7
7
  * If input is an object, force 1 into "1.0" to handle ratios properly
8
8
  * String input requires "1.0" as input, so 1 will be treated as 1
9
9
  */
10
10
  function fromRatio(ratio, opts) {
11
- var newColor = {
12
- r: (0, util_1.convertToPercentage)(ratio.r),
13
- g: (0, util_1.convertToPercentage)(ratio.g),
14
- b: (0, util_1.convertToPercentage)(ratio.b),
11
+ const newColor = {
12
+ r: (0, util_js_1.convertToPercentage)(ratio.r),
13
+ g: (0, util_js_1.convertToPercentage)(ratio.g),
14
+ b: (0, util_js_1.convertToPercentage)(ratio.b),
15
15
  };
16
16
  if (ratio.a !== undefined) {
17
17
  newColor.a = Number(ratio.a);
18
18
  }
19
- return new index_1.TinyColor(newColor, opts);
19
+ return new index_js_1.TinyColor(newColor, opts);
20
20
  }
21
21
  exports.fromRatio = fromRatio;
22
22
  /** old random function */
23
23
  function legacyRandom() {
24
- return new index_1.TinyColor({
24
+ return new index_js_1.TinyColor({
25
25
  r: Math.random(),
26
26
  g: Math.random(),
27
27
  b: Math.random(),
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { HSL, HSLA, HSV, HSVA, Numberify, RGB, RGBA } from './interfaces';
1
+ import { HSL, HSLA, HSV, HSVA, Numberify, RGB, RGBA } from './interfaces.js';
2
2
  export interface TinyColorOptions {
3
3
  format: string;
4
4
  gradientType: string;
@@ -204,4 +204,3 @@ export declare class TinyColor {
204
204
  */
205
205
  equals(color?: ColorInput): boolean;
206
206
  }
207
- export declare function tinycolor(color?: ColorInput, opts?: Partial<TinyColorOptions>): TinyColor;