Skip to content

Instantly share code, notes, and snippets.

@TheEmpty
Last active December 27, 2015 03:19
Show Gist options
  • Save TheEmpty/7258361 to your computer and use it in GitHub Desktop.
Save TheEmpty/7258361 to your computer and use it in GitHub Desktop.
Color fading for Java UI
int[] rgb = {0,0,0};
int i = 0; // current working index
int step = 25;
while(true) {
// so raise R, raise G, drop R, raise B, drop G, etc?
int p = (i + 2) % 3; // previous index
if(rgb[i] >= 255 && rgb[p] > 0) {
rgb[p] -= step;
if(rgb[p] < 0) rgb[p] = 0;
} else {
if(rgb[i] >= 255) {
i = (i+1) % 3;
}
rgb[i] = (rgb[i] + step);
if(rgb[i] >= 255) rgb[i] = 255;
}
java.awt.Color clr = new java.awt.Color(rgb[0],rgb[1],rgb[2]);
try { Thread.sleep( 250 ); } catch(Exception ex) {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment