public GradientColorTable(GradientStop[] gradientPoints, int resolution) { if (null == gradientPoints) throw new ArgumentNullException("gradientPoints"); if (resolution < 1) throw new ArgumentException("Resolution should be at least 1", "resolution"); _sampleLength = 1f / resolution; _resolution = (float)resolution; _values = new Color[(int)(resolution)]; FillSamples(gradientPoints); }
/// <summary> /// Fills the color sample values. /// </summary> /// <param name="gradientPoints">The gradient points.</param> private void FillSamples(GradientStop[] gradientPoints) { if (gradientPoints.Length < 2) throw new ArgumentException("Gradient points array should be at least 2 poitns length", "gradientPoints"); for (int n = 0; n < gradientPoints.Length - 1; ++n) { int fromOffset = GetSampleOffsetFromPosition(gradientPoints[n].Position), toOffset = GetSampleOffsetFromPosition(gradientPoints[n + 1].Position); if (toOffset == fromOffset) { _values[toOffset] = gradientPoints[n].Color; } else { Color color1 = gradientPoints[n].Color; Color color2 = gradientPoints[n + 1].Color; color2 = new Color( color2.R - color1.R, color2.G - color1.G, color2.B - color1.B, color2.A - color1.A); //Color.Multiply(ref color2, 1f / (toOffset - fromOffset), out color2); color2 = color2 * (1f / (toOffset - fromOffset)); //fill colors from/to offset for (int m = fromOffset; m <= toOffset; ++m) { _values[m] = color1; color1 = new Color(color2.R + color1.R, color2.G + color1.G, color2.B + color1.B, color2.A + color1.A); } } } }