SiteExperts.com Logo Home | Community | Developer's Paradise | Jobs
User Groups | Site Tools | Site Information | Search

Inside Technique : Cool Rules : The Code


function prefix(s) {
  // Make sure the hex# is 2 characters
  if (1==s.length)
    s="0"+s
  return s
}

function breakApart(start) {
  // Split the hex # into the RGB components
  var temp = new Array()
  temp[0] = parseInt(start.substring(0,2),16)
  temp[1] = parseInt(start.substring(2,4),16)
  temp[2] = parseInt(start.substring(4,6),16)
  return temp
}

function diffParts(startInt, endInt, steps) {
  // Determine the increment amount for each step
  var temp = new Array()
  for (var i=0; i<3; i++)
    temp[i] = Math.round((endInt[i] - startInt[i]) / steps)
  return temp
}


function createHR(start, end, steps, width, height) {
  var startInt = breakApart(start)
  var endInt = breakApart(end)
  var stepList = diffParts(startInt, endInt, steps)
  str = "<TABLE width="+width + " height="+height
  var alignment = arguments[7] // Optional alignment argument
  if (null!=alignment) {
    alignment.toLowerCase()
    // Ignore values other than left, right, and center
    if ("left"==alignment)
      str+=" align=left"
    else if ("right"==alignment)
      str+=" align=right"
    else if ("center"==alignment)
      str+=" align=center"
  }
  str+=" cellspacing=0 cellpadding=0 border=0><TR>"
  for (var r=0; r < steps; r++) {
      var hr = prefix(startInt[0].toString(16))
      var hg = prefix(startInt[1].toString(16))
      var hb = prefix(startInt[2].toString(16))
      // increment color
      for (var i=0; i < 3; i++)
        startInt[i]+=stepList[i]

      str+="<TD BGCOLOR="+hr + hg + hb+">"
      // Cross-browser version
      // Make sure to point the image to a valid
      // location on your server
      if (arguments[5]==true) str+="<IMG SRC='/gifs/s.gif' WIDTH=1 HEIGHT=1>"
      str+="</TD>"
      // Vertical rule
      if ((arguments[6]==true) && (r < steps-1))
        str+="<TR>"

  }
  str+="</TABLE>"
  return str
}
Discuss and Rate this Article