@ctrl/tinycolor 3.6.1 → 4.0.1
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.
- package/README.md +1 -1
- package/dist/bundles/tinycolor.umd.min.js +1 -1
- package/dist/bundles/tinycolor.umd.min.js.map +1 -1
- package/dist/conversion.js +32 -32
- package/dist/format-input.js +20 -21
- package/dist/from-ratio.js +3 -3
- package/dist/index.d.ts +0 -1
- package/dist/index.js +164 -192
- package/dist/module/conversion.js +31 -31
- package/dist/module/css-color-names.js +1 -1
- package/dist/module/format-input.js +17 -18
- package/dist/module/from-ratio.js +1 -1
- package/dist/module/index.js +159 -187
- package/dist/module/public_api.js +0 -3
- package/dist/module/random.js +37 -40
- package/dist/module/readability.js +12 -16
- package/dist/module/to-ms-filter.js +6 -6
- package/dist/module/umd_api.js +14 -13
- package/dist/module/util.js +3 -3
- package/dist/public_api.d.ts +0 -2
- package/dist/public_api.js +0 -3
- package/dist/random.js +37 -40
- package/dist/readability.js +13 -17
- package/dist/to-ms-filter.js +8 -8
- package/dist/umd_api.d.ts +0 -1
- package/dist/umd_api.js +20 -19
- package/dist/util.js +3 -3
- package/package.json +19 -39
@@ -23,17 +23,17 @@ export function rgbToHsl(r, g, b) {
|
|
23
23
|
r = bound01(r, 255);
|
24
24
|
g = bound01(g, 255);
|
25
25
|
b = bound01(b, 255);
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
26
|
+
const max = Math.max(r, g, b);
|
27
|
+
const min = Math.min(r, g, b);
|
28
|
+
let h = 0;
|
29
|
+
let s = 0;
|
30
|
+
const l = (max + min) / 2;
|
31
31
|
if (max === min) {
|
32
32
|
s = 0;
|
33
33
|
h = 0; // achromatic
|
34
34
|
}
|
35
35
|
else {
|
36
|
-
|
36
|
+
const d = max - min;
|
37
37
|
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
38
38
|
switch (max) {
|
39
39
|
case r:
|
@@ -50,7 +50,7 @@ export function rgbToHsl(r, g, b) {
|
|
50
50
|
}
|
51
51
|
h /= 6;
|
52
52
|
}
|
53
|
-
return { h
|
53
|
+
return { h, s, l };
|
54
54
|
}
|
55
55
|
function hue2rgb(p, q, t) {
|
56
56
|
if (t < 0) {
|
@@ -77,9 +77,9 @@ function hue2rgb(p, q, t) {
|
|
77
77
|
* *Returns:* { r, g, b } in the set [0, 255]
|
78
78
|
*/
|
79
79
|
export function hslToRgb(h, s, l) {
|
80
|
-
|
81
|
-
|
82
|
-
|
80
|
+
let r;
|
81
|
+
let g;
|
82
|
+
let b;
|
83
83
|
h = bound01(h, 360);
|
84
84
|
s = bound01(s, 100);
|
85
85
|
l = bound01(l, 100);
|
@@ -90,8 +90,8 @@ export function hslToRgb(h, s, l) {
|
|
90
90
|
r = l;
|
91
91
|
}
|
92
92
|
else {
|
93
|
-
|
94
|
-
|
93
|
+
const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
|
94
|
+
const p = 2 * l - q;
|
95
95
|
r = hue2rgb(p, q, h + 1 / 3);
|
96
96
|
g = hue2rgb(p, q, h);
|
97
97
|
b = hue2rgb(p, q, h - 1 / 3);
|
@@ -108,12 +108,12 @@ export function rgbToHsv(r, g, b) {
|
|
108
108
|
r = bound01(r, 255);
|
109
109
|
g = bound01(g, 255);
|
110
110
|
b = bound01(b, 255);
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
111
|
+
const max = Math.max(r, g, b);
|
112
|
+
const min = Math.min(r, g, b);
|
113
|
+
let h = 0;
|
114
|
+
const v = max;
|
115
|
+
const d = max - min;
|
116
|
+
const s = max === 0 ? 0 : d / max;
|
117
117
|
if (max === min) {
|
118
118
|
h = 0; // achromatic
|
119
119
|
}
|
@@ -133,7 +133,7 @@ export function rgbToHsv(r, g, b) {
|
|
133
133
|
}
|
134
134
|
h /= 6;
|
135
135
|
}
|
136
|
-
return { h
|
136
|
+
return { h, s, v };
|
137
137
|
}
|
138
138
|
/**
|
139
139
|
* Converts an HSV color value to RGB.
|
@@ -145,15 +145,15 @@ export function hsvToRgb(h, s, v) {
|
|
145
145
|
h = bound01(h, 360) * 6;
|
146
146
|
s = bound01(s, 100);
|
147
147
|
v = bound01(v, 100);
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
148
|
+
const i = Math.floor(h);
|
149
|
+
const f = h - i;
|
150
|
+
const p = v * (1 - s);
|
151
|
+
const q = v * (1 - f * s);
|
152
|
+
const t = v * (1 - (1 - f) * s);
|
153
|
+
const mod = i % 6;
|
154
|
+
const r = [v, q, p, p, t, v][mod];
|
155
|
+
const g = [t, v, v, q, p, p][mod];
|
156
|
+
const b = [p, p, t, v, v, q][mod];
|
157
157
|
return { r: r * 255, g: g * 255, b: b * 255 };
|
158
158
|
}
|
159
159
|
/**
|
@@ -163,7 +163,7 @@ export function hsvToRgb(h, s, v) {
|
|
163
163
|
* Returns a 3 or 6 character hex
|
164
164
|
*/
|
165
165
|
export function rgbToHex(r, g, b, allow3Char) {
|
166
|
-
|
166
|
+
const hex = [
|
167
167
|
pad2(Math.round(r).toString(16)),
|
168
168
|
pad2(Math.round(g).toString(16)),
|
169
169
|
pad2(Math.round(b).toString(16)),
|
@@ -185,7 +185,7 @@ export function rgbToHex(r, g, b, allow3Char) {
|
|
185
185
|
*/
|
186
186
|
// eslint-disable-next-line max-params
|
187
187
|
export function rgbaToHex(r, g, b, a, allow4Char) {
|
188
|
-
|
188
|
+
const hex = [
|
189
189
|
pad2(Math.round(r).toString(16)),
|
190
190
|
pad2(Math.round(g).toString(16)),
|
191
191
|
pad2(Math.round(b).toString(16)),
|
@@ -206,7 +206,7 @@ export function rgbaToHex(r, g, b, a, allow4Char) {
|
|
206
206
|
* Rarely used, but required for "toFilter()"
|
207
207
|
*/
|
208
208
|
export function rgbaToArgbHex(r, g, b, a) {
|
209
|
-
|
209
|
+
const hex = [
|
210
210
|
pad2(convertDecimalToHex(a)),
|
211
211
|
pad2(Math.round(r).toString(16)),
|
212
212
|
pad2(Math.round(g).toString(16)),
|
@@ -1,4 +1,3 @@
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-redundant-type-constituents */
|
2
1
|
import { convertHexToDecimal, hslToRgb, hsvToRgb, parseIntFromHex, rgbToRgb, } from './conversion.js';
|
3
2
|
import { names } from './css-color-names.js';
|
4
3
|
import { boundAlpha, convertToPercentage } from './util.js';
|
@@ -21,13 +20,13 @@ import { boundAlpha, convertToPercentage } from './util.js';
|
|
21
20
|
* ```
|
22
21
|
*/
|
23
22
|
export function inputToRGB(color) {
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
23
|
+
let rgb = { r: 0, g: 0, b: 0 };
|
24
|
+
let a = 1;
|
25
|
+
let s = null;
|
26
|
+
let v = null;
|
27
|
+
let l = null;
|
28
|
+
let ok = false;
|
29
|
+
let format = false;
|
31
30
|
if (typeof color === 'string') {
|
32
31
|
color = stringInputToObject(color);
|
33
32
|
}
|
@@ -57,26 +56,26 @@ export function inputToRGB(color) {
|
|
57
56
|
}
|
58
57
|
a = boundAlpha(a);
|
59
58
|
return {
|
60
|
-
ok
|
59
|
+
ok,
|
61
60
|
format: color.format || format,
|
62
61
|
r: Math.min(255, Math.max(rgb.r, 0)),
|
63
62
|
g: Math.min(255, Math.max(rgb.g, 0)),
|
64
63
|
b: Math.min(255, Math.max(rgb.b, 0)),
|
65
|
-
a
|
64
|
+
a,
|
66
65
|
};
|
67
66
|
}
|
68
67
|
// <http://www.w3.org/TR/css3-values/#integers>
|
69
|
-
|
68
|
+
const CSS_INTEGER = '[-\\+]?\\d+%?';
|
70
69
|
// <http://www.w3.org/TR/css3-values/#number-value>
|
71
|
-
|
70
|
+
const CSS_NUMBER = '[-\\+]?\\d*\\.\\d+%?';
|
72
71
|
// Allow positive/negative integer/number. Don't capture the either/or, just the entire outcome.
|
73
|
-
|
72
|
+
const CSS_UNIT = `(?:${CSS_NUMBER})|(?:${CSS_INTEGER})`;
|
74
73
|
// Actual matching.
|
75
74
|
// Parentheses and commas are optional, but not required.
|
76
75
|
// Whitespace can take the place of commas or opening paren
|
77
|
-
|
78
|
-
|
79
|
-
|
76
|
+
const PERMISSIVE_MATCH3 = `[\\s|\\(]+(${CSS_UNIT})[,|\\s]+(${CSS_UNIT})[,|\\s]+(${CSS_UNIT})\\s*\\)?`;
|
77
|
+
const PERMISSIVE_MATCH4 = `[\\s|\\(]+(${CSS_UNIT})[,|\\s]+(${CSS_UNIT})[,|\\s]+(${CSS_UNIT})[,|\\s]+(${CSS_UNIT})\\s*\\)?`;
|
78
|
+
const matchers = {
|
80
79
|
CSS_UNIT: new RegExp(CSS_UNIT),
|
81
80
|
rgb: new RegExp('rgb' + PERMISSIVE_MATCH3),
|
82
81
|
rgba: new RegExp('rgba' + PERMISSIVE_MATCH4),
|
@@ -98,7 +97,7 @@ export function stringInputToObject(color) {
|
|
98
97
|
if (color.length === 0) {
|
99
98
|
return false;
|
100
99
|
}
|
101
|
-
|
100
|
+
let named = false;
|
102
101
|
if (names[color]) {
|
103
102
|
color = names[color];
|
104
103
|
named = true;
|
@@ -110,7 +109,7 @@ export function stringInputToObject(color) {
|
|
110
109
|
// Keep most of the number bounding out of this function - don't worry about [0,1] or [0,100] or [0,360]
|
111
110
|
// Just return an object and let the conversion functions handle that.
|
112
111
|
// This way the result will be the same whether the tinycolor is initialized with string or object.
|
113
|
-
|
112
|
+
let match = matchers.rgb.exec(color);
|
114
113
|
if (match) {
|
115
114
|
return { r: match[1], g: match[2], b: match[3] };
|
116
115
|
}
|
@@ -5,7 +5,7 @@ import { convertToPercentage } from './util.js';
|
|
5
5
|
* String input requires "1.0" as input, so 1 will be treated as 1
|
6
6
|
*/
|
7
7
|
export function fromRatio(ratio, opts) {
|
8
|
-
|
8
|
+
const newColor = {
|
9
9
|
r: convertToPercentage(ratio.r),
|
10
10
|
g: convertToPercentage(ratio.g),
|
11
11
|
b: convertToPercentage(ratio.b),
|