Skip to content

Commit fa3aa2c

Browse files
authored
ESM cleanup - round 1 (#247)
* update header * doc updates
1 parent 1567c64 commit fa3aa2c

File tree

6 files changed

+244
-242
lines changed

6 files changed

+244
-242
lines changed

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,31 @@ var tinycolor = require("tinycolor2");
1919
var color = tinycolor("red");
2020
```
2121

22+
Or in a module like so:
23+
24+
```js
25+
import tinycolor from "tinycolor2";
26+
var color = tinycolor("red");
27+
```
28+
2229
## Including in a browser
2330

24-
The package can be bundled from npm, or you can use it directly in a script tag by downloading [tinycolor.js](https://raw.githubusercontent.com/bgrins/TinyColor/master/tinycolor.js):
31+
The package can be bundled from npm, but if you prefer to download it locally you have two choices:
32+
33+
### ESM
34+
35+
It can be used as a module by downloading [npm/esm/tinycolor.js](https://github.com/bgrins/TinyColor/blob/master/npm/esm/tinycolor.js) or using https://esm.sh/tinycolor2.
36+
37+
```html
38+
<script type='module'>
39+
import tinycolor from "https://esm.sh/tinycolor2";
40+
var color = tinycolor("red");
41+
</script>
42+
```
43+
44+
### UMD
45+
46+
You can use it directly in a script tag by downloading the UMD file from [npm/cjs/tinycolor.js](https://github.com/bgrins/TinyColor/blob/master/npm/cjs/tinycolor.js):
2547

2648
```html
2749
<script type='text/javascript' src='tinycolor.js'></script>

dist/tinycolor-min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mod.js

Lines changed: 55 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
// TinyColor v1.4.2
21
// https://github.com/bgrins/TinyColor
32
// Brian Grinstead, MIT License
43

5-
var trimLeft = /^\s+/,
6-
trimRight = /\s+$/,
7-
mathRound = Math.round,
8-
mathMin = Math.min,
9-
mathMax = Math.max,
10-
mathRandom = Math.random;
4+
const trimLeft = /^\s+/;
5+
const trimRight = /\s+$/;
116

127
export default function tinycolor(color, opts) {
138
color = color ? color : "";
@@ -28,17 +23,17 @@ export default function tinycolor(color, opts) {
2823
(this._g = rgb.g),
2924
(this._b = rgb.b),
3025
(this._a = rgb.a),
31-
(this._roundA = mathRound(100 * this._a) / 100),
26+
(this._roundA = Math.round(100 * this._a) / 100),
3227
(this._format = opts.format || rgb.format);
3328
this._gradientType = opts.gradientType;
3429

3530
// Don't let the range of [0,255] come back in [0,1].
3631
// Potentially lose a little bit of precision here, but will fix issues where
3732
// .5 gets interpreted as half of the total, instead of half of 1
3833
// If it was supposed to be 128, this was already taken care of by `inputToRgb`
39-
if (this._r < 1) this._r = mathRound(this._r);
40-
if (this._g < 1) this._g = mathRound(this._g);
41-
if (this._b < 1) this._b = mathRound(this._b);
34+
if (this._r < 1) this._r = Math.round(this._r);
35+
if (this._g < 1) this._g = Math.round(this._g);
36+
if (this._b < 1) this._b = Math.round(this._b);
4237

4338
this._ok = rgb.ok;
4439
}
@@ -85,7 +80,7 @@ tinycolor.prototype = {
8580
},
8681
setAlpha: function (value) {
8782
this._a = boundAlpha(value);
88-
this._roundA = mathRound(100 * this._a) / 100;
83+
this._roundA = Math.round(100 * this._a) / 100;
8984
return this;
9085
},
9186
toHsv: function () {
@@ -94,9 +89,9 @@ tinycolor.prototype = {
9489
},
9590
toHsvString: function () {
9691
var hsv = rgbToHsv(this._r, this._g, this._b);
97-
var h = mathRound(hsv.h * 360),
98-
s = mathRound(hsv.s * 100),
99-
v = mathRound(hsv.v * 100);
92+
var h = Math.round(hsv.h * 360),
93+
s = Math.round(hsv.s * 100),
94+
v = Math.round(hsv.v * 100);
10095
return this._a == 1
10196
? "hsv(" + h + ", " + s + "%, " + v + "%)"
10297
: "hsva(" + h + ", " + s + "%, " + v + "%, " + this._roundA + ")";
@@ -107,9 +102,9 @@ tinycolor.prototype = {
107102
},
108103
toHslString: function () {
109104
var hsl = rgbToHsl(this._r, this._g, this._b);
110-
var h = mathRound(hsl.h * 360),
111-
s = mathRound(hsl.s * 100),
112-
l = mathRound(hsl.l * 100);
105+
var h = Math.round(hsl.h * 360),
106+
s = Math.round(hsl.s * 100),
107+
l = Math.round(hsl.l * 100);
113108
return this._a == 1
114109
? "hsl(" + h + ", " + s + "%, " + l + "%)"
115110
: "hsla(" + h + ", " + s + "%, " + l + "%, " + this._roundA + ")";
@@ -128,54 +123,54 @@ tinycolor.prototype = {
128123
},
129124
toRgb: function () {
130125
return {
131-
r: mathRound(this._r),
132-
g: mathRound(this._g),
133-
b: mathRound(this._b),
126+
r: Math.round(this._r),
127+
g: Math.round(this._g),
128+
b: Math.round(this._b),
134129
a: this._a,
135130
};
136131
},
137132
toRgbString: function () {
138133
return this._a == 1
139134
? "rgb(" +
140-
mathRound(this._r) +
135+
Math.round(this._r) +
141136
", " +
142-
mathRound(this._g) +
137+
Math.round(this._g) +
143138
", " +
144-
mathRound(this._b) +
139+
Math.round(this._b) +
145140
")"
146141
: "rgba(" +
147-
mathRound(this._r) +
142+
Math.round(this._r) +
148143
", " +
149-
mathRound(this._g) +
144+
Math.round(this._g) +
150145
", " +
151-
mathRound(this._b) +
146+
Math.round(this._b) +
152147
", " +
153148
this._roundA +
154149
")";
155150
},
156151
toPercentageRgb: function () {
157152
return {
158-
r: mathRound(bound01(this._r, 255) * 100) + "%",
159-
g: mathRound(bound01(this._g, 255) * 100) + "%",
160-
b: mathRound(bound01(this._b, 255) * 100) + "%",
153+
r: Math.round(bound01(this._r, 255) * 100) + "%",
154+
g: Math.round(bound01(this._g, 255) * 100) + "%",
155+
b: Math.round(bound01(this._b, 255) * 100) + "%",
161156
a: this._a,
162157
};
163158
},
164159
toPercentageRgbString: function () {
165160
return this._a == 1
166161
? "rgb(" +
167-
mathRound(bound01(this._r, 255) * 100) +
162+
Math.round(bound01(this._r, 255) * 100) +
168163
"%, " +
169-
mathRound(bound01(this._g, 255) * 100) +
164+
Math.round(bound01(this._g, 255) * 100) +
170165
"%, " +
171-
mathRound(bound01(this._b, 255) * 100) +
166+
Math.round(bound01(this._b, 255) * 100) +
172167
"%)"
173168
: "rgba(" +
174-
mathRound(bound01(this._r, 255) * 100) +
169+
Math.round(bound01(this._r, 255) * 100) +
175170
"%, " +
176-
mathRound(bound01(this._g, 255) * 100) +
171+
Math.round(bound01(this._g, 255) * 100) +
177172
"%, " +
178-
mathRound(bound01(this._b, 255) * 100) +
173+
Math.round(bound01(this._b, 255) * 100) +
179174
"%, " +
180175
this._roundA +
181176
")";
@@ -411,9 +406,9 @@ function inputToRGB(color) {
411406
return {
412407
ok: ok,
413408
format: color.format || format,
414-
r: mathMin(255, mathMax(rgb.r, 0)),
415-
g: mathMin(255, mathMax(rgb.g, 0)),
416-
b: mathMin(255, mathMax(rgb.b, 0)),
409+
r: Math.min(255, Math.max(rgb.r, 0)),
410+
g: Math.min(255, Math.max(rgb.g, 0)),
411+
b: Math.min(255, Math.max(rgb.b, 0)),
417412
a: a,
418413
};
419414
}
@@ -446,8 +441,8 @@ function rgbToHsl(r, g, b) {
446441
g = bound01(g, 255);
447442
b = bound01(b, 255);
448443

449-
var max = mathMax(r, g, b),
450-
min = mathMin(r, g, b);
444+
var max = Math.max(r, g, b),
445+
min = Math.min(r, g, b);
451446
var h,
452447
s,
453448
l = (max + min) / 2;
@@ -517,8 +512,8 @@ function rgbToHsv(r, g, b) {
517512
g = bound01(g, 255);
518513
b = bound01(b, 255);
519514

520-
var max = mathMax(r, g, b),
521-
min = mathMin(r, g, b);
515+
var max = Math.max(r, g, b),
516+
min = Math.min(r, g, b);
522517
var h,
523518
s,
524519
v = max;
@@ -573,9 +568,9 @@ function hsvToRgb(h, s, v) {
573568
// Returns a 3 or 6 character hex
574569
function rgbToHex(r, g, b, allow3Char) {
575570
var hex = [
576-
pad2(mathRound(r).toString(16)),
577-
pad2(mathRound(g).toString(16)),
578-
pad2(mathRound(b).toString(16)),
571+
pad2(Math.round(r).toString(16)),
572+
pad2(Math.round(g).toString(16)),
573+
pad2(Math.round(b).toString(16)),
579574
];
580575

581576
// Return a 3 character hex if possible
@@ -597,9 +592,9 @@ function rgbToHex(r, g, b, allow3Char) {
597592
// a in [0, 1]. Returns a 4 or 8 character rgba hex
598593
function rgbaToHex(r, g, b, a, allow4Char) {
599594
var hex = [
600-
pad2(mathRound(r).toString(16)),
601-
pad2(mathRound(g).toString(16)),
602-
pad2(mathRound(b).toString(16)),
595+
pad2(Math.round(r).toString(16)),
596+
pad2(Math.round(g).toString(16)),
597+
pad2(Math.round(b).toString(16)),
603598
pad2(convertDecimalToHex(a)),
604599
];
605600

@@ -625,9 +620,9 @@ function rgbaToHex(r, g, b, a, allow4Char) {
625620
function rgbaToArgbHex(r, g, b, a) {
626621
var hex = [
627622
pad2(convertDecimalToHex(a)),
628-
pad2(mathRound(r).toString(16)),
629-
pad2(mathRound(g).toString(16)),
630-
pad2(mathRound(b).toString(16)),
623+
pad2(Math.round(r).toString(16)),
624+
pad2(Math.round(g).toString(16)),
625+
pad2(Math.round(b).toString(16)),
631626
];
632627

633628
return hex.join("");
@@ -642,9 +637,9 @@ tinycolor.equals = function (color1, color2) {
642637

643638
tinycolor.random = function () {
644639
return tinycolor.fromRatio({
645-
r: mathRandom(),
646-
g: mathRandom(),
647-
b: mathRandom(),
640+
r: Math.random(),
641+
g: Math.random(),
642+
b: Math.random(),
648643
});
649644
};
650645

@@ -684,9 +679,9 @@ function lighten(color, amount) {
684679
function brighten(color, amount) {
685680
amount = amount === 0 ? 0 : amount || 10;
686681
var rgb = tinycolor(color).toRgb();
687-
rgb.r = mathMax(0, mathMin(255, rgb.r - mathRound(255 * -(amount / 100))));
688-
rgb.g = mathMax(0, mathMin(255, rgb.g - mathRound(255 * -(amount / 100))));
689-
rgb.b = mathMax(0, mathMin(255, rgb.b - mathRound(255 * -(amount / 100))));
682+
rgb.r = Math.max(0, Math.min(255, rgb.r - Math.round(255 * -(amount / 100))));
683+
rgb.g = Math.max(0, Math.min(255, rgb.g - Math.round(255 * -(amount / 100))));
684+
rgb.b = Math.max(0, Math.min(255, rgb.b - Math.round(255 * -(amount / 100))));
690685
return tinycolor(rgb);
691686
}
692687

@@ -1078,7 +1073,7 @@ function bound01(n, max) {
10781073
if (isOnePointZero(n)) n = "100%";
10791074

10801075
var processPercent = isPercentage(n);
1081-
n = mathMin(max, mathMax(0, parseFloat(n)));
1076+
n = Math.min(max, Math.max(0, parseFloat(n)));
10821077

10831078
// Automatically convert percentage into number
10841079
if (processPercent) {
@@ -1096,7 +1091,7 @@ function bound01(n, max) {
10961091

10971092
// Force a number between 0 and 1
10981093
function clamp01(val) {
1099-
return mathMin(1, mathMax(0, val));
1094+
return Math.min(1, Math.max(0, val));
11001095
}
11011096

11021097
// Parse a base-16 hex value into a base-10 integer

0 commit comments

Comments
 (0)