Colour Cycler

Home

This function cycles through colors, fading between them. Each time the function is called it returns the next hex number for your color. You could use timer or other repetitive event to call the function, and use the returned color to change anything you like.

Please feel free to use, edit or improve this code in any way you like.

var myFontColour = ""
var hexString = "0123456789ABCDEF"
var hexNumber1 = 0
var hexNumber2 = 0
var changingPart = ""
var changingDirection = "UP"
var colourCycle = 1
function showtime() {
  if (changingDirection == "UP") {
    hexNumber2 = hexNumber2 + 1
    if (hexNumber2 > 15) {
      hexNumber2 = 0
      hexNumber1 = hexNumber1 + 1
      if (hexNumber1 > 15) {
        hexNumber2 = 15
        hexNumber1 = 15
        changingDirection = "DOWN"
        colourCycle = colourCycle + 1
      }
    }
  } else {
    hexNumber2 = hexNumber2 - 1
    if (hexNumber2 < 0) {
      hexNumber2 = 15
      hexNumber1 = hexNumber1 - 1
      if (hexNumber1 < 0) {
        hexNumber2 = 0
        hexNumber1 = 0
        changingDirection = "UP"
        colourCycle = colourCycle + 1
        if (colourCycle == 7) {
          colourCycle = 1
        }
      }
    }
  }
  changingPart = "" + hexString.substr(hexNumber1, 1) + hexString.substr(hexNumber2, 1) + ""
  switch (colourCycle) {
    case 1:
      myFontColour = changingPart + "FF00"
      break
    case 2:
      myFontColour = "FF" + changingPart + "00"
      break
    case 3:
      myFontColour = "FF00" + changingPart
      break
    case 4:
      myFontColour = changingPart + "00FF"
      break
    case 5:
      myFontColour = "00" + changingPart + "FF"
      break
    case 6:
      myFontColour = "00FF" + changingPart
      break
  }
  return outputColor
}