Skip to content

Commit

Permalink
Merge pull request #339 from girder/memoize-annotation-style-color
Browse files Browse the repository at this point in the history
Memoize annotation style color.
  • Loading branch information
manthey authored Feb 21, 2019
2 parents 06d8729 + 7371c57 commit ab066cd
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions web_client/annotations/style.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,41 @@
import _ from 'underscore';
import tc from 'tinycolor2';

const props = [
'label'
];
var memoizeColorAlpha = {entries: 0};

function colorAlpha(color) {
if (!color) {
return null;
if (memoizeColorAlpha[color]) {
return memoizeColorAlpha[color];
}
color = tc(color);
return {
rgb: color.toHexString(),
alpha: color.getAlpha()
};
var tccolor = tc(color),
value = {
rgb: tccolor.toHexString(),
alpha: tccolor.getAlpha()
};
memoizeColorAlpha.entries += 1;
if (memoizeColorAlpha.entries > 100) {
memoizeColorAlpha = {entries: 0};
}
memoizeColorAlpha[color] = value;
return value;
}

export default function style(json) {
var color;
const style = _.pick(json, ...props);
const style = {};

color = colorAlpha(json.fillColor);
if (color) {
if (json.label) {
style.label = json.label;
}
if (json.fillColor) {
color = colorAlpha(json.fillColor);
style.fillColor = color.rgb;
style.fillOpacity = color.alpha;
}

color = colorAlpha(json.lineColor);
if (color) {
if (json.lineColor) {
color = colorAlpha(json.lineColor);
style.strokeColor = color.rgb;
style.strokeOpacity = color.alpha;
}

if (json.lineWidth) {
style.strokeWidth = json.lineWidth;
}
Expand Down

0 comments on commit ab066cd

Please sign in to comment.