Skip to content

Centralise color scale function creation #1090

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 8 commits into from
Oct 27, 2016
Merged

Conversation

etpinard
Copy link
Contributor

fixes #1064

Adding support for rgba colorscales (thus fixing #1064) was easy. It was simply a matter of porting that patch made in #422 for scatter traces in the contour module.

But, by doing I realised that our color scale function creation code wasn't very dry. So, this PR generalise the colorbar component makeScaleFunction routine so that all trace plot and colorbar module that need a color scale can use it.

- so that contour, heatmap and colorbar can call it
  to generate their color scale function
- as the interpolation logic isn't trivial (in order to support
  rgba colorscale), reusing the same logic everywhere is a big +
- no more d3.scale.linear in heatmap, contour and all the
  colorbar modules!
- this adds support for rgba colorscales in contour traces.
- the legend items of traces with colorscale are now
  properly shown.
@etpinard etpinard added bug something broken status: reviewable labels Oct 26, 2016
@etpinard
Copy link
Contributor Author

regarding c3577f9, the legend items in the baselines were wrong. They are now on-par their scatter equivalent:

scattergeo:

image

scatter3d:

image

if(opts.noNumericCheck || isNumeric(v)) {
var colorArray = _sclFunc(v);

if(opts.returnArray) return colorArray;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess returnArray is only valid if noNumericCheck===true - otherwise a non-numeric input would still return a color string. That's fine (assuming we don't use this), but we should probably throw an error if the unsupported combination is ever used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point. thanks for the headsup!

.range(range)
.clamp(true);

var sclFunc = function(v) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐎 It may not the slowest part of this, but since this is a hot path it might be worth pulling the opts logic out of this function, something like:

var sclNumeric = function(v) {
    var colorArray = _sclFunc(v);
    var colorObj = {
        r: colorArray[0],
        g: colorArray[1],
        b: colorArray[2],
        a: colorArray[3]
    };
    // may even be worthwhile to rewrite this one to avoid the creation
    // of a tinycolor object, but that doesn't have to be now.
    return tinycolor(colorObj).toRgbString();
};

if(opts.returnArray) {
    // and noNumericCheck, but we should already have checked that this is true
    return _sclFunc;
}
else if(opts.noNumericCheck) {
    sclFunc = sclNumeric;
}
else {
    sclFunc = function(v) {
        if(isNumeric(v)) return sclNumeric(v);
        else if(tinycolor(v).isValid()) return v;
        else return Color.defaultLine;
    };
}
// then your domain & range patches and return

Copy link
Contributor Author

@etpinard etpinard Oct 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may not the slowest part of this,

Yep, that's what I thought too. But you're right, 🐎 always 🐎

markerLine = marker.line || {},
zmin = trace.zmin,
zmax = trace.zmax,
scl = getColorscale(trace.colorscale),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah good catch, we've already done this at the supplyDefaults step so no need to repeat it anywhere else.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, so many getColorscale were 🔪 in this PR.

@@ -1,6 +1,523 @@
{
"data": [
{
"type": "contour",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might want to change the title to match what's actually being tested in this image (scatter & contour colorscale opacity)

* @return {function}
*
*/
module.exports = function makeScaleFunction(scl, cmin, cmax, opts) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this signature is a bit funny, especially when you have calls like

makeScaleFunction(scl, null, null, {domain: domain, range: range})

where scl gets ignored entirely. Maybe better to just put everything into opts?

- options object now includes cmin / cmax
- now respect all noNumericCheck + returnArray combination
- small perf boost in noNumericCheck version
- pass noNumericCheck option in contour / heatmap
  (where a clean-z-data pass is already done) and in
  all the colorbar module (which have clean data)
- into extractScale and makeColorScaleFn
domain: domain,
range: range
};
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK sure - we could imagine having this one also include opts and call the other one, since I can't see us ever using this without wrapping it in makeColorScaleFunc... but then naming gets confusing. meh, lets leave it as you have it here.

@alexcjohnson
Copy link
Collaborator

💃 nice consolidation!

@etpinard etpinard added this to the v1.19.0 milestone Oct 27, 2016
@etpinard etpinard merged commit 582d2d8 into master Oct 27, 2016
@etpinard etpinard deleted the contour-rgba-colorscale branch October 27, 2016 18:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug something broken
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RGBA colorscales are with broken on contour traces
2 participants