private double NormalizeY(int index, double py)
{
Rectangle rect = GradientRectangle;
var yvals = (from val in vals select GetYFromValue (val)).Concat(
new double[] {rect.Y, rect.Y + rect.Height}).OrderByDescending (
v => v).ToArray();
index++;
if (py >= yvals [index - 1]) {
py = yvals [index - 1];
} else if (py < yvals [index + 1]) {
py = yvals [index + 1];
}
return py;
}