Skip to content

Instantly share code, notes, and snippets.

@kurtisdunn
Last active August 29, 2015 14:05
Show Gist options
  • Save kurtisdunn/9beb1d31835532dcf436 to your computer and use it in GitHub Desktop.
Save kurtisdunn/9beb1d31835532dcf436 to your computer and use it in GitHub Desktop.
Random CSS background-image generated with JSP
<%
File imageDirectory = new File("./images");
String images[] = imageDirectory.list();
if(images != null){
int imageNumber = 0;
double randomNumber = Math.random();
int random2Digits = (int)(randomNumber * 100);
for(int i = 0; i < random2Digits; i++){
imageNumber = i % images.length;
}
String imageName = images[imageNumber];
int red = 0;
int green = 0;
int blue = 0;
if(imageName.length() > 13){
int lastDot = imageName.lastIndexOf(".");
String redString = imageName.substring(lastDot - 9, lastDot - 6);
String greenString = imageName.substring(lastDot - 6, lastDot - 3);
String blueString = imageName.substring(lastDot - 3, lastDot);
try{
red = Integer.parseInt(redString);
}
catch(Exception e){
}
try{
green = Integer.parseInt(greenString);
}
catch(Exception e){
}
try{
blue = Integer.parseInt(blueString);
}
catch(Exception e){
}
}
out.print("html{background: url(images/"+images[imageNumber]+");}");
}
%>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment