Gamma correct RGB interpolation #14

Closed
vorg opened this Issue Jan 7, 2016 · 4 comments

Comments

Projects
None yet
2 participants

vorg commented Jan 7, 2016

Are you considering adding it in the future?

Interpolating RGB values without gamma correct produces darker "dirtier" colors than they should be.

screen shot 2016-01-07 at 11 14 52

The solution would be to move values to linear space, interpolate and bring it back to gamma space.

//pseudocode for R channel
var color_hex = '#FF0000';
var color = hex2bytes(color_hex);
var byte_r = color[0];
var float_r = byte_r/255;
var linear_r = Math.pow(float_r, 2.2);
var inter_r = interpolate(linear_r, linear_r_2, t);
var gamma_r = Math.pow(inter_r, 1/2.2);
var out_r = Math.floor(gamma_r * 255);
Owner

mbostock commented Jan 7, 2016

This would be lovely. I’d recommend adopting an optional gamma parameter for interpolateRgbGamma, similar to what is done for Cubehelix interpolation.

mbostock closed this in 05d909a Feb 2, 2016

Owner

mbostock commented Feb 2, 2016

Your test case, above; my implementation, below:

screen shot 2016-02-02 at 12 51 04 pm

vorg commented Feb 3, 2016

Nice one. Thanks

Owner

mbostock commented Feb 3, 2016

Thanks for the suggestion. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment