Skip to content
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

Gamma correct RGB interpolation #14

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

Gamma correct RGB interpolation #14

vorg opened this issue Jan 7, 2016 · 4 comments

Comments

@vorg
Copy link

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);
@mbostock
Copy link
Member

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
Copy link
Member

mbostock commented Feb 2, 2016

Your test case, above; my implementation, below:

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

@vorg
Copy link
Author

vorg commented Feb 3, 2016

Nice one. Thanks

@mbostock
Copy link
Member

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
Labels
None yet
Development

No branches or pull requests

2 participants