public static Color GetRelativeColor(this GradientStopCollection gsc, double offset) { GradientStop before = gsc.Where(w => w.Offset == gsc.Min(m => m.Offset)).First(); GradientStop after = gsc.Where(w => w.Offset == gsc.Max(m => m.Offset)).First(); 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); }
private Color GetRelativeColor(GradientStopCollection gsc, double offset) { GradientStop before = gsc.Where(w => w.Offset == gsc.Min(m => m.Offset)).First(); GradientStop after = gsc.Where(w => w.Offset == gsc.Max(m => m.Offset)).First(); 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 System.Windows.Media.Color { ScA = (float)((offset - before.Offset) * (after.Color.ScA - before.Color.ScA) / (after.Offset - before.Offset) + before.Color.ScA), ScR = (float)((offset - before.Offset) * (after.Color.ScR - before.Color.ScR) / (after.Offset - before.Offset) + before.Color.ScR), ScG = (float)((offset - before.Offset) * (after.Color.ScG - before.Color.ScG) / (after.Offset - before.Offset) + before.Color.ScG), ScB = (float)((offset - before.Offset) * (after.Color.ScB - before.Color.ScB) / (after.Offset - before.Offset) + before.Color.ScB) }; return(color); }
private Color EvaluateGradient(double offset) { // Praise be to StackOverflow // https://stackoverflow.com/questions/9650049/get-color-in-specific-location-on-gradient GradientStop before = _hpGradient.Where(w => w.Offset == _hpGradient.Min(m => m.Offset)).First(); GradientStop after = _hpGradient.Where(w => w.Offset == _hpGradient.Max(m => m.Offset)).First(); foreach (GradientStop gs in _hpGradient) { if (gs.Offset < offset && gs.Offset > before.Offset) { before = gs; } if (gs.Offset > offset && gs.Offset < after.Offset) { after = gs; } } Color color = new Color { ScA = (float)((offset - before.Offset) * (after.Color.ScA - before.Color.ScA) / (after.Offset - before.Offset) + before.Color.ScA), ScR = (float)((offset - before.Offset) * (after.Color.ScR - before.Color.ScR) / (after.Offset - before.Offset) + before.Color.ScR), ScG = (float)((offset - before.Offset) * (after.Color.ScG - before.Color.ScG) / (after.Offset - before.Offset) + before.Color.ScG), ScB = (float)((offset - before.Offset) * (after.Color.ScB - before.Color.ScB) / (after.Offset - before.Offset) + before.Color.ScB) }; return(color); }
public static Color GetRelativeColor(this GradientStopCollection gsc, double offset, float alpha) { var point = gsc.SingleOrDefault(x => x.Offset == offset); if (point != null) { return(point.Color); } GradientStop before = gsc.Where(x => x.Offset == gsc.Min(m => m.Offset)).First(); GradientStop after = gsc.Where(x => x.Offset == gsc.Max(m => m.Offset)).First(); 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.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); if (alpha >= 0 && alpha <= 1) { color.ScA = alpha; } else { color.ScA = (float)((offset - before.Offset) * (after.Color.ScA - before.Color.ScA) / (after.Offset - before.Offset) + before.Color.ScA); } return(color); }
private Color GetColor(float v) { if (v <= gradientStops.Min(a => a.Offset)) { return(gradientStops.WithMin(a => a.Offset).Color); } if (v >= gradientStops.Max(a => a.Offset)) { return(gradientStops.WithMax(a => a.Offset).Color); } var par = GradientStops.Select((gr, i) => new { gr, i }).FirstOrDefault(p => p.gr.Offset > v); float v1 = (float)GradientStops[par.i - 1].Offset; float v2 = (float)GradientStops[par.i].Offset; float vx = (v - v1) / (v2 - v1); return(GradientStops[par.i - 1].Color * (1 - vx) + GradientStops[par.i].Color * (vx)); }
/// <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)); }
private Color GetRelativeColor(GradientStopCollection gsc, double offset) { GradientStop before = gsc.Where(w => w.Offset == gsc.Min(m => m.Offset)).First(); GradientStop after = gsc.Where(w => w.Offset == gsc.Max(m => m.Offset)).First(); 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; }