Skip to content

Introduces attribute source to image traces #5075

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

Merged
merged 26 commits into from
Aug 31, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
1c893e2
Introduces attribute "source" to image traces featuring fast rendering
antoinerg Jun 26, 2020
d45496d
Ignores image trace's source attribute that are not data URIs
antoinerg Aug 13, 2020
f03c7ae
Adds image_labuda_droplets_source to mock_test.js
antoinerg Aug 14, 2020
2a687f2
Add "z" variable to hovertemplate in image defined by source attr
antoinerg Aug 14, 2020
aaf7cbc
Append promise of image loading to the plot api so it can wait for it
antoinerg Aug 14, 2020
e693446
In image traces, delete source attribute when it is invalid
antoinerg Aug 14, 2020
130593e
Fixes image_test to reflect that invalid source are deleted
antoinerg Aug 17, 2020
09eb1d0
Improve code style in the plot routine of image traces
antoinerg Aug 17, 2020
9b8a843
Add a workaround to image's onload event not firing when image is cached
antoinerg Aug 17, 2020
2ba0998
Introduces Lib.isIOS() to detect Apple mobile devices
antoinerg Aug 17, 2020
a2ea331
Fall back to legacy rendering of image traces for iOS devices
antoinerg Aug 17, 2020
4f807c5
Add missing semicolon
antoinerg Aug 18, 2020
7dd3b68
Fall back to legacy rendering of images traces for IE
antoinerg Aug 18, 2020
f2caed8
Loads images into an <img> instead of an <image> for image traces
antoinerg Aug 18, 2020
d91e0c1
Reintroduce image traces with source attribute in the `test-image` suite
antoinerg Aug 18, 2020
fe1d208
Improves code readability for image traces
antoinerg Aug 18, 2020
2960e55
Removes broken test following changes in how image traces check userA…
antoinerg Aug 18, 2020
3717cbe
Rename _isFrom(Z|Source) to _has(Z|Source)
antoinerg Aug 19, 2020
b586a86
Replace Labuda's photography of droplets by one from public domain
antoinerg Aug 19, 2020
d73f4c4
Add image_astronaut_source to mock_test.js
antoinerg Aug 19, 2020
18133c9
Update description of attribute `source` in images traces
antoinerg Aug 20, 2020
19ce21d
Introduce colormodel "rgba256" to image traces
antoinerg Aug 21, 2020
74574b6
Update description of attribute colormodel in image traces
antoinerg Aug 31, 2020
8c736f7
Update description of attribute source in image traces
antoinerg Aug 31, 2020
3b75921
Prefer z data over source in image traces and improve code readability
antoinerg Aug 31, 2020
66c58f6
:hocho: unreachable code in image trace defaults routine
antoinerg Aug 31, 2020
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
9 changes: 5 additions & 4 deletions src/traces/image/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ var hovertemplateAttrs = require('../../plots/template_attributes').hovertemplat
var extendFlat = require('../../lib/extend').extendFlat;
var colormodel = require('./constants').colormodel;

var cm = ['rgb', 'rgba', 'hsl', 'hsla'];
var cm = ['rgb', 'rgba', 'rgba256', 'hsl', 'hsla'];
var zminDesc = [];
var zmaxDesc = [];
for(var i = 0; i < cm.length; i++) {
zminDesc.push('For the `' + cm[i] + '` colormodel, it is [' + colormodel[cm[i]].min.join(', ') + '].');
zmaxDesc.push('For the `' + cm[i] + '` colormodel, it is [' + colormodel[cm[i]].max.join(', ') + '].');
var cr = colormodel[cm[i]];
zminDesc.push('For the `' + cm[i] + '` colormodel, it is [' + (cr.zminDflt || cr.min).join(', ') + '].');
zmaxDesc.push('For the `' + cm[i] + '` colormodel, it is [' + (cr.zmaxDflt || cr.max).join(', ') + '].');
}

module.exports = extendFlat({
Expand Down Expand Up @@ -47,7 +48,7 @@ module.exports = extendFlat({
editType: 'calc',
description: [
'Color model used to map the numerical color components described in `z` into colors.',
'If `source` is specified, this attribute will be set to `rgba`.'
'If `source` is specified, this attribute will be set to `rgba256`.'
].join(' ')
},
zmin: {
Expand Down
4 changes: 2 additions & 2 deletions src/traces/image/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ function constrain(min, max) {

// Generate a function to scale color components according to zmin/zmax and the colormodel
function makeScaler(trace) {
var colormodel = trace.colormodel;
var cr = constants.colormodel[trace.colormodel];
var colormodel = (cr.colormodel || trace.colormodel);
var n = colormodel.length;
var cr = constants.colormodel[colormodel];

trace._sArray = [];
// Loop over all color components
Expand Down
11 changes: 11 additions & 0 deletions src/traces/image/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

module.exports = {
colormodel: {
// min and max define the numerical range accepted in CSS
// If z(min|max)Dflt are not defined, z(min|max) will default to min/max
rgb: {
min: [0, 0, 0],
max: [255, 255, 255],
Expand All @@ -22,6 +24,15 @@ module.exports = {
fmt: function(c) {return c.slice(0, 4);},
suffix: ['', '', '', '']
},
rgba256: {
colormodel: 'rgba', // because rgba256 is not an accept colormodel in CSS
zminDflt: [0, 0, 0, 0],
zmaxDflt: [255, 255, 255, 255],
min: [0, 0, 0, 0],
max: [255, 255, 255, 1],
fmt: function(c) {return c.slice(0, 4);},
suffix: ['', '', '', '']
},
hsl: {
min: [0, 0, 0],
max: [360, 100, 100],
Expand Down
13 changes: 8 additions & 5 deletions src/traces/image/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@ module.exports = function supplyDefaults(traceIn, traceOut) {
coerce('dx');
coerce('dy');

var cm;
if(traceOut._hasZ) {
coerce('colormodel');
coerce('zmin', constants.colormodel[traceOut.colormodel].min);
coerce('zmax', constants.colormodel[traceOut.colormodel].max);
cm = constants.colormodel[traceOut.colormodel];
coerce('zmin', (cm.zminDflt || cm.min));
coerce('zmax', (cm.zmaxDflt || cm.max));
} else if(traceOut._hasSource) {
traceOut.colormodel = 'rgba';
traceOut.zmin = constants.colormodel[traceOut.colormodel].min;
traceOut.zmax = constants.colormodel[traceOut.colormodel].max;
traceOut.colormodel = 'rgba256';
cm = constants.colormodel[traceOut.colormodel];
traceOut.zmin = (cm.zminDflt || cm.min);
traceOut.zmax = (cm.zmaxDflt || cm.max);
}

coerce('text');
Expand Down
5 changes: 3 additions & 2 deletions src/traces/image/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ module.exports = function hoverPoints(pointData, xval, yval) {
if(parts.indexOf('color') !== -1) fmtColor = true;
}

var colormodel = trace.colormodel;
var cr = constants.colormodel[trace.colormodel];
var colormodel = cr.colormodel || trace.colormodel;
var dims = colormodel.length;
var c = trace._scaler(pixel);
var s = constants.colormodel[colormodel].suffix;
var s = cr.suffix;

var colorstring = [];
if(trace.hovertemplate || fmtColor) {
Expand Down
5 changes: 3 additions & 2 deletions src/traces/image/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ module.exports = function plot(gd, plotinfo, cdimage, imageLayer) {

// Create a new canvas and draw magnified pixels on it
function drawMagnifiedPixelsOnCanvas(readPixel) {
var colormodel = trace.colormodel;
var canvas = document.createElement('canvas');
canvas.width = imageWidth;
canvas.height = imageHeight;
Expand All @@ -108,7 +107,9 @@ module.exports = function plot(gd, plotinfo, cdimage, imageLayer) {
var ipx = function(i) {return Lib.constrain(Math.round(xa.c2p(x0 + i * dx) - left), 0, imageWidth);};
var jpx = function(j) {return Lib.constrain(Math.round(ya.c2p(y0 + j * dy) - top), 0, imageHeight);};

var fmt = constants.colormodel[colormodel].fmt;
var cr = constants.colormodel[trace.colormodel];
var colormodel = (cr.colormodel || trace.colormodel);
var fmt = cr.fmt;
var c;
for(i = 0; i < cd0.w; i++) {
var ipx0 = ipx(i); var ipx1 = ipx(i + 1);
Expand Down
2 changes: 1 addition & 1 deletion test/image/mocks/image_adventurer.json

Large diffs are not rendered by default.

13 changes: 7 additions & 6 deletions test/jasmine/tests/image_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ describe('image supplyDefaults', function() {
var tests = [
['rgb', [0, 0, 0], [255, 255, 255]],
['rgba', [0, 0, 0, 0], [255, 255, 255, 1]],
['rgba256', [0, 0, 0, 0], [255, 255, 255, 255]],
['hsl', [0, 0, 0], [360, 100, 100]],
['hsla', [0, 0, 0, 0], [360, 100, 100, 1]]
];
Expand Down Expand Up @@ -140,13 +141,13 @@ describe('image supplyDefaults', function() {
expect(traceOut.zmax).toEqual([20, 100, 100, 1], 'zmax default');
});

it('should set colormodel to rgba when source is defined', function() {
it('should set colormodel to rgba256 when source is defined', function() {
traceIn = {
type: 'image',
source: 'data:image/png;base64,asdf'
};
supplyDefaults(traceIn, traceOut);
expect(traceOut.colormodel).toBe('rgba');
expect(traceOut.colormodel).toBe('rgba256');
});

it('should override zmin/zmax when source is defined', function() {
Expand All @@ -157,9 +158,9 @@ describe('image supplyDefaults', function() {
zmax: 50
};
supplyDefaults(traceIn, traceOut);
expect(traceOut.colormodel).toBe('rgba');
expect(traceOut.colormodel).toBe('rgba256');
expect(traceOut.zmin).toEqual([0, 0, 0, 0]);
expect(traceOut.zmax).toEqual([255, 255, 255, 1]);
expect(traceOut.zmax).toEqual([255, 255, 255, 255]);
});
});

Expand Down Expand Up @@ -543,7 +544,7 @@ describe('image hover:', function() {
.then(function() {_hover(255, 295);})
.then(function() {
assertHoverLabelContent({
nums: 'x: 31.5\ny: 35.5\nz: [128, 77, 54, 254]\nRGBA: [128, 77, 54, 1]',
nums: 'x: 31.5\ny: 35.5\nz: [128, 77, 54, 255]\nRGBA: [128, 77, 54, 1]',
name: ''
});
})
Expand Down Expand Up @@ -573,7 +574,7 @@ describe('image hover:', function() {
.then(function() {_hover(255, 295);})
.then(function() {
assertHoverLabelContent({
nums: 'x: 31.5\ny: 35.5\nz: [128, 77, 54, 254]\nHSLA: [128°, 77%, 54%, 1]',
nums: 'x: 31.5\ny: 35.5\nz: [128, 77, 54, 255]\nHSLA: [128°, 77%, 54%, 1]',
name: ''
});
})
Expand Down