static Color[] GenerateColors(AdaptiveGradient gradient, uint numSamples) { double leftAnchor = gradient.First().Anchor.AnchorValue; double rightAnchor = gradient.Last().Anchor.AnchorValue; //we need to make sure it spreads out across the whole range, so we set the //increment such that we include the end two colors. double increment = (rightAnchor - leftAnchor) / (numSamples - 1); Color[] colors = new Color[numSamples]; double targetValue; for (int i = 0; i < numSamples; i++) { targetValue = leftAnchor + increment * i; //consider stops in pairs, and if we're in the right pair // interpolate between the stops. for (int j = 0; j < gradient.Count - 1; j++) { //because of the way we're counting, if this condition // is true, we're between a pair of gradient stops // with the left one smaller and the right one larger. if (targetValue >= gradient[j].Anchor.AnchorValue) { colors[i] = InterpolateBetweenStops(targetValue, gradient[j], gradient[j + 1]); continue; } } } return(colors); }
/// <summary> /// Generates <see cref="AdaptiveGradientMap{T}"/>s based upon the specified /// <see cref="AdaptiveGradient"/> and a number of samples. /// </summary> /// <param name="gradient">The gradient to generate a map with.</param> /// <param name="numSamples">The number of samples to interpolate on the /// gradient.</param> public AdaptiveGradientMapBuilder(AdaptiveGradient gradient, uint numSamples = 10) { NumSamples = numSamples; targetValues = GenerateTargetValues(gradient, numSamples); colorValues = GenerateColors(gradient, numSamples); }
/// <summary> /// Generate a default gradient, with <see cref="Colors.Blue"/> at the /// minimum value of the dataset and <see cref="Colors.Red"/> at the /// maximum. /// </summary> /// <returns>A default gradient, auto scaled from blue to red.</returns> public static AdaptiveGradient BlueRedGradient() { AdaptiveGradient ag = new AdaptiveGradient(); //default blue = low, red = high. ag.Add(new AdaptiveGradientStop(Colors.Blue, new MinValue())); ag.Add(new AdaptiveGradientStop(Colors.Red, new MaxValue())); return(ag); }
static double[] GenerateTargetValues(AdaptiveGradient gradient, uint numSamples) { double leftAnchor = gradient.First().Anchor.AnchorValue; double rightAnchor = gradient.Last().Anchor.AnchorValue; double increment = (rightAnchor - leftAnchor) / numSamples; double[] targetValues = new double[numSamples]; //generate sample sets. Note that each block is represented by the max value in the block. for (int i = 1; i <= numSamples; i++) { targetValues[i - 1] = leftAnchor + increment * i; } return(targetValues); }