Pinta.ColorGradientWidget.FindValueIndex C# (CSharp) Method

FindValueIndex() private method

private FindValueIndex ( int y ) : int
y int
return int
        private int FindValueIndex(int y)
        {
            if (ValueIndex == -1) {
                var yvals = (from val in vals select GetYFromValue (val)).ToArray ();
                int count = Count - 1;

                for (int i = 0; i < count; i++) {
                    double y1 = yvals [i];
                    double y2 = yvals [i + 1];
                    double h = (y1 - y2) / 2;

                    // pointer is below the lowest value triangle
                    if (i == 0 && y1 < y)
                        return i;

                    // pointer is above the highest value triangle
                    if (i == (count - 1) && y2 > y)
                        return i + 1;

                    // pointer is outside i and i + 1 value triangles
                    if (!(y1 >= y && y >= y2))
                        continue;

                    // pointer is closer to lower value triangle
                    if (y1 - y <= h) return i;
                    // pointer is closer to higher value triangle
                    if (y - y2 <= h) return i + 1;
                }

                return -1;
            } else {
                return ValueIndex;
            }
        }