A simple dithering approach for gradients.
Just to clarify - The reason for using floats instead of writing to this.Pixel directly is because a color uses only 8-bit ( opposed to a 32-bit float ) .. and since the whole point of dithering is to fake a higher bit depth, we don't want to lose any precision until we're done with our algorithm.
Code: Select all
float C, F, S, R, G, B;
C = 9/16-X/8; // Low contrast gradient ( less colors available then preferable )
R = C;
G = C;
B = C;
F = frac(C*255);
S = 1/255; // 1-bit offset ( on a 8-bit value )
if(frac(F) < 0.25)R += S;
if(frac(F) < 0.50)G += S;
if(frac(F) < 0.75)B += S;
this.Pixel.R = R;
this.Pixel.G = G;
this.Pixel.B = B;
Attached is a zip containing two 512x512 radial gradients, one without dithering, one using the described technique. Since the difference is subtle, also attached is a high contrast comparison of the two.
More complex algorithms to follow ..
K