Skip to content

Commit f11358a

Browse files
authored
Update generator.js
1 parent ef98927 commit f11358a

File tree

1 file changed

+56
-59
lines changed

1 file changed

+56
-59
lines changed

generator.js

Lines changed: 56 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
/*
22
TODO: write tests (lazy) like always right? <3
33
*/
4-
const parser = require("gradient-parser");
4+
const parser = require('gradient-parser');
55

66
const getColor = color => {
77
switch (color.type) {
8-
case "hex":
8+
case 'hex':
99
return `#${color.value}`;
10-
case "literal":
10+
case 'literal':
1111
return color.value;
1212
default:
13-
return `${color.type}(${color.value.join(",")})`;
13+
return `${color.type}(${color.value.join(',')})`;
1414
}
1515
};
1616

@@ -21,12 +21,7 @@ const getColorsAndLocations = (colorStops, maxWidth) =>
2121

2222
// PX value for location will break!
2323
// TODO Make it happen for px + repeat?
24-
const locationValue = getPixelsForColor(
25-
color,
26-
colorStops.length,
27-
index,
28-
maxWidth
29-
);
24+
const locationValue = getPixelsForColor(color, colorStops.length, index, maxWidth);
3025
acc["locations"] = [...acc.locations, locationValue];
3126

3227
return acc;
@@ -37,87 +32,92 @@ const getColorsAndLocations = (colorStops, maxWidth) =>
3732
const getPixelsForColor = (color, colorsLength, index, maxWidth) => {
3833
const { length } = color;
3934
if (!length) {
40-
return (1 / (colorsLength - 1)) * index;
35+
return (1 / (colorsLength - 1)) * index;
4136
}
4237
if (length.type === "px") {
43-
return parseFloat(length.value);
38+
return parseFloat(length.value);
4439
}
45-
if (length.type === "%") {
46-
if (maxWidth) {
47-
return (parseFloat(length.value) * maxWidth) / 100;
48-
} else {
49-
return length.value / 100;
50-
}
51-
}
52-
};
53-
const getRepeatingColorsAndLocations = (colorStops, sizes) => {
54-
const { width: maxWidth, height: maxHeight } = sizes;
55-
56-
if (!maxWidth && !maxHeight) {
57-
throw new Error(
58-
"You have to define width and height for repeating gradient to work"
59-
);
40+
if (length.type === "%"){
41+
if (maxWidth) {
42+
return parseFloat(length.value) * maxWidth / 100;
43+
} else {
44+
return length.value / 100;
45+
}
6046
}
47+
}
6148

62-
const {
63-
colors: initialColors,
64-
locations: initialLocations
65-
} = getColorsAndLocations(colorStops, maxWidth);
49+
const getRepeatingColorsAndLocations = (colorStops, sizes) => {
50+
const {width: maxWidth, height: maxHeight} = sizes;
51+
const {colors: initialColors, locations: initialLocations} = getColorsAndLocations(colorStops, maxWidth);
6652
const maxValue = parseFloat(initialLocations.slice(-1)[0]);
6753
const increment = maxValue / maxWidth;
68-
const maxChunks = Math.round(maxWidth / maxValue) + 1;
54+
const maxChunks = Math.round((maxWidth / maxValue));
6955
const locations = [...Array(maxChunks).keys()].reduce((acc, i) => {
70-
return [...acc, ...initialLocations.map(j => j / maxWidth + increment * i)];
56+
return [...acc, ...initialLocations.map(j => {
57+
console.log()
58+
return j/maxWidth + increment * i
59+
})]
7160
}, []);
72-
const colors = locations.map(
73-
(_, i) => initialColors[i % initialColors.length]
74-
);
61+
const colors = locations.map((_, i) => initialColors[i % initialColors.length])
7562

76-
return { locations, colors };
63+
return { colors, locations };
7764
};
65+
7866
const getVectorsByDirection = direction => {
7967
switch (direction) {
80-
case "top":
68+
case 'top':
8169
return getVectorsByAngle(0);
82-
case "right":
70+
case 'right':
8371
return getVectorsByAngle(90);
84-
case "bottom":
72+
case 'bottom':
8573
return getVectorsByAngle(180);
86-
case "left":
74+
case 'left':
8775
return getVectorsByAngle(270);
88-
case "left top":
76+
case 'left top':
8977
return getVectorsByAngle(270 + 45);
90-
case "left bottom":
78+
case 'left bottom':
9179
return getVectorsByAngle(180 + 45);
92-
case "right top":
80+
case 'right top':
9381
return getVectorsByAngle(45);
94-
case "right bottom":
82+
case 'right bottom':
9583
return getVectorsByAngle(90 + 45);
9684
}
9785
};
98-
const round = number => Math.round(number * 1000) / 1000;
99-
const degreesToRadians = function(degrees) {
100-
return (degrees * Math.PI) / 180;
86+
87+
const round = (number) => Math.round(number * 10000) / 10000;
88+
var pointFromAngleAndDistance = function (origin, angle, distance) {
89+
angle = degreesToRadians(angle);
90+
return {
91+
x: distance * Math.cos(angle) + origin.x,
92+
y: distance * Math.sin(angle) + origin.y
93+
};
10194
};
95+
var degreesToRadians = function (degrees) { return degrees * Math.PI / 180; };
96+
10297
const getVectorsByAngle = alfa => {
103-
const angle = degreesToRadians(alfa);
98+
const angle = alfa * Math.PI / 180;
10499

105-
let gradientLineLength = round(
106-
Math.abs(Math.sin(angle)) + Math.abs(Math.cos(angle))
107-
);
100+
let gradientLineLength =
101+
round(Math.abs(Math.sin(angle)) + Math.abs(Math.cos(angle)));
108102
let center = { x: 0.5, y: 0.5 };
109103

110104
let yDiff = (Math.sin(angle - Math.PI / 2) * gradientLineLength) / 2;
111105
let xDiff = (Math.cos(angle - Math.PI / 2) * gradientLineLength) / 2;
112106

113107
return {
114-
start: [center.x - xDiff, center.y - yDiff],
115-
end: [center.x + xDiff, center.y + yDiff]
108+
start: {
109+
x: center.x - xDiff,
110+
y: center.y - yDiff
111+
},
112+
end: {
113+
x: center.x + xDiff,
114+
y: center.y + yDiff,
115+
},
116116
};
117117
};
118118

119119
const getVectorsByOrientation = orientation => {
120-
return orientation.type === "directional"
120+
return orientation.type === 'directional'
121121
? getVectorsByDirection(orientation.value)
122122
: getVectorsByAngle(orientation.value);
123123
};
@@ -128,10 +128,7 @@ const generateGradient = (gradient, sizes) => {
128128
if (type === "radial-gradient") {
129129
return "Only linear-gradient type is supported for now";
130130
}
131-
const colorsAndLocations =
132-
type === "linear-gradient"
133-
? getColorsAndLocations(colorStops)
134-
: getRepeatingColorsAndLocations(colorStops, sizes);
131+
const colorsAndLocations = type === "linear-gradient" ? getColorsAndLocations(colorStops) : getRepeatingColorsAndLocations(colorStops, sizes);
135132

136133
return {
137134
...colorsAndLocations,

0 commit comments

Comments
 (0)