I bothered to post because I wanted to help you figure out how to troubleshoot things on your own, and also to make it easier for people to help you, which honestly is not easy with that function. Variables should have meaningful names, and by using ... you hide almost all parameters, making it very hard to follow the flow.
I can guarantee you that you will have forgotten what this code does before long:
Code:
local Num = (select("#", ...) / 3)
local Segment, RelPercent = modf(Percent * (Num - 1))
local R1, G1, B1, R2, G2, B2 = select((Segment * 3) + 1, ...)
return R1 + (R2 - R1) * RelPercent, G1 + (G2 - G1) * RelPercent, B1 + (B2 - B1) * RelPercent
If you refuse to take any of this to heart, then at least use this:
Originally Posted by Lombra
That being said, debugging is the first aid of programming! print the values on every line (or at least on two lines) where they change, and you should find out where the logic went wrong soon enough.
|
Debugging is an extremely simple and useful method of troubleshooting. Everyone uses it.
For example, you could do something like this.
Lua Code:
A.ColorGradient = function(a, b, ...)
local Percent
if b == 0 then
Percent = 0
else
Percent = a / b
end
print(Percent) -- does the Percent value look fine here?
if Percent >= 1 then
local R, G, B = select(select("#", ...) - 2, ...)
print(R, G, B) -- are these the expected color values?
return R, G, B
elseif Percent <= 0 then
local R, G, B = ...
print(R, G, B) -- are these the expected color values?
return R, G, B
end
local Num = (select("#", ...) / 3)
local Segment, RelPercent = modf(Percent * (Num - 1))
local R1, G1, B1, R2, G2, B2 = select((Segment * 3) + 1, ...)
print(R1, G1, B1, R2, G2, B2) -- are these the expected color values?
return R1 + (R2 - R1) * RelPercent, G1 + (G2 - G1) * RelPercent, B1 + (B2 - B1) * RelPercent
end
Edit: I may have been needlessly hostile, and for that I apologize. I stand by my opinion that the function code is not at all readable, however. Don't use varargs (...) nor
select unless you have a reason to do so, use meaningful variable names, and I would also say the last few lines are a bit too "mathy", but you may be forced to change that if you don't use varargs anyway, I suppose.