Skip to content

Commit 29ed8e3

Browse files
committed
Cleanup and Linting WIP
1 parent 3cc4741 commit 29ed8e3

File tree

3 files changed

+166
-148
lines changed

3 files changed

+166
-148
lines changed

helpers.js

+113-67
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/**
22
* Require constants
33
*/
4-
const Color = require('color');
5-
const round10 = require('round10').round10;
6-
const easeInSine = require('eases/sine-in');
7-
const easeOutSine = require('eases/sine-out');
4+
const Color = require('color');
5+
const round10 = require('round10').round10;
6+
const easeInSine = require('eases/sine-in');
7+
const easeOutSine = require('eases/sine-out');
88
const easeInOutSine = require('eases/sine-in-out');
9-
const easeInQuad = require('eases/quad-in');
10-
const easeOutQuad = require('eases/quad-out');
9+
const easeInQuad = require('eases/quad-in');
10+
const easeOutQuad = require('eases/quad-out');
1111
const easeInOutQuad = require('eases/quad-in-out');
1212

1313
/**
@@ -27,23 +27,16 @@ const scrimCoordinates = {
2727
0.94: '80.36%',
2828
0.97: '87.18%',
2929
0.99: '93.73%'
30-
}
31-
32-
/**
33-
* Calculate the color stops based on start+stopColor in an array and easingType
34-
*/
35-
exports.getColorStops = function(colors, easingType, delta, alphaDecimals) {
36-
colors = transparentFix(colors);
37-
let gradientCoordinates = getCoordinates(easingType, delta);
38-
let colorStops = '';
39-
for (let ammount in gradientCoordinates) {
40-
let color = Color(colors[1]).mix(Color(colors[0]), ammount);
41-
color = roundHslAlpha(color.hsl().string(), alphaDecimals);
42-
colorStops += `, ${color} ${gradientCoordinates[ammount]}`;
43-
}
44-
colorStops += `, ${colors[1]} 100%`;
45-
return colorStops;
4630
};
31+
const supportedGradients = [
32+
'ease-in-sine-gradient',
33+
'ease-out-sine-gradient',
34+
'ease-in-out-sine-gradient',
35+
'ease-in-quad-gradient',
36+
'ease-out-quad-gradient',
37+
'ease-in-out-quad-gradient',
38+
'scrim-gradient'
39+
];
4740

4841
/**
4942
* If a color is transparent then convert it to a transparent of the sibling
@@ -53,13 +46,50 @@ function transparentFix(colors) {
5346
? Color(colors[Math.abs(i - 1)]).alpha(0).hsl().string()
5447
: color
5548
);
56-
};
49+
}
50+
51+
/**
52+
* Test if new coordinate is far enough away from old coordinate
53+
*/
54+
function isFarEnough(x, y, xOld, yOld, delta) {
55+
return Math.sqrt(((x - xOld) ** 2) + ((y - yOld) ** 2)) > delta;
56+
}
57+
58+
/**
59+
* Convert decimal number to percentage string
60+
*/
61+
function getPercentage(number) {
62+
return `${round10(number * 100, -1)}%`;
63+
}
64+
65+
/**
66+
* Easing functions switcheroo
67+
*/
68+
function ease(x, type) {
69+
switch (type) {
70+
case 'ease-in-sine-gradient':
71+
return easeInSine(x);
72+
case 'ease-out-sine-gradient':
73+
return easeOutSine(x);
74+
case 'ease-in-out-sine-gradient':
75+
return easeInOutSine(x);
76+
case 'ease-in-quad-gradient':
77+
return easeInQuad(x);
78+
case 'ease-out-quad-gradient':
79+
return easeOutQuad(x);
80+
case 'ease-in-out-quad-gradient':
81+
return easeInOutQuad(x);
82+
default:
83+
console.log(`Sorry, easing gradient does not support ${type}.`);
84+
return null;
85+
}
86+
}
5787

5888
/**
5989
* Get coordinates based on easing function.
6090
* Delta checks ensures there's roughly the same distance between coordinates.
6191
*/
62-
function getCoordinates(easingFunction, delta) {
92+
function getCoordinates(easingFunction, precision) {
6393
if (easingFunction === 'scrim-gradient') return scrimCoordinates;
6494

6595
const yIncrements = 0.001;
@@ -72,6 +102,7 @@ function getCoordinates(easingFunction, delta) {
72102
let xOld = 0;
73103
let yOld = 0;
74104
let firstTime = true;
105+
let delta = precision;
75106

76107
while (firstTime || !isFarEnough(1, 1, xOld, yOld, delta - deltaTolerance)) {
77108
if (firstTime) {
@@ -81,7 +112,7 @@ function getCoordinates(easingFunction, delta) {
81112
y = 0;
82113
xOld = 0;
83114
yOld = 0;
84-
delta = delta - deltaAdjust;
115+
delta -= deltaAdjust;
85116
coordinates = {};
86117
}
87118
while (y <= 1) {
@@ -96,61 +127,76 @@ function getCoordinates(easingFunction, delta) {
96127
}
97128
}
98129
return coordinates;
99-
};
130+
}
100131

101132
/**
102-
* Easing functions switcheroo
133+
* Round alpha in hsl colors to alphaDecimals
103134
*/
104-
function ease(x, type) {
105-
switch (type) {
106-
case 'ease-in-sine-gradient':
107-
return easeInSine(x);
108-
break;
109-
case 'ease-out-sine-gradient':
110-
return easeOutSine(x);
111-
break;
112-
case 'ease-in-out-sine-gradient':
113-
return easeInOutSine(x);
114-
break;
115-
case 'ease-in-quad-gradient':
116-
return easeInQuad(x);
117-
break;
118-
case 'ease-out-quad-gradient':
119-
return easeOutQuad(x);
120-
break;
121-
case 'ease-in-out-quad-gradient':
122-
return easeInOutQuad(x);
123-
break;
124-
default:
125-
console.log(`Sorry, easing gradient does not support ${type}.`);
126-
}
135+
function roundHslAlpha(color, alphaDecimals) {
136+
const prefix = color.substring(0, color.indexOf('('));
137+
const values = color
138+
.substring(color.indexOf('(') + 1, color.indexOf(')'))
139+
.split(',')
140+
.map(string => string.indexOf('%') === -1
141+
? round10(Number(string), -alphaDecimals)
142+
: string.trim()
143+
);
144+
return `${prefix}(${values.join(', ')})`;
145+
}
146+
147+
/**
148+
* Calculate the color stops based on start+stopColor in an array and easingType
149+
*/
150+
exports.getColorStops = function getColorStops(
151+
colors,
152+
easingType,
153+
delta,
154+
alphaDecimals
155+
) {
156+
const fixedColors = transparentFix(colors);
157+
const gradientCoordinates = getCoordinates(easingType, delta);
158+
let colorStops = '';
159+
Object.keys(gradientCoordinates).forEach((ammount) => {
160+
// https://github.com/Qix-/color
161+
let color = Color(fixedColors[1]).mix(Color(fixedColors[0]), ammount);
162+
color = color.hsl().string();
163+
color = roundHslAlpha(color, alphaDecimals);
164+
colorStops += `${color} ${gradientCoordinates[ammount]}, `;
165+
});
166+
colorStops += `${colors[1]} 100%`;
167+
return colorStops;
127168
};
128169

170+
129171
/**
130-
* Convert decimal number to percentage string
172+
* Check if a string matches one of the supported gradients
131173
*/
132-
function getPercentage(number) {
133-
return round10(number * 100, -1) + '%';
174+
exports.isEasingGradient = function isEasingGradient(str) {
175+
return new RegExp(supportedGradients.join('|')).test(str);
134176
};
135177

136178
/**
137-
* Test if new coordinate is far enough away from old coordinate
179+
* Convert a function object to a word object
138180
*/
139-
function isFarEnough(x, y, xOld, yOld, delta) {
140-
return Math.sqrt((x - xOld) ** 2 + (y - yOld) ** 2) > delta;
181+
exports.functionToWord = function functionToWord(obj) {
182+
const object = obj;
183+
const array = [];
184+
// TODO: Make eslint happy
185+
/* eslint-disable no-restricted-syntax */
186+
for (const objNode of object.nodes) {
187+
if (objNode.type === 'word') {
188+
array.push(objNode.value);
189+
}
190+
}
191+
/* eslint-enable no-restricted-syntax */
192+
object.value = `${obj.value}(${array.join()})`;
193+
object.type = 'word';
194+
return object;
141195
};
142196

143197
/**
144-
* Round alpha in hsl colors to alphaDecimals
198+
* Test if two colors are identical
145199
*/
146-
function roundHslAlpha(color, alphaDecimals) {
147-
let prefix = color.substring(0, color.indexOf('('));
148-
let values = color
149-
.substring(color.indexOf('(') + 1, color.indexOf(')'))
150-
.split(',')
151-
.map(string => string.indexOf('%') === -1
152-
? round10(Number(string), -alphaDecimals)
153-
: string.trim()
154-
);
155-
return `${prefix}(${values.join(', ')})`;
200+
exports.isSameColor = function isSameColor(colorA, colorB) {
201+
return Color(colorA).hsl().string() === Color(colorB).hsl().string();
156202
};

0 commit comments

Comments
 (0)