#region License Information /* HeuristicLab * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion 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); } } }