d3/d3-interpolate

Gamma correct RGB interpolation

vorg opened this issue · 4 comments

vorg commented

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);

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

Your test case, above; my implementation, below:

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

vorg commented

Nice one. Thanks

Thanks for the suggestion. :)