Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

colorWithOpacity tests and return consistency fix #116

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions __tests__/stylefunction-utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,42 @@ const stylefunction = require('../stylefunction');

describe('utility functions currently in stylefunction.js', function() {

describe('colorWithOpacity()', function() {
const colorWithOpacity = stylefunction.__get__('colorWithOpacity');
const colorCache = stylefunction.__get__('colorCache');

test('should not be identity with undefined opacity', function() {
const glcolor = Color.parse('hsl(47, 13%, 86%)');
const color = colorWithOpacity(glcolor);

should.notStrictEqual(color, colorWithOpacity(glcolor));
should(color).deepEqual(colorWithOpacity(glcolor));
});

test('should cache strictly by color', function() {

const glcolor1 = Color.parse('hsl(47, 13%, 86%)');
should.deepEqual([224, 222, 215, 1], colorWithOpacity(glcolor1, 1));
should(colorCache).containDeep({
[glcolor1.toString()]: {color: [224, 222, 215, 1], opacity: 1}
});

should.deepEqual([224, 222, 215, 0.5], colorWithOpacity(glcolor1, 0.5));
should(colorCache).containDeep(colorCache, {
[glcolor1.toString()]: {color: [224, 222, 215, 0.5], opacity: 1}
});
});

test('should multiply alpha correctly', function() {

const glcolor = Color.parse('rgba(255, 0, 0, 0.4)');
should.deepEqual([255, 0, 0, 0.2], colorWithOpacity(glcolor, 0.5));
should(colorCache).containDeep({
[glcolor.toString()]: {color: [255, 0, 0, 0.2], opacity: 0.4}
});
});
});

describe('evaluateFilter()', function() {
const filterCache = stylefunction.__get__('filterCache');
const evaluateFilter = stylefunction.__get__('evaluateFilter');
Expand Down
28 changes: 16 additions & 12 deletions stylefunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,22 @@ function evaluateFilter(layerId, filter, feature, zoom) {

const colorCache = {};
function colorWithOpacity(color, opacity) {
if (color && opacity !== undefined) {
let colorData = colorCache[color];
if (!colorData) {
colorCache[color] = colorData = {
color: color.toArray(),
opacity: color.a
};
}
color = colorData.color;
color[3] = colorData.opacity * opacity;
if (color[3] === 0) {
color = undefined;
if (color) {
if (opacity !== undefined) {
let colorData = colorCache[color];
if (!colorData) {
colorCache[color] = colorData = {
color: color.toArray(),
opacity: color.a
};
}
color = colorData.color;
color[3] = colorData.opacity * opacity;
if (color[3] === 0) {
color = undefined;
}
} else {
color = color.toArray();
}
}
return color;
Expand Down