@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
package/dist/module/random.js
CHANGED
@@ -1,14 +1,12 @@
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-redundant-type-constituents */
|
2
1
|
// randomColor by David Merfield under the CC0 license
|
3
2
|
// https://github.com/davidmerfield/randomColor/
|
4
3
|
import { TinyColor } from './index.js';
|
5
|
-
export function random(options) {
|
6
|
-
if (options === void 0) { options = {}; }
|
4
|
+
export function random(options = {}) {
|
7
5
|
// Check if we need to generate multiple colors
|
8
6
|
if (options.count !== undefined &&
|
9
7
|
options.count !== null) {
|
10
|
-
|
11
|
-
|
8
|
+
const totalColors = options.count;
|
9
|
+
const colors = [];
|
12
10
|
options.count = undefined;
|
13
11
|
while (totalColors > colors.length) {
|
14
12
|
// Since we're generating multiple colors,
|
@@ -24,12 +22,12 @@ export function random(options) {
|
|
24
22
|
return colors;
|
25
23
|
}
|
26
24
|
// First we pick a hue (H)
|
27
|
-
|
25
|
+
const h = pickHue(options.hue, options.seed);
|
28
26
|
// Then use H to determine saturation (S)
|
29
|
-
|
27
|
+
const s = pickSaturation(h, options);
|
30
28
|
// Then use S and H to determine brightness (B).
|
31
|
-
|
32
|
-
|
29
|
+
const v = pickBrightness(h, s, options);
|
30
|
+
const res = { h, s, v };
|
33
31
|
if (options.alpha !== undefined) {
|
34
32
|
res.a = options.alpha;
|
35
33
|
}
|
@@ -37,8 +35,8 @@ export function random(options) {
|
|
37
35
|
return new TinyColor(res);
|
38
36
|
}
|
39
37
|
function pickHue(hue, seed) {
|
40
|
-
|
41
|
-
|
38
|
+
const hueRange = getHueRange(hue);
|
39
|
+
let res = randomWithin(hueRange, seed);
|
42
40
|
// Instead of storing red as two seperate ranges,
|
43
41
|
// we group them, using negative numbers
|
44
42
|
if (res < 0) {
|
@@ -53,9 +51,9 @@ function pickSaturation(hue, options) {
|
|
53
51
|
if (options.luminosity === 'random') {
|
54
52
|
return randomWithin([0, 100], options.seed);
|
55
53
|
}
|
56
|
-
|
57
|
-
|
58
|
-
|
54
|
+
const { saturationRange } = getColorInfo(hue);
|
55
|
+
let sMin = saturationRange[0];
|
56
|
+
let sMax = saturationRange[1];
|
59
57
|
switch (options.luminosity) {
|
60
58
|
case 'bright':
|
61
59
|
sMin = 55;
|
@@ -72,8 +70,8 @@ function pickSaturation(hue, options) {
|
|
72
70
|
return randomWithin([sMin, sMax], options.seed);
|
73
71
|
}
|
74
72
|
function pickBrightness(H, S, options) {
|
75
|
-
|
76
|
-
|
73
|
+
let bMin = getMinimumBrightness(H, S);
|
74
|
+
let bMax = 100;
|
77
75
|
switch (options.luminosity) {
|
78
76
|
case 'dark':
|
79
77
|
bMax = bMin + 20;
|
@@ -91,36 +89,36 @@ function pickBrightness(H, S, options) {
|
|
91
89
|
return randomWithin([bMin, bMax], options.seed);
|
92
90
|
}
|
93
91
|
function getMinimumBrightness(H, S) {
|
94
|
-
|
95
|
-
for (
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
92
|
+
const { lowerBounds } = getColorInfo(H);
|
93
|
+
for (let i = 0; i < lowerBounds.length - 1; i++) {
|
94
|
+
const s1 = lowerBounds[i][0];
|
95
|
+
const v1 = lowerBounds[i][1];
|
96
|
+
const s2 = lowerBounds[i + 1][0];
|
97
|
+
const v2 = lowerBounds[i + 1][1];
|
100
98
|
if (S >= s1 && S <= s2) {
|
101
|
-
|
102
|
-
|
99
|
+
const m = (v2 - v1) / (s2 - s1);
|
100
|
+
const b = v1 - m * s1;
|
103
101
|
return m * S + b;
|
104
102
|
}
|
105
103
|
}
|
106
104
|
return 0;
|
107
105
|
}
|
108
106
|
function getHueRange(colorInput) {
|
109
|
-
|
107
|
+
const num = parseInt(colorInput, 10);
|
110
108
|
if (!Number.isNaN(num) && num < 360 && num > 0) {
|
111
109
|
return [num, num];
|
112
110
|
}
|
113
111
|
if (typeof colorInput === 'string') {
|
114
|
-
|
112
|
+
const namedColor = bounds.find(n => n.name === colorInput);
|
115
113
|
if (namedColor) {
|
116
|
-
|
114
|
+
const color = defineColor(namedColor);
|
117
115
|
if (color.hueRange) {
|
118
116
|
return color.hueRange;
|
119
117
|
}
|
120
118
|
}
|
121
|
-
|
119
|
+
const parsed = new TinyColor(colorInput);
|
122
120
|
if (parsed.isValid) {
|
123
|
-
|
121
|
+
const hue = parsed.toHsv().h;
|
124
122
|
return [hue, hue];
|
125
123
|
}
|
126
124
|
}
|
@@ -131,9 +129,8 @@ function getColorInfo(hue) {
|
|
131
129
|
if (hue >= 334 && hue <= 360) {
|
132
130
|
hue -= 360;
|
133
131
|
}
|
134
|
-
for (
|
135
|
-
|
136
|
-
var color = defineColor(bound);
|
132
|
+
for (const bound of bounds) {
|
133
|
+
const color = defineColor(bound);
|
137
134
|
if (color.hueRange && hue >= color.hueRange[0] && hue <= color.hueRange[1]) {
|
138
135
|
return color;
|
139
136
|
}
|
@@ -145,17 +142,17 @@ function randomWithin(range, seed) {
|
|
145
142
|
return Math.floor(range[0] + Math.random() * (range[1] + 1 - range[0]));
|
146
143
|
}
|
147
144
|
// Seeded random algorithm from http://indiegamr.com/generate-repeatable-random-numbers-in-js/
|
148
|
-
|
149
|
-
|
145
|
+
const max = range[1] || 1;
|
146
|
+
const min = range[0] || 0;
|
150
147
|
seed = (seed * 9301 + 49297) % 233280;
|
151
|
-
|
148
|
+
const rnd = seed / 233280.0;
|
152
149
|
return Math.floor(min + rnd * (max - min));
|
153
150
|
}
|
154
151
|
function defineColor(bound) {
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
152
|
+
const sMin = bound.lowerBounds[0][0];
|
153
|
+
const sMax = bound.lowerBounds[bound.lowerBounds.length - 1][0];
|
154
|
+
const bMin = bound.lowerBounds[bound.lowerBounds.length - 1][1];
|
155
|
+
const bMax = bound.lowerBounds[0][1];
|
159
156
|
return {
|
160
157
|
name: bound.name,
|
161
158
|
hueRange: bound.hueRange,
|
@@ -167,7 +164,7 @@ function defineColor(bound) {
|
|
167
164
|
/**
|
168
165
|
* @hidden
|
169
166
|
*/
|
170
|
-
export
|
167
|
+
export const bounds = [
|
171
168
|
{
|
172
169
|
name: 'monochrome',
|
173
170
|
hueRange: null,
|
@@ -8,8 +8,8 @@ import { TinyColor } from './index.js';
|
|
8
8
|
* Analyze the 2 colors and returns the color contrast defined by (WCAG Version 2)
|
9
9
|
*/
|
10
10
|
export function readability(color1, color2) {
|
11
|
-
|
12
|
-
|
11
|
+
const c1 = new TinyColor(color1);
|
12
|
+
const c2 = new TinyColor(color2);
|
13
13
|
return ((Math.max(c1.getLuminance(), c2.getLuminance()) + 0.05) /
|
14
14
|
(Math.min(c1.getLuminance(), c2.getLuminance()) + 0.05));
|
15
15
|
}
|
@@ -26,11 +26,9 @@ export function readability(color1, color2) {
|
|
26
26
|
* new TinyColor().isReadable('#000', '#111', { level: 'AA', size: 'large' }) => false
|
27
27
|
* ```
|
28
28
|
*/
|
29
|
-
export function isReadable(color1, color2, wcag2) {
|
30
|
-
|
31
|
-
|
32
|
-
var readabilityLevel = readability(color1, color2);
|
33
|
-
switch (((_a = wcag2.level) !== null && _a !== void 0 ? _a : 'AA') + ((_b = wcag2.size) !== null && _b !== void 0 ? _b : 'small')) {
|
29
|
+
export function isReadable(color1, color2, wcag2 = { level: 'AA', size: 'small' }) {
|
30
|
+
const readabilityLevel = readability(color1, color2);
|
31
|
+
switch ((wcag2.level ?? 'AA') + (wcag2.size ?? 'small')) {
|
34
32
|
case 'AAsmall':
|
35
33
|
case 'AAAlarge':
|
36
34
|
return readabilityLevel >= 4.5;
|
@@ -59,20 +57,18 @@ export function isReadable(color1, color2, wcag2) {
|
|
59
57
|
* new TinyColor().mostReadable('#a8015a', ["#faf3f3"], { includeFallbackColors:true, level: 'AAA', size: 'small' }).toHexString(); // "#ffffff"
|
60
58
|
* ```
|
61
59
|
*/
|
62
|
-
export function mostReadable(baseColor, colorList, args) {
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
var color = colorList_1[_i];
|
69
|
-
var score = readability(baseColor, color);
|
60
|
+
export function mostReadable(baseColor, colorList, args = { includeFallbackColors: false, level: 'AA', size: 'small' }) {
|
61
|
+
let bestColor = null;
|
62
|
+
let bestScore = 0;
|
63
|
+
const { includeFallbackColors, level, size } = args;
|
64
|
+
for (const color of colorList) {
|
65
|
+
const score = readability(baseColor, color);
|
70
66
|
if (score > bestScore) {
|
71
67
|
bestScore = score;
|
72
68
|
bestColor = new TinyColor(color);
|
73
69
|
}
|
74
70
|
}
|
75
|
-
if (isReadable(baseColor, bestColor, { level
|
71
|
+
if (isReadable(baseColor, bestColor, { level, size }) || !includeFallbackColors) {
|
76
72
|
return bestColor;
|
77
73
|
}
|
78
74
|
args.includeFallbackColors = false;
|
@@ -4,13 +4,13 @@ import { TinyColor } from './index.js';
|
|
4
4
|
* Returns the color represented as a Microsoft filter for use in old versions of IE.
|
5
5
|
*/
|
6
6
|
export function toMsFilter(firstColor, secondColor) {
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
const color = new TinyColor(firstColor);
|
8
|
+
const hex8String = '#' + rgbaToArgbHex(color.r, color.g, color.b, color.a);
|
9
|
+
let secondHex8String = hex8String;
|
10
|
+
const gradientType = color.gradientType ? 'GradientType = 1, ' : '';
|
11
11
|
if (secondColor) {
|
12
|
-
|
12
|
+
const s = new TinyColor(secondColor);
|
13
13
|
secondHex8String = '#' + rgbaToArgbHex(s.r, s.g, s.b, s.a);
|
14
14
|
}
|
15
|
-
return
|
15
|
+
return `progid:DXImageTransform.Microsoft.gradient(${gradientType}startColorstr=${hex8String},endColorstr=${secondHex8String})`;
|
16
16
|
}
|
package/dist/module/umd_api.js
CHANGED
@@ -1,20 +1,21 @@
|
|
1
1
|
import { names } from './css-color-names.js';
|
2
2
|
import { inputToRGB, isValidCSSUnit, stringInputToObject } from './format-input.js';
|
3
3
|
import { fromRatio, legacyRandom } from './from-ratio.js';
|
4
|
-
import { TinyColor
|
4
|
+
import { TinyColor } from './index.js';
|
5
5
|
import { random } from './random.js';
|
6
6
|
import { mostReadable, readability } from './readability.js';
|
7
7
|
import { toMsFilter } from './to-ms-filter.js';
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
8
|
+
const tinycolorumd = {
|
9
|
+
TinyColor,
|
10
|
+
readability,
|
11
|
+
mostReadable,
|
12
|
+
random,
|
13
|
+
names,
|
14
|
+
fromRatio,
|
15
|
+
legacyRandom,
|
16
|
+
toMsFilter,
|
17
|
+
inputToRGB,
|
18
|
+
stringInputToObject,
|
19
|
+
isValidCSSUnit,
|
20
|
+
};
|
20
21
|
export default tinycolorumd;
|
package/dist/module/util.js
CHANGED
@@ -6,7 +6,7 @@ export function bound01(n, max) {
|
|
6
6
|
if (isOnePointZero(n)) {
|
7
7
|
n = '100%';
|
8
8
|
}
|
9
|
-
|
9
|
+
const isPercent = isPercentage(n);
|
10
10
|
n = max === 360 ? n : Math.min(max, Math.max(0, parseFloat(n)));
|
11
11
|
// Automatically convert percentage into number
|
12
12
|
if (isPercent) {
|
@@ -68,8 +68,8 @@ export function boundAlpha(a) {
|
|
68
68
|
* @hidden
|
69
69
|
*/
|
70
70
|
export function convertToPercentage(n) {
|
71
|
-
if (n <= 1) {
|
72
|
-
return
|
71
|
+
if (Number(n) <= 1) {
|
72
|
+
return `${Number(n) * 100}%`;
|
73
73
|
}
|
74
74
|
return n;
|
75
75
|
}
|
package/dist/public_api.d.ts
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
import { tinycolor } from './index.js';
|
2
1
|
export * from './index.js';
|
3
2
|
export * from './css-color-names.js';
|
4
3
|
export * from './readability.js';
|
@@ -8,4 +7,3 @@ export * from './format-input.js';
|
|
8
7
|
export * from './random.js';
|
9
8
|
export * from './interfaces.js';
|
10
9
|
export * from './conversion.js';
|
11
|
-
export default tinycolor;
|
package/dist/public_api.js
CHANGED
@@ -14,7 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
15
|
};
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
-
var index_js_1 = require("./index.js");
|
18
17
|
__exportStar(require("./index.js"), exports);
|
19
18
|
__exportStar(require("./css-color-names.js"), exports);
|
20
19
|
__exportStar(require("./readability.js"), exports);
|
@@ -24,5 +23,3 @@ __exportStar(require("./format-input.js"), exports);
|
|
24
23
|
__exportStar(require("./random.js"), exports);
|
25
24
|
__exportStar(require("./interfaces.js"), exports);
|
26
25
|
__exportStar(require("./conversion.js"), exports);
|
27
|
-
// kept for backwards compatability with v1
|
28
|
-
exports.default = index_js_1.tinycolor;
|
package/dist/random.js
CHANGED
@@ -1,17 +1,15 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.bounds = exports.random = void 0;
|
4
|
-
/* eslint-disable @typescript-eslint/no-redundant-type-constituents */
|
5
4
|
// randomColor by David Merfield under the CC0 license
|
6
5
|
// https://github.com/davidmerfield/randomColor/
|
7
|
-
|
8
|
-
function random(options) {
|
9
|
-
if (options === void 0) { options = {}; }
|
6
|
+
const index_js_1 = require("./index.js");
|
7
|
+
function random(options = {}) {
|
10
8
|
// Check if we need to generate multiple colors
|
11
9
|
if (options.count !== undefined &&
|
12
10
|
options.count !== null) {
|
13
|
-
|
14
|
-
|
11
|
+
const totalColors = options.count;
|
12
|
+
const colors = [];
|
15
13
|
options.count = undefined;
|
16
14
|
while (totalColors > colors.length) {
|
17
15
|
// Since we're generating multiple colors,
|
@@ -27,12 +25,12 @@ function random(options) {
|
|
27
25
|
return colors;
|
28
26
|
}
|
29
27
|
// First we pick a hue (H)
|
30
|
-
|
28
|
+
const h = pickHue(options.hue, options.seed);
|
31
29
|
// Then use H to determine saturation (S)
|
32
|
-
|
30
|
+
const s = pickSaturation(h, options);
|
33
31
|
// Then use S and H to determine brightness (B).
|
34
|
-
|
35
|
-
|
32
|
+
const v = pickBrightness(h, s, options);
|
33
|
+
const res = { h, s, v };
|
36
34
|
if (options.alpha !== undefined) {
|
37
35
|
res.a = options.alpha;
|
38
36
|
}
|
@@ -41,8 +39,8 @@ function random(options) {
|
|
41
39
|
}
|
42
40
|
exports.random = random;
|
43
41
|
function pickHue(hue, seed) {
|
44
|
-
|
45
|
-
|
42
|
+
const hueRange = getHueRange(hue);
|
43
|
+
let res = randomWithin(hueRange, seed);
|
46
44
|
// Instead of storing red as two seperate ranges,
|
47
45
|
// we group them, using negative numbers
|
48
46
|
if (res < 0) {
|
@@ -57,9 +55,9 @@ function pickSaturation(hue, options) {
|
|
57
55
|
if (options.luminosity === 'random') {
|
58
56
|
return randomWithin([0, 100], options.seed);
|
59
57
|
}
|
60
|
-
|
61
|
-
|
62
|
-
|
58
|
+
const { saturationRange } = getColorInfo(hue);
|
59
|
+
let sMin = saturationRange[0];
|
60
|
+
let sMax = saturationRange[1];
|
63
61
|
switch (options.luminosity) {
|
64
62
|
case 'bright':
|
65
63
|
sMin = 55;
|
@@ -76,8 +74,8 @@ function pickSaturation(hue, options) {
|
|
76
74
|
return randomWithin([sMin, sMax], options.seed);
|
77
75
|
}
|
78
76
|
function pickBrightness(H, S, options) {
|
79
|
-
|
80
|
-
|
77
|
+
let bMin = getMinimumBrightness(H, S);
|
78
|
+
let bMax = 100;
|
81
79
|
switch (options.luminosity) {
|
82
80
|
case 'dark':
|
83
81
|
bMax = bMin + 20;
|
@@ -95,36 +93,36 @@ function pickBrightness(H, S, options) {
|
|
95
93
|
return randomWithin([bMin, bMax], options.seed);
|
96
94
|
}
|
97
95
|
function getMinimumBrightness(H, S) {
|
98
|
-
|
99
|
-
for (
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
96
|
+
const { lowerBounds } = getColorInfo(H);
|
97
|
+
for (let i = 0; i < lowerBounds.length - 1; i++) {
|
98
|
+
const s1 = lowerBounds[i][0];
|
99
|
+
const v1 = lowerBounds[i][1];
|
100
|
+
const s2 = lowerBounds[i + 1][0];
|
101
|
+
const v2 = lowerBounds[i + 1][1];
|
104
102
|
if (S >= s1 && S <= s2) {
|
105
|
-
|
106
|
-
|
103
|
+
const m = (v2 - v1) / (s2 - s1);
|
104
|
+
const b = v1 - m * s1;
|
107
105
|
return m * S + b;
|
108
106
|
}
|
109
107
|
}
|
110
108
|
return 0;
|
111
109
|
}
|
112
110
|
function getHueRange(colorInput) {
|
113
|
-
|
111
|
+
const num = parseInt(colorInput, 10);
|
114
112
|
if (!Number.isNaN(num) && num < 360 && num > 0) {
|
115
113
|
return [num, num];
|
116
114
|
}
|
117
115
|
if (typeof colorInput === 'string') {
|
118
|
-
|
116
|
+
const namedColor = exports.bounds.find(n => n.name === colorInput);
|
119
117
|
if (namedColor) {
|
120
|
-
|
118
|
+
const color = defineColor(namedColor);
|
121
119
|
if (color.hueRange) {
|
122
120
|
return color.hueRange;
|
123
121
|
}
|
124
122
|
}
|
125
|
-
|
123
|
+
const parsed = new index_js_1.TinyColor(colorInput);
|
126
124
|
if (parsed.isValid) {
|
127
|
-
|
125
|
+
const hue = parsed.toHsv().h;
|
128
126
|
return [hue, hue];
|
129
127
|
}
|
130
128
|
}
|
@@ -135,9 +133,8 @@ function getColorInfo(hue) {
|
|
135
133
|
if (hue >= 334 && hue <= 360) {
|
136
134
|
hue -= 360;
|
137
135
|
}
|
138
|
-
for (
|
139
|
-
|
140
|
-
var color = defineColor(bound);
|
136
|
+
for (const bound of exports.bounds) {
|
137
|
+
const color = defineColor(bound);
|
141
138
|
if (color.hueRange && hue >= color.hueRange[0] && hue <= color.hueRange[1]) {
|
142
139
|
return color;
|
143
140
|
}
|
@@ -149,17 +146,17 @@ function randomWithin(range, seed) {
|
|
149
146
|
return Math.floor(range[0] + Math.random() * (range[1] + 1 - range[0]));
|
150
147
|
}
|
151
148
|
// Seeded random algorithm from http://indiegamr.com/generate-repeatable-random-numbers-in-js/
|
152
|
-
|
153
|
-
|
149
|
+
const max = range[1] || 1;
|
150
|
+
const min = range[0] || 0;
|
154
151
|
seed = (seed * 9301 + 49297) % 233280;
|
155
|
-
|
152
|
+
const rnd = seed / 233280.0;
|
156
153
|
return Math.floor(min + rnd * (max - min));
|
157
154
|
}
|
158
155
|
function defineColor(bound) {
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
156
|
+
const sMin = bound.lowerBounds[0][0];
|
157
|
+
const sMax = bound.lowerBounds[bound.lowerBounds.length - 1][0];
|
158
|
+
const bMin = bound.lowerBounds[bound.lowerBounds.length - 1][1];
|
159
|
+
const bMax = bound.lowerBounds[0][1];
|
163
160
|
return {
|
164
161
|
name: bound.name,
|
165
162
|
hueRange: bound.hueRange,
|
package/dist/readability.js
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.mostReadable = exports.isReadable = exports.readability = void 0;
|
4
|
-
|
4
|
+
const index_js_1 = require("./index.js");
|
5
5
|
// Readability Functions
|
6
6
|
// ---------------------
|
7
7
|
// <http://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef (WCAG Version 2)
|
@@ -11,8 +11,8 @@ var index_js_1 = require("./index.js");
|
|
11
11
|
* Analyze the 2 colors and returns the color contrast defined by (WCAG Version 2)
|
12
12
|
*/
|
13
13
|
function readability(color1, color2) {
|
14
|
-
|
15
|
-
|
14
|
+
const c1 = new index_js_1.TinyColor(color1);
|
15
|
+
const c2 = new index_js_1.TinyColor(color2);
|
16
16
|
return ((Math.max(c1.getLuminance(), c2.getLuminance()) + 0.05) /
|
17
17
|
(Math.min(c1.getLuminance(), c2.getLuminance()) + 0.05));
|
18
18
|
}
|
@@ -30,11 +30,9 @@ exports.readability = readability;
|
|
30
30
|
* new TinyColor().isReadable('#000', '#111', { level: 'AA', size: 'large' }) => false
|
31
31
|
* ```
|
32
32
|
*/
|
33
|
-
function isReadable(color1, color2, wcag2) {
|
34
|
-
|
35
|
-
|
36
|
-
var readabilityLevel = readability(color1, color2);
|
37
|
-
switch (((_a = wcag2.level) !== null && _a !== void 0 ? _a : 'AA') + ((_b = wcag2.size) !== null && _b !== void 0 ? _b : 'small')) {
|
33
|
+
function isReadable(color1, color2, wcag2 = { level: 'AA', size: 'small' }) {
|
34
|
+
const readabilityLevel = readability(color1, color2);
|
35
|
+
switch ((wcag2.level ?? 'AA') + (wcag2.size ?? 'small')) {
|
38
36
|
case 'AAsmall':
|
39
37
|
case 'AAAlarge':
|
40
38
|
return readabilityLevel >= 4.5;
|
@@ -64,20 +62,18 @@ exports.isReadable = isReadable;
|
|
64
62
|
* new TinyColor().mostReadable('#a8015a', ["#faf3f3"], { includeFallbackColors:true, level: 'AAA', size: 'small' }).toHexString(); // "#ffffff"
|
65
63
|
* ```
|
66
64
|
*/
|
67
|
-
function mostReadable(baseColor, colorList, args) {
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
var color = colorList_1[_i];
|
74
|
-
var score = readability(baseColor, color);
|
65
|
+
function mostReadable(baseColor, colorList, args = { includeFallbackColors: false, level: 'AA', size: 'small' }) {
|
66
|
+
let bestColor = null;
|
67
|
+
let bestScore = 0;
|
68
|
+
const { includeFallbackColors, level, size } = args;
|
69
|
+
for (const color of colorList) {
|
70
|
+
const score = readability(baseColor, color);
|
75
71
|
if (score > bestScore) {
|
76
72
|
bestScore = score;
|
77
73
|
bestColor = new index_js_1.TinyColor(color);
|
78
74
|
}
|
79
75
|
}
|
80
|
-
if (isReadable(baseColor, bestColor, { level
|
76
|
+
if (isReadable(baseColor, bestColor, { level, size }) || !includeFallbackColors) {
|
81
77
|
return bestColor;
|
82
78
|
}
|
83
79
|
args.includeFallbackColors = false;
|
package/dist/to-ms-filter.js
CHANGED
@@ -1,20 +1,20 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.toMsFilter = void 0;
|
4
|
-
|
5
|
-
|
4
|
+
const conversion_js_1 = require("./conversion.js");
|
5
|
+
const index_js_1 = require("./index.js");
|
6
6
|
/**
|
7
7
|
* Returns the color represented as a Microsoft filter for use in old versions of IE.
|
8
8
|
*/
|
9
9
|
function toMsFilter(firstColor, secondColor) {
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
10
|
+
const color = new index_js_1.TinyColor(firstColor);
|
11
|
+
const hex8String = '#' + (0, conversion_js_1.rgbaToArgbHex)(color.r, color.g, color.b, color.a);
|
12
|
+
let secondHex8String = hex8String;
|
13
|
+
const gradientType = color.gradientType ? 'GradientType = 1, ' : '';
|
14
14
|
if (secondColor) {
|
15
|
-
|
15
|
+
const s = new index_js_1.TinyColor(secondColor);
|
16
16
|
secondHex8String = '#' + (0, conversion_js_1.rgbaToArgbHex)(s.r, s.g, s.b, s.a);
|
17
17
|
}
|
18
|
-
return
|
18
|
+
return `progid:DXImageTransform.Microsoft.gradient(${gradientType}startColorstr=${hex8String},endColorstr=${secondHex8String})`;
|
19
19
|
}
|
20
20
|
exports.toMsFilter = toMsFilter;
|
package/dist/umd_api.d.ts
CHANGED
@@ -6,7 +6,6 @@ import { random } from './random.js';
|
|
6
6
|
import { mostReadable, readability } from './readability.js';
|
7
7
|
import { toMsFilter } from './to-ms-filter.js';
|
8
8
|
export interface TinyColorUMD {
|
9
|
-
(): TinyColor;
|
10
9
|
TinyColor: typeof TinyColor;
|
11
10
|
readability: typeof readability;
|
12
11
|
random: typeof random;
|
package/dist/umd_api.js
CHANGED
@@ -1,22 +1,23 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
3
|
+
const css_color_names_js_1 = require("./css-color-names.js");
|
4
|
+
const format_input_js_1 = require("./format-input.js");
|
5
|
+
const from_ratio_js_1 = require("./from-ratio.js");
|
6
|
+
const index_js_1 = require("./index.js");
|
7
|
+
const random_js_1 = require("./random.js");
|
8
|
+
const readability_js_1 = require("./readability.js");
|
9
|
+
const to_ms_filter_js_1 = require("./to-ms-filter.js");
|
10
|
+
const tinycolorumd = {
|
11
|
+
TinyColor: index_js_1.TinyColor,
|
12
|
+
readability: readability_js_1.readability,
|
13
|
+
mostReadable: readability_js_1.mostReadable,
|
14
|
+
random: random_js_1.random,
|
15
|
+
names: css_color_names_js_1.names,
|
16
|
+
fromRatio: from_ratio_js_1.fromRatio,
|
17
|
+
legacyRandom: from_ratio_js_1.legacyRandom,
|
18
|
+
toMsFilter: to_ms_filter_js_1.toMsFilter,
|
19
|
+
inputToRGB: format_input_js_1.inputToRGB,
|
20
|
+
stringInputToObject: format_input_js_1.stringInputToObject,
|
21
|
+
isValidCSSUnit: format_input_js_1.isValidCSSUnit,
|
22
|
+
};
|
22
23
|
exports.default = tinycolorumd;
|
package/dist/util.js
CHANGED
@@ -9,7 +9,7 @@ function bound01(n, max) {
|
|
9
9
|
if (isOnePointZero(n)) {
|
10
10
|
n = '100%';
|
11
11
|
}
|
12
|
-
|
12
|
+
const isPercent = isPercentage(n);
|
13
13
|
n = max === 360 ? n : Math.min(max, Math.max(0, parseFloat(n)));
|
14
14
|
// Automatically convert percentage into number
|
15
15
|
if (isPercent) {
|
@@ -76,8 +76,8 @@ exports.boundAlpha = boundAlpha;
|
|
76
76
|
* @hidden
|
77
77
|
*/
|
78
78
|
function convertToPercentage(n) {
|
79
|
-
if (n <= 1) {
|
80
|
-
return
|
79
|
+
if (Number(n) <= 1) {
|
80
|
+
return `${Number(n) * 100}%`;
|
81
81
|
}
|
82
82
|
return n;
|
83
83
|
}
|