title: Lua: color fading function
body: <p>I'm trying to create a function that inputs two RGB colors and a percentage then returns a color in-between the two based off of the percentage.</p>
  
  <p>I found the Dec2Hex function somewhere online and figured it would be useful.</p>
  
  <p>Right now I have tried:</p>
  
  <pre><code>function Dec2Hex(nValue) -- http://www.indigorose.com/forums/threads/10192-Convert-Hexadecimal-to-Decimal
      if type(nValue) == "string" then
          nValue = String.ToNumber(nValue);
      end
      nHexVal = string.format("%X", nValue);  -- %X returns uppercase hex, %x gives lowercase letters
      sHexVal = nHexVal.."";
      if nValue &lt; 16 then
          return "0"..tostring(sHexVal)
      else
          return sHexVal
      end
  end
  
  function fade_RGB(colour1, colour2, percentage)
      r1, g1, b1 = string.match(colour1, "#([0-9A-F][0-9A-F])([0-9A-F][0-9A-F])([0-9A-F][0-9A-F])")
      r2, g2, b2 = string.match(colour2, "#([0-9A-F][0-9A-F])([0-9A-F][0-9A-F])([0-9A-F][0-9A-F])")
      r3 = (tonumber(r1, 16)/tonumber(r2, 16))*(percentage)
      g3 = (tonumber(g1, 16)/tonumber(g2, 16))*(percentage)
      b3 = (tonumber(b1, 16)/tonumber(b2, 16))*(percentage)
      return "#"..Dec2Hex(r3).. Dec2Hex(g3)..Dec2Hex(b3)
  end
  </code></pre>
  
  <p>I think I'm headed in the right direction but the math isn't right and I can't figure out how to fix it. Thanks in advance!</p>