using System.Drawing.Drawing2D; using SharpMap; using SharpMap.Rendering.Decoration; using System; using System.Drawing; using System.Globalization; namespace HeuristicLab.BioBoost.Views.MapViews { public class GradientLegend : MapDecoration { public Size Size { get; set; } public double MinValue { get; set; } public double MaxValue { get; set; } public ColorBlend ColorBlend { get; set; } public Point Offset { get; set; } public Font Font { get; set; } public String Unit { get; set; } public GradientLegend() { Size = new Size(150, 20); Anchor = MapDecorationAnchor.LeftBottom; MinValue = 0; MaxValue = 1; Offset = new Point(10, 10); // color blend does not match the gradient legend exactly /* ColorBlend = new ColorBlend { Colors = new[] { Color.FromArgb(0, 0, 127), Color.FromArgb(0, 0, 255), Color.FromArgb(0, 255, 255), Color.FromArgb(0, 255, 0), Color.FromArgb(127, 255, 0), Color.FromArgb(255, 255, 0), Color.FromArgb(255, 127, 0), Color.FromArgb(255, 0, 0), Color.FromArgb(127, 0, 0)}, Positions = new[] { 0.0f, 0.125f, 0.25f, 0.375f, 0.5f, 0.625f, 0.75f, 0.875f, 1f } }; */ // GradientLegend uses ColorBlend from System.Drawing and we need to convert it to a SharpMap colorblend here ColorBlend = new ColorBlend() { Colors = LazyValueLayerGenerator.DefaultColorBlend.Colors, Positions = LazyValueLayerGenerator.DefaultColorBlend.Positions }; Font = (Font)SystemFonts.DefaultFont.Clone(); } protected override Size InternalSize(Graphics g, Map map) { return new Size(Size.Width + Offset.X + 4, Size.Height + Offset.Y + 3); } protected override void OnRender(Graphics g, Map map) { var pos = new PointF(g.ClipBounds.Left + Offset.X, g.ClipBounds.Bottom - Offset.Y - Size.Height); var brush = new System.Drawing.Drawing2D.LinearGradientBrush(pos, new PointF(pos.X + Size.Width, pos.Y), Color.Black, Color.Black) { InterpolationColors = ColorBlend }; g.FillRectangle(new SolidBrush(Color.FromArgb(127, 255, 255, 255)), pos.X - 1, pos.Y - 1, Size.Width + 3, Size.Height + 2); var minStr = MinValue.ToString(CultureInfo.InvariantCulture); var maxStr = MaxValue.ToString("G4", CultureInfo.InvariantCulture); if (!String.IsNullOrEmpty(Unit)) maxStr += " " + Unit; var minDim = g.MeasureString(minStr, Font); var maxDim = g.MeasureString(maxStr, Font); var textHeight = Math.Min(minDim.Height, maxDim.Height); g.FillRectangle(brush, pos.X, pos.Y + textHeight, Size.Width, Size.Height - textHeight - 2); g.DrawString(minStr, Font, Brushes.Black, pos.X, pos.Y); g.DrawString(maxStr, Font, Brushes.Black, pos.X + Size.Width - maxDim.Width - 2, pos.Y); g.DrawLine(Pens.Black, pos.X, pos.Y, pos.X, pos.Y + Size.Height); g.DrawLine(Pens.Black, pos.X + Size.Width, pos.Y, pos.X + Size.Width, pos.Y + Size.Height); g.DrawLine(Pens.Black, pos.X + Size.Width / 4, pos.Y + textHeight, pos.X + Size.Width / 4, pos.Y + Size.Height); g.DrawLine(Pens.Black, pos.X + Size.Width / 2, pos.Y + textHeight / 2, pos.X + Size.Width / 2, pos.Y + Size.Height); g.DrawLine(Pens.Black, pos.X + Size.Width / 4 * 3, pos.Y + textHeight, pos.X + Size.Width / 4 * 3, pos.Y + Size.Height); } } }