public static Color GetRelativeColor(this GradientStopCollection gsc, double offset) { var before = gsc.First(w => w.Offset == gsc.Min(m => m.Offset)); var after = gsc.First(w => w.Offset == gsc.Max(m => m.Offset)); foreach (var gs in gsc) { if (gs.Offset < offset && gs.Offset > before.Offset) { before = gs; } if (gs.Offset > offset && gs.Offset < after.Offset) { after = gs; } } var color = new Color(); color.ScA = (float)((offset - before.Offset) * (after.Color.ScA - before.Color.ScA) / (after.Offset - before.Offset) + before.Color.ScA); color.ScR = (float)((offset - before.Offset) * (after.Color.ScR - before.Color.ScR) / (after.Offset - before.Offset) + before.Color.ScR); color.ScG = (float)((offset - before.Offset) * (after.Color.ScG - before.Color.ScG) / (after.Offset - before.Offset) + before.Color.ScG); color.ScB = (float)((offset - before.Offset) * (after.Color.ScB - before.Color.ScB) / (after.Offset - before.Offset) + before.Color.ScB); return(color); }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { double percentage = (double)value; GradientStop start = GradientStops.First(); GradientStop stop = GradientStops.Last(); foreach (GradientStop gradientStop in GradientStops) { if (start.Offset < gradientStop.Offset && gradientStop.Offset < percentage) { start = gradientStop; } if (percentage < gradientStop.Offset && gradientStop.Offset < stop.Offset) { stop = gradientStop; } } return(new SolidColorBrush(Color.FromScRgb((float)((percentage - stop.Offset) * (start.Color.ScA - stop.Color.ScA) / (start.Offset - stop.Offset) + stop.Color.ScA), (float)((percentage - stop.Offset) * (start.Color.ScR - stop.Color.ScR) / (start.Offset - stop.Offset) + stop.Color.ScR), (float)((percentage - stop.Offset) * (start.Color.ScG - stop.Color.ScG) / (start.Offset - stop.Offset) + stop.Color.ScG), (float)((percentage - stop.Offset) * (start.Color.ScB - stop.Color.ScB) / (start.Offset - stop.Offset) + stop.Color.ScB)))); }
/// <summary> /// Gets the color of the relative. /// </summary> /// <param name="gsc"> /// The GSC. /// </param> /// <param name="offset"> /// The offset. /// </param> /// <returns> /// The <see cref="Color"/>. /// </returns> public static Color GetRelativeColor(this GradientStopCollection gsc, double offset) { GradientStop before = gsc.First(w => w.Offset == gsc.Min(m => m.Offset)); GradientStop after = gsc.First(w => w.Offset == gsc.Max(m => m.Offset)); foreach (GradientStop gs in gsc) { if (gs.Offset < offset && gs.Offset > before.Offset) { before = gs; } if (gs.Offset > offset && gs.Offset < after.Offset) { after = gs; } } var a = (float) ((offset - before.Offset) * (after.Color.ScA - before.Color.ScA) / (after.Offset - before.Offset) + before.Color.ScA); var r = (float) ((offset - before.Offset) * (after.Color.ScR - before.Color.ScR) / (after.Offset - before.Offset) + before.Color.ScR); var g = (float) ((offset - before.Offset) * (after.Color.ScG - before.Color.ScG) / (after.Offset - before.Offset) + before.Color.ScG); var b = (float) ((offset - before.Offset) * (after.Color.ScB - before.Color.ScB) / (after.Offset - before.Offset) + before.Color.ScB); Color color = Color.FromArgb((int)(a * 255), (int)(r * 255), (int)(g * 255), (int)(b * 255)); return(color); }
public LuaColor GetRelativeColor(Dictionary <LuaColor, double> gradientColors, double offset) { if (offset < 0) { offset = 0; } if (offset > 1) { offset = 1; } var gsc = new GradientStopCollection(gradientColors.Select(gc => new GradientStop(gc.Key.Color, gc.Value))); var b = gsc.First(w => Math.Abs(w.Offset - gsc.Min(m => m.Offset)) < 0.01); var a = gsc.First(w => Math.Abs(w.Offset - gsc.Max(m => m.Offset)) < 0.01); foreach (var gs in gsc) { if (gs.Offset < offset && gs.Offset > b.Offset) { b = gs; } if (gs.Offset > offset && gs.Offset < a.Offset) { a = gs; } } var color = new Color { ScA = (float)((offset - b.Offset) * (a.Color.ScA - b.Color.ScA) / (a.Offset - b.Offset) + b.Color.ScA), ScR = (float)((offset - b.Offset) * (a.Color.ScR - b.Color.ScR) / (a.Offset - b.Offset) + b.Color.ScR), ScG = (float)((offset - b.Offset) * (a.Color.ScG - b.Color.ScG) / (a.Offset - b.Offset) + b.Color.ScG), ScB = (float)((offset - b.Offset) * (a.Color.ScB - b.Color.ScB) / (a.Offset - b.Offset) + b.Color.ScB) }; return(new LuaColor(color)); }