mirror of
https://github.com/chanzuckerberg/cellxgene.git
synced 2026-09-27 18:18:12 +08:00
Merge pull request #20 from chanzuckerberg/bkmartinjr-perf2
Perf - cache parsed color descriptions
This commit is contained in:
+16
-1
@@ -1,7 +1,13 @@
|
||||
// jshint esversion: 6
|
||||
import { scaleRGB } from "./scaleRGB";
|
||||
|
||||
export const parseRGB = c => {
|
||||
// maintain a cache of already parsed RGB names, as it is reasonably expensive
|
||||
// to do this operation. This lets us have speed, but keep the pleasant ability
|
||||
// to talk about colors by their text description eg, 'rgb(0,0,1)'
|
||||
//
|
||||
const colorCache = new Object(null); // no prototype
|
||||
|
||||
function parseColorName(c) {
|
||||
if (c[0] !== "#") {
|
||||
const _c = c.replace(/[^\d,.]/g, "").split(",");
|
||||
return [scaleRGB(+_c[0]), scaleRGB(+_c[1]), scaleRGB(+_c[2])];
|
||||
@@ -13,4 +19,13 @@ export const parseRGB = c => {
|
||||
scaleRGB(parseInt(parsedHex[3], 16))
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
export const parseRGB = c => {
|
||||
var cv = colorCache[c];
|
||||
if (!cv) {
|
||||
cv = parseColorName(c);
|
||||
colorCache[c] = cv;
|
||||
}
|
||||
return cv;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user