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.
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);
mbostock commented
This would be lovely. I’d recommend adopting an optional gamma parameter for interpolateRgbGamma, similar to what is done for Cubehelix interpolation.
mbostock commented
vorg commented
Nice one. Thanks
mbostock commented
Thanks for the suggestion. :)