我試圖創建一個函數,輸入兩個RGB顏色和百分比,然後返回基於百分比的兩個之間的顏色。Lua:褪色功能
我在網上找到了Dec2Hex函數,並認爲它會有用。
現在我曾嘗試:
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 < 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
我覺得我朝着正確的方向,但數學是不正確的,我無法弄清楚如何解決它。提前致謝!
'tonumber(r1,16)'做了什麼?自從我最後碰到Lua以來已經有一段時間了。 –
將十六進制轉換爲十進制 –
像這樣? http://hawwashsoft.proboards.com/thread/109/gradient-draw-library – warspyking