Skip to content

Commit d415afc

Browse files
author
Elusive
committed
- Added trim to fromHSLString method.
- Added trim to fromRGBString method. - Added round to alpha hex calculatino in _getHex method. - Added comment to _toHex method in Helpers JS. - Fixed error message in fromHSLString method. - Updated README.
1 parent d742174 commit d415afc

11 files changed

Lines changed: 53 additions & 40 deletions

File tree

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ It is a lightweight (~4kb gzipped) and modern library, and features full support
1010
- [Basic Usage](#basic-usage)
1111
- [Color Creation](#color-creation)
1212
- [Color Formatting](#color-formatting)
13-
- [Color Output](#color-output)
1413
- [Color Attributes](#color-attributes)
1514
- [Color Manipulation](#color-manipulation)
1615
- [Color Schemes](#color-schemes)
@@ -488,4 +487,4 @@ Create a new *Color* by multiplying two colors together by a specified amount.
488487

489488
```javascript
490489
const multiplied = Color.multiply(color1, color2, amount);
491-
```
490+
```

dist/frost-color.js

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* FrostColor v3.0.0
2+
* FrostColor v3.0.1
33
* https://github.com/elusivecodes/FrostColor
44
*/
55
(function(global, factory) {
@@ -719,6 +719,8 @@
719719
* @returns {Color} A new Color object.
720720
*/
721721
fromHSLString(string) {
722+
string = string.trim();
723+
722724
const HSL2Match = string.match(this._hsl2RegExp);
723725

724726
if (HSL2Match) {
@@ -757,7 +759,7 @@
757759
);
758760
}
759761

760-
throw new Error('Invalid HSLA string');
762+
throw new Error('Invalid HSL string');
761763
},
762764

763765
/**
@@ -791,6 +793,8 @@
791793
* @returns {Color} A new Color object.
792794
*/
793795
fromRGBString(string) {
796+
string = string.trim();
797+
794798
const RGB2Match = string.match(this._rgb2RegExp);
795799

796800
if (RGB2Match) {
@@ -838,16 +842,14 @@
838842
* @returns {Color} A new Color object.
839843
*/
840844
fromString(string) {
841-
string = string.toLowerCase();
845+
string = string.toLowerCase().trim();
842846

843847
if (string === 'transparent') {
844848
return new this(0, 0, 0, 0);
845849
}
846850

847851
if (string in this.colors) {
848852
string = this.colors[string];
849-
} else {
850-
string = string.trim();
851853
}
852854

853855
if (string.substring(0, 1) === '#') {
@@ -900,6 +902,11 @@
900902
+ b * amount;
901903
},
902904

905+
/**
906+
* Shorten a hex string (if possible).
907+
* @param {string} hex The hex string.
908+
* @returns {string} The hex string.
909+
*/
903910
_toHex(hex) {
904911
if (hex.length === 9 &&
905912
hex[1] === hex[2] &&
@@ -992,10 +999,10 @@
992999
*/
9931000
mix(color1, color2, amount) {
9941001
return new this(
995-
Color._lerp(color1._r, color2._r, amount),
996-
Color._lerp(color1._g, color2._g, amount),
997-
Color._lerp(color1._b, color2._b, amount),
998-
Color._lerp(color1._a, color2._a, amount)
1002+
this._lerp(color1._r, color2._r, amount),
1003+
this._lerp(color1._g, color2._g, amount),
1004+
this._lerp(color1._b, color2._b, amount),
1005+
this._lerp(color1._a, color2._a, amount)
9991006
);
10001007
},
10011008

@@ -1008,22 +1015,22 @@
10081015
*/
10091016
multiply(color1, color2, amount) {
10101017
return new this(
1011-
Color._lerp(
1018+
this._lerp(
10121019
color1._r,
10131020
color1._r * color2._r / 255,
10141021
amount
10151022
),
1016-
Color._lerp(
1023+
this._lerp(
10171024
color1._g,
10181025
color1._g * color2._g / 255,
10191026
amount
10201027
),
1021-
Color._lerp(
1028+
this._lerp(
10221029
color1._b,
10231030
color1._b * color2._b / 255,
10241031
amount
10251032
),
1026-
Color._lerp(
1033+
this._lerp(
10271034
color1._a,
10281035
color1._a * color2._a,
10291036
amount
@@ -1159,7 +1166,7 @@
11591166

11601167
if (this._a < 1) {
11611168
return hex +
1162-
(this._a * 255 | 1 << 8)
1169+
(Math.round(this._a * 255) | 1 << 8)
11631170
.toString(16)
11641171
.slice(1);
11651172
}
@@ -1181,7 +1188,7 @@
11811188
*/
11821189
_getHSV() {
11831190
return this.constructor.RGB2HSV(this._r, this._g, this._b);
1184-
},
1191+
}
11851192

11861193
});
11871194

dist/frost-color.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.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "frostcolor",
3-
"version": "3.0.0",
3+
"version": "3.0.1",
44
"description": "FrostColor is a free, open-source color manipulation library for JavaScript.",
55
"keywords": [
66
"color",

src/Color/static/create.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ Object.assign(Color, {
8787
* @returns {Color} A new Color object.
8888
*/
8989
fromHSLString(string) {
90+
string = string.trim();
91+
9092
const HSL2Match = string.match(this._hsl2RegExp);
9193

9294
if (HSL2Match) {
@@ -125,7 +127,7 @@ Object.assign(Color, {
125127
);
126128
}
127129

128-
throw new Error('Invalid HSLA string');
130+
throw new Error('Invalid HSL string');
129131
},
130132

131133
/**
@@ -159,6 +161,8 @@ Object.assign(Color, {
159161
* @returns {Color} A new Color object.
160162
*/
161163
fromRGBString(string) {
164+
string = string.trim();
165+
162166
const RGB2Match = string.match(this._rgb2RegExp);
163167

164168
if (RGB2Match) {
@@ -206,16 +210,14 @@ Object.assign(Color, {
206210
* @returns {Color} A new Color object.
207211
*/
208212
fromString(string) {
209-
string = string.toLowerCase();
213+
string = string.toLowerCase().trim();
210214

211215
if (string === 'transparent') {
212216
return new this(0, 0, 0, 0);
213217
}
214218

215219
if (string in this.colors) {
216220
string = this.colors[string];
217-
} else {
218-
string = string.trim();
219221
}
220222

221223
if (string.substring(0, 1) === '#') {

src/Color/static/helpers.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ Object.assign(Color, {
3131
+ b * amount;
3232
},
3333

34+
/**
35+
* Shorten a hex string (if possible).
36+
* @param {string} hex The hex string.
37+
* @returns {string} The hex string.
38+
*/
3439
_toHex(hex) {
3540
if (hex.length === 9 &&
3641
hex[1] === hex[2] &&

src/Color/static/utility.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ Object.assign(Color, {
6969
*/
7070
mix(color1, color2, amount) {
7171
return new this(
72-
Color._lerp(color1._r, color2._r, amount),
73-
Color._lerp(color1._g, color2._g, amount),
74-
Color._lerp(color1._b, color2._b, amount),
75-
Color._lerp(color1._a, color2._a, amount)
72+
this._lerp(color1._r, color2._r, amount),
73+
this._lerp(color1._g, color2._g, amount),
74+
this._lerp(color1._b, color2._b, amount),
75+
this._lerp(color1._a, color2._a, amount)
7676
);
7777
},
7878

@@ -85,22 +85,22 @@ Object.assign(Color, {
8585
*/
8686
multiply(color1, color2, amount) {
8787
return new this(
88-
Color._lerp(
88+
this._lerp(
8989
color1._r,
9090
color1._r * color2._r / 255,
9191
amount
9292
),
93-
Color._lerp(
93+
this._lerp(
9494
color1._g,
9595
color1._g * color2._g / 255,
9696
amount
9797
),
98-
Color._lerp(
98+
this._lerp(
9999
color1._b,
100100
color1._b * color2._b / 255,
101101
amount
102102
),
103-
Color._lerp(
103+
this._lerp(
104104
color1._a,
105105
color1._a * color2._a,
106106
amount

src/Color/utility/helpers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Object.assign(Color.prototype, {
1414

1515
if (this._a < 1) {
1616
return hex +
17-
(this._a * 255 | 1 << 8)
17+
(Math.round(this._a * 255) | 1 << 8)
1818
.toString(16)
1919
.slice(1);
2020
}
@@ -36,6 +36,6 @@ Object.assign(Color.prototype, {
3636
*/
3737
_getHSV() {
3838
return this.constructor.RGB2HSV(this._r, this._g, this._b);
39-
},
39+
}
4040

4141
});

src/wrapper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* FrostColor v3.0.0
2+
* FrostColor v3.0.1
33
* https://github.com/elusivecodes/FrostColor
44
*/
55
(function(global, factory) {

test/Color/formatting.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ describe('Color Formatting', function() {
1414

1515
it('returns a short hex string (with alpha)', function() {
1616
assert.strictEqual(
17-
new Color(17, 17, 17, .07)
17+
new Color(17, 17, 17, .4)
1818
.toHexString(),
19-
'#1111'
19+
'#1116'
2020
);
2121
});
2222

@@ -32,7 +32,7 @@ describe('Color Formatting', function() {
3232
assert.strictEqual(
3333
new Color(120, 50, 50, .5)
3434
.toHexString(),
35-
'#7832327f'
35+
'#78323280'
3636
);
3737
});
3838
});

0 commit comments

Comments
 (0)