// Copyright (C) Stichting Deltares 2024. All rights reserved. // // This file is part of the application DAM - UI. // // DAM - UI 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. // // This program 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 this program. If not, see . // // All names, logos, and references to "Deltares" are registered trademarks of // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Linq; namespace Deltares.Dam.Forms { /// /// TODO: class should be moved to maps assembly and should be covered by unit tests! /// public class Symbol { private readonly Color selectedFeatureBackgroundColor = Color.Red; private readonly Dictionary, Color> colorTable; private readonly Dictionary labelTable; private readonly Dictionary images; private readonly Dictionary selectedImages; protected Symbol() { colorTable = new Dictionary, Color>(); labelTable = new Dictionary(); images = new Dictionary(); selectedImages = new Dictionary(); } public Color GetColor(double value) { foreach (KeyValuePair, Color> keyValuePair in colorTable) { if (keyValuePair.Key(value)) { return keyValuePair.Value; } } return Color.Empty; } public Dictionary GetNamedSymbolTable() { return labelTable; } public Image GetImage(Color color, bool selected) { if (selected) { if (!selectedImages.ContainsKey(color)) { const int width = 23; const int height = width; var bitmap = new Bitmap(width, height); Graphics graphics = Graphics.FromImage(bitmap); // backround "glow" graphics.FillEllipse(new SolidBrush(selectedFeatureBackgroundColor), 0, 0, width - 1, height - 1); //graphics.FillRectangle(new SolidBrush(selectedFeatureBackgroundColor), 0, 0, width - 1, height - 1); // fill color const int fillOffset = 4; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.FillEllipse(new SolidBrush(color), fillOffset, fillOffset, width - fillOffset - 1 - 3, height - fillOffset - 1 - 3); // border const int borderThickness = 1; const int borderOffset = 3; var blackPen = new Pen(Brushes.Black) { Width = borderThickness }; graphics.DrawEllipse(blackPen, borderOffset, borderOffset, width - (1 + borderOffset + 4), height - (1 + borderOffset + 4)); /* const int square = 4; var leftTop = new Rectangle(1, 1, square, square); var leftBottom = new Rectangle(1, height- square -1, square, square); var rightTop = new Rectangle(width - square -1, 1, square, square); var rightBottom = new Rectangle(width - square -1, height - square -1, square, square); graphics.FillRectangle(new SolidBrush(Color.FromArgb(96, selectedFeatureBackgroundColor)), new Rectangle(1, 1, width, height)); graphics.FillRectangle(new SolidBrush(Color.White), leftTop); graphics.FillRectangle(new SolidBrush(Color.White), leftBottom); graphics.FillRectangle(new SolidBrush(Color.White), rightTop); graphics.FillRectangle(new SolidBrush(Color.White), rightBottom); graphics.DrawRectangle(new Pen(Color.Black), leftTop); graphics.DrawRectangle(new Pen(Color.Black), leftBottom); graphics.DrawRectangle(new Pen(Color.Black), rightTop); graphics.DrawRectangle(new Pen(Color.Black), rightBottom); graphics.DrawRectangle(new Pen(Color.Black), 1, 1, width - 1, height - 1); */ selectedImages[color] = bitmap; } return selectedImages[color]; } if (!images.ContainsKey(color)) { var bitmap = new Bitmap(18, 18); Graphics graphics = Graphics.FromImage(bitmap); graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.FillEllipse(new SolidBrush(color), 0, 0, 16, 16); var pen = new Pen(Brushes.Black); graphics.DrawEllipse(pen, 0, 0, 16, 16); images[color] = bitmap; } return images[color]; } public Image GetPieImage(IEnumerable percentages, IEnumerable values, bool selected) { Image bitmap = CreatePie(selected, percentages, values); return bitmap; } protected void RegisterColorAndLabel(string label, Color color, Func evaluationFunction) { colorTable.Add(evaluationFunction, color); labelTable.Add(label, GetImage(colorTable[evaluationFunction], false)); } private Image CreatePie(bool selected, IEnumerable piePercentages, IEnumerable pieValues) { double sum = piePercentages.Sum(); var bitmap = new Bitmap(18, 18); Graphics graphics = Graphics.FromImage(bitmap); var rect = new Rectangle(1, 1, 16, 16); var fDegSum = 0.0f; double[] percentages = piePercentages.ToArray(); double[] values = pieValues.ToArray(); for (var iCnt = 0; iCnt < percentages.Count(); iCnt++) { double fDegValue = (percentages[iCnt] / sum) * 360; Brush brush = new SolidBrush(GetColor(values[iCnt])); graphics.FillPie(brush, rect, fDegSum, (float) fDegValue); fDegSum += (float) fDegValue; } if (selected) { // Glow const int glowThickness = 2; var glowPen = new Pen(selectedFeatureBackgroundColor) { Width = glowThickness }; graphics.DrawEllipse(glowPen, 0, 0, 18, 18); } // else { var pen = new Pen(Brushes.Black); graphics.DrawEllipse(pen, 1, 1, 16, 16); } return bitmap; } } }