performance - parse color descriptions once

This commit is contained in:
bkmartinjr
2018-04-30 20:20:50 -07:00
parent c98dd20cb3
commit 118248446e
5 changed files with 41 additions and 49 deletions
+19
View File
@@ -0,0 +1,19 @@
import {scaleRGB} from "./scaleRGB";
export const parseRGB = (c) => {
if (c[0] !== "#") {
const _c = c.replace(/[^\d,.]/g, '').split(',');
return [
scaleRGB(+_c[0]),
scaleRGB(+_c[1]),
scaleRGB(+_c[2])
];
} else {
var parsedHex = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(c);
return [
scaleRGB(parseInt(parsedHex[1], 16)),
scaleRGB(parseInt(parsedHex[2], 16)),
scaleRGB(parseInt(parsedHex[3], 16))
];
}
};