function ImLengthSqr(vec2) return (vec2.x * vec2.x) + (vec2.y * vec2.y) end
function ImDot(a, b) return a.x * b.x + a.y * b.y end
function ImClamp(v, mn, mx) return ((v < mn) and mn or ((v > mx) and mx or v)) end
-- int, int, ImVec2, ImVec2, ImU32, ImU32
function imgui.ImDrawList.__index:ShadeVertsLinearColorGradientKeepAlpha(vert_start_idx, vert_end_idx, gradient_p0, gradient_p1, col0, col1)
local gradient_extent = imgui.ImVec2(gradient_p1.x - gradient_p0.x, gradient_p1.y - gradient_p0.y )
local gradient_inv_length2 = 1.0 / ImLengthSqr(gradient_extent)
col0 = {
r = bit.band(bit.rshift(col0, 16), 0xFF),
g = bit.band(bit.rshift(col0, 8), 0xFF),
b = bit.band(bit.rshift(col0, 0), 0xFF)
}
local col_delta = {
r = bit.band(bit.rshift(col1, 16), 0xFF) - col0.r,
g = bit.band(bit.rshift(col1, 8), 0xFF) - col0.g,
b = bit.band(bit.rshift(col1, 0), 0xFF) - col0.b
}
for vert = vert_start_idx, vert_end_idx do
local d = ImDot(
imgui.ImVec2(
self.VtxBuffer.Data[vert].pos.x - gradient_p0.x,
self.VtxBuffer.Data[vert].pos.y - gradient_p0.y
),
gradient_extent
)
local t = ImClamp(d * gradient_inv_length2, 0.0, 1.0)
self.VtxBuffer.Data[vert].col = bit.bor(
bit.lshift(col0.r + col_delta.r * t, 16),
bit.lshift(col0.g + col_delta.g * t, 8),
bit.lshift(col0.b + col_delta.b * t, 0),
bit.band(self.VtxBuffer.Data[vert].col, 0xFF000000)
)
end
end