numColors := len(t.colors)
if numColors == 0 { return rgb{} }
if numColors == 1 { return t.colors[0] }
if fraction <= 0.0 { return t.colors[0] }
if fraction >= 1.0 { return t.colors[numColors - 1] }
numSegments := numColors - 1
globalStage := fraction * float64(numSegments)
floorStage := math.Floor(globalStage)
stageIdx := max(0, min(int(floorStage), numSegments - 1))
scaledFraction := int((globalStage - floorStage) * float64(math.MaxInt16 + 1) + 0.5)
startColor := t.colors[stageIdx]
endColor := t.colors[stageIdx + 1]
lerp := func(start, end uint8) uint8 {
s, e := int(start), int(end)
val := (s << 15 + (e - s) * scaledFraction + int(math.MaxInt16 + 1) / 2) >> 15
return uint8(val & 0xFF)
}
return rgb{
lerp(startColor.r, endColor.r),
lerp(startColor.g, endColor.g),
lerp(startColor.b, endColor.b),
}
}