Skip to content

Commit 1f60036

Browse files
committed
fix(Utils): add colors to hex method
1 parent b0b17dd commit 1f60036

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

src/Utils.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ export const isEmpty = (x) => {
1010
}
1111

1212
if (!x) return true;
13-
if (x === "") return true;
1413
if (x === {}) return true;
1514
if (x === []) return true;
1615
if (x == null) return true;
@@ -85,8 +84,30 @@ export const parseColor = (color) => {
8584
} else {
8685
colors = [0, 0, 0, 0];
8786
}
88-
isNaN(colors[3]) && (colors[3] = 1);
89-
return colors.slice(0, 4);
87+
88+
// Add alpha if missing
89+
if (isNaN(colors[3])) {
90+
colors[3] = 255;
91+
}
92+
93+
return colors;
94+
};
95+
96+
/**
97+
* Parsed colors to hex code
98+
* @param colors
99+
* @returns {string}
100+
*/
101+
export const colorsToHex = (colors) => {
102+
const hexColors = colors.map((color) => {
103+
color = color.toString(16);
104+
while (color.length < 2) {
105+
color += "0";
106+
}
107+
return color;
108+
});
109+
110+
return "#" + hexColors.join("");
90111
};
91112

92113
/**

0 commit comments

Comments
 (0)