Skip to content

Commit 8f016e7

Browse files
committed
Use different color lib for greater precision
1 parent c25eb19 commit 8f016e7

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

index.js

+39-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const postcss = require('postcss');
22
const valueParser = require('postcss-value-parser');
3-
const tinyColor = require('tinycolor2');
3+
const Color = require('color');
4+
const round10 = require('round10').round10;
5+
// Easing functions
46
const easeInSine = require('eases/sine-in');
57
const easeOutSine = require('eases/sine-out');
68
const easeInOutSine = require('eases/sine-in-out');
@@ -64,14 +66,16 @@ module.exports = postcss.plugin('easing-gradient', function easingGradient() {
6466
// them back to words.
6567
if (param.type === 'function') functionToWord(param);
6668

67-
// If a param is a color and if so then add to colors array.
68-
//
69-
if (tinyColor(param.value).isValid()) {
70-
colors.push(param.value);
71-
72-
// A gradient direction is written using words and spaces
73-
} else if (param.type === 'word' || param.type === 'space') {
74-
direction += param.value;
69+
// If a param is a color and then add to colors array.
70+
try {
71+
Color(param.value) && colors.push(param.value);
72+
}
73+
catch(error) {
74+
// Test if it's a word or space and assume that's part of the
75+
// direction anotation — e.g. 'to'+' '+'top' or '45deg'
76+
if (param.type === 'word' || param.type === 'space') {
77+
direction += param.value;
78+
}
7579
}
7680
}
7781

@@ -117,6 +121,28 @@ function functionToWord(obj) {
117121
return obj;
118122
}
119123

124+
/**
125+
* Mix colors
126+
*/
127+
function mixColors(colorA, colorB, ammount) {
128+
return Color(colorA).mix(Color(colorB), ammount).hsl().string();
129+
}
130+
131+
/**
132+
* Round hue and alpha in hsl colors to 3 decimals
133+
*/
134+
function roundHueAlpha(color) {
135+
let prefix = color.substring(0, color.indexOf('('));
136+
let values = color
137+
.substring(color.indexOf('(') + 1, color.indexOf(')'))
138+
.split(',')
139+
.map(string => string.indexOf('%') === -1
140+
? round10(Number(string), -3)
141+
: string.trim()
142+
);
143+
return `${prefix}(${values.join(', ')})`;
144+
}
145+
120146
/**
121147
* Calculate the color stops based on start+stopColor in an array and easingType
122148
*/
@@ -125,12 +151,11 @@ function getColorStops(colors, easingType) {
125151
let gradientCoordinates = getCoordinates(easingType);
126152
let colorStops = '';
127153
for (let ammount in gradientCoordinates) {
128-
let color = tinyColor
129-
.mix(colors[0], colors[1], ammount * 100)
130-
.toHslString();
154+
let color = mixColors(colors[1], colors[0], ammount);
155+
color = roundHueAlpha(color);
131156
colorStops += `, ${color} ${gradientCoordinates[ammount]}`;
132157
}
133-
colorStops += `, ${colors[1]} 100%`;
158+
colorStops += `, ${roundHueAlpha(colors[1])} 100%`;
134159
return colorStops;
135160
};
136161

@@ -149,7 +174,7 @@ function transparentFix(colors) {
149174
*/
150175
function transparentToAlpha(colors, color) {
151176
let otherColor = colors[Math.abs(colors.indexOf(color) - 1)];
152-
let transparentOfOtherColor = tinyColor(otherColor).setAlpha(0).toHslString();
177+
let transparentOfOtherColor = Color(otherColor).alpha(0).hsl().string();
153178
return transparentOfOtherColor;
154179
}
155180

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "postcss-easing-gradients",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"description": "PostCSS plugin to create smooth linear-gradients that approximate easing functions.",
55
"main": "index.js",
66
"scripts": {
@@ -27,6 +27,6 @@
2727
"eases": "^1.0.8",
2828
"postcss": "^5.2.16",
2929
"postcss-value-parser": "^3.3.0",
30-
"tinycolor2": "^1.4.1"
30+
"round10": "^1.0.3",
3131
}
3232
}

0 commit comments

Comments
 (0)