Creating a Linear Gradient with Java for a drop down menu
It is a common mistake when trying to interpolate a color encoded in a 32bit value to just adjust the value. But that doesn't work (as you have found out).
1. Use the Color( float, float, float ) constructor that takes values from 0.0 - 1.0, and perform your linear interpolation on each value separately.
2. Keep a different integer for each red, green, and blue component, clamped between (0, 255), and then OR them together to create the final color.
int color = 0xFF << 24 | red << 16 | green << 8 | blue; // the 0xFF is the alpha value.
I did something like that once and faced an additional problem. Something to do with "gamma correction".
Basically even if the numbers were linear (as in 0.1, 0.2, 0.3), the actual color produced didnt look like it brightened in linear steps.
So what I did, was take the linearly interpolated value (between 0 and 1!) and take the square root of it (actually you should do pow(value, 1.0f/2.2f) but I found square root to be close enough for my purposes) and use that instead
so see if it looks any better if you use
Color(sqrt(r),sqrt(g),sqrt(b)) instead of Color(r,g,b)
I dont know too much about this topic though.
I did something like that once and faced an additional problem. Something to do with "gamma correction".
Basically even if the numbers were linear (as in 0.1, 0.2, 0.3), the actual color produced didnt look like it brightened in linear steps.
So what I did, was take the linearly interpolated value (between 0 and 1!) and take the square root of it (actually you should do pow(value, 1.0f/2.2f) but I found square root to be close enough for my purposes) and use that instead
so see if it looks any better if you use
Color(sqrt(r),sqrt(g),sqrt(b)) instead of Color(r,g,b)
I dont know too much about this topic though.
Gamma correction isn't necessary unless you really desire a linear gradient. Gradients in Photoshop, for example, are not linear.
If banding is noticeable you might want to consider adding a simple random dither to it.
do you mean just a random float that is less than what i subtracted added back into the amount or just subtracting a random float from the value to get the new value?
If banding is noticeable you might want to consider adding a simple random dither to it.
do you mean just a random float that is less than what i subtracted added back into the amount or just subtracting a random float from the value to get the new value?
Normally you would use floor(value * 255.0f + 0.5f) to convert your [0, 1] value to a 24-bit image. Instead use floor(value * 255.0f + random), where random is a float in the range [0, 1]. You can see the effect this has here.
If banding is noticeable you might want to consider adding a simple random dither to it.
do you mean just a random float that is less than what i subtracted added back into the amount or just subtracting a random float from the value to get the new value?
Normally you would use floor(value * 255.0f + 0.5f) to convert your [0, 1] value to a 24-bit image. Instead use floor(value * 255.0f + random), where random is a float in the range [0, 1]. You can see the effect this has here.
Thanks Chris this explains a lot with problems I have been having.