Friday Puzzler: Color Rotator
It has been a while since my last ColdFusion Friday puzzler, so I hope you enjoy this one. It occurred to me while driving my eldest to school Wednesday morning and in my "not quite awake yet" semi-conscious daze, it seemed like a sneaky little puzzle. I figured out a possible simple solution before I got home, but I'm curious to see what others make of it. Ready?
Your client wants their web site to make use of a rotating color background. Each day, the background color of the web site will change. The list of colors used for the background are: red,blue,green,yellow,orange. (I know, I know, this is why I don't do web design.)
Your goal is to write a UDF that accepts a date and selects a color. If you pass the day after that initial date, you should pick the next day in the list. On the day you pick orange, the next day would pick red.
You may think - why not simply use the the day of the week modded by 5?
But this doesn't work on the edges. You could switch to the day of the year but would have the same issue (although a heck of a lot less). So how would you solve this issue? Note that you cannot persist anything in the database (or file system, etc).

Server.SiteBgIndex++;
And day of year would work except for leap year. So starting with that method and making it work better we could do DateDiff now() '01-01-2000' Mod 5.
https://gist.github.com/dannyrscott/5064455#file-f...
<cfset iColor = (DateDiff("dd", "1/1/2000", Now()) MOD ArrayLen(colorsRotation)) + 1 />
Now, this takes care of things server-side. Question is, this tells the user which color is today's background. It doesn't let the user "select" a color and it doesn't update the background color until another request is made. You could perhaps store some information in a cookie (if that sort of persistence is allowed) and implement some JavaScript to update the background color if the user is still viewing the same page before and after midnight.
http://pastebin.com/805J0ijt
BTW, "Driving a car" + "Thinking of coding" = "Nooooooooooooooooooooooooooooooooooooooooooooooooooot a good idea. ^^;