private void GenerateCivIdenticons() { List <Entity> civs = Entities.Where(entity => entity.IsCiv).ToList(); List <string> races = Entities.Where(entity => entity.IsCiv).GroupBy(entity => entity.Race).Select(entity => entity.Key).OrderBy(entity => entity).ToList(); int identiconSeed = Events.Count; Random code = new Random(identiconSeed); //Calculates color //Creates a variety of colors //Races 1 to 6 get a medium color //Races 7 to 12 get a light color //Races 13 to 18 get a dark color //19+ reduced color variance int maxHue = 300; int colorVariance; if (races.Count <= 1) { colorVariance = 0; } else if (races.Count <= 6) { colorVariance = Convert.ToInt32(Math.Floor(maxHue / Convert.ToDouble(races.Count - 1))); } else if (races.Count > 18) { colorVariance = Convert.ToInt32(Math.Floor(maxHue / (Math.Ceiling(races.Count / 3.0) - 1))); } else { colorVariance = 60; } foreach (Entity civ in civs) { int colorIndex = races.IndexOf(civ.Race); Color raceColor; if (colorIndex * colorVariance < 360) { raceColor = Formatting.HsvToRgb(colorIndex * colorVariance, 1, 1.0); } else if (colorIndex * colorVariance < 720) { raceColor = Formatting.HsvToRgb(colorIndex * colorVariance - 360, 0.4, 1); } else if (colorIndex * colorVariance < 1080) { raceColor = Formatting.HsvToRgb(colorIndex * colorVariance - 720, 1, 0.4); } else { raceColor = Color.Black; } int identiconCode; IdenticonRenderer identiconRenderer = new IdenticonRenderer(); identiconCode = code.Next(); civ.IdenticonCode = identiconCode; int alpha; if (races.Count <= 12) { alpha = 175; } else { alpha = 175; } if (!MainRaces.ContainsKey(civ.Race)) { MainRaces.Add(civ.Race, raceColor); } civ.IdenticonColor = Color.FromArgb(alpha, raceColor); civ.LineColor = raceColor; using (MemoryStream identiconStream = new MemoryStream()) { using (Bitmap identicon = identiconRenderer.Render(identiconCode, 64, civ.IdenticonColor)) { identicon.Save(identiconStream, System.Drawing.Imaging.ImageFormat.Png); byte[] identiconBytes = identiconStream.GetBuffer(); civ.Identicon = new Bitmap(identicon); civ.IdenticonString = Convert.ToBase64String(identiconBytes); } } using (MemoryStream smallIdenticonStream = new MemoryStream()) { using (Bitmap smallIdenticon = identiconRenderer.Render(identiconCode, 24, civ.IdenticonColor)) { smallIdenticon.Save(smallIdenticonStream, System.Drawing.Imaging.ImageFormat.Png); byte[] smallIdenticonBytes = smallIdenticonStream.GetBuffer(); civ.SmallIdenticonString = Convert.ToBase64String(smallIdenticonBytes); } } foreach (Entity group in civ.Groups) { group.Identicon = civ.Identicon; group.SmallIdenticonString = civ.SmallIdenticonString; } } Bitmap nullIdenticon = new Bitmap(64, 64); using (Graphics nullGraphics = Graphics.FromImage(nullIdenticon)) { using (SolidBrush nullBrush = new SolidBrush(Color.FromArgb(150, Color.Red))) nullGraphics.FillRectangle(nullBrush, new Rectangle(0, 0, 64, 64)); Entities.Where(entity => entity.Identicon == null).ToList().ForEach(entity => entity.Identicon = nullIdenticon); } }
private void GenerateCivIdenticons() { List <Entity> civs = Entities.Where(entity => entity.IsCiv).ToList(); List <string> races = Entities.Where(entity => entity.IsCiv).GroupBy(entity => entity.Race).Select(entity => entity.Key).OrderBy(entity => entity).ToList(); //Calculates color //Creates a variety of colors //Races 1 to 6 get a medium color //Races 7 to 12 get a light color //Races 13 to 18 get a dark color //19+ reduced color variance int maxHue = 300; int colorVariance; if (races.Count <= 1) { colorVariance = 0; } else if (races.Count <= 6) { colorVariance = Convert.ToInt32(Math.Floor(maxHue / Convert.ToDouble(races.Count - 1))); } else if (races.Count > 18) { colorVariance = Convert.ToInt32(Math.Floor(maxHue / (Math.Ceiling(races.Count / 3.0) - 1))); } else { colorVariance = 60; } foreach (Entity civ in civs) { int colorIndex = races.IndexOf(civ.Race); Color raceColor; if (colorIndex * colorVariance < 360) { raceColor = Formatting.HsvToRgb(colorIndex * colorVariance, 1, 1.0); } else if (colorIndex * colorVariance < 720) { raceColor = Formatting.HsvToRgb(colorIndex * colorVariance - 360, 0.4, 1); } else if (colorIndex * colorVariance < 1080) { raceColor = Formatting.HsvToRgb(colorIndex * colorVariance - 720, 1, 0.4); } else { raceColor = Color.Black; } var alpha = 176; if (!MainRaces.ContainsKey(civ.Race)) { MainRaces.Add(civ.Race, raceColor); } civ.LineColor = Color.FromArgb(alpha, raceColor); var iconStyle = new IdenticonStyle { BackColor = Jdenticon.Rendering.Color.FromArgb(alpha, raceColor.R, raceColor.G, raceColor.B) }; var identicon = Identicon.FromValue(civ.Name, 128); identicon.Style = iconStyle; civ.Identicon = identicon.ToBitmap(); using (MemoryStream identiconStream = new MemoryStream()) { civ.Identicon.Save(identiconStream, ImageFormat.Png); byte[] identiconBytes = identiconStream.GetBuffer(); civ.IdenticonString = Convert.ToBase64String(identiconBytes); } var small = Identicon.FromValue(civ.Name, 32); small.Style = iconStyle; var smallIdenticon = small.ToBitmap(); using (MemoryStream smallIdenticonStream = new MemoryStream()) { smallIdenticon.Save(smallIdenticonStream, ImageFormat.Png); byte[] smallIdenticonBytes = smallIdenticonStream.GetBuffer(); civ.SmallIdenticonString = Convert.ToBase64String(smallIdenticonBytes); } foreach (var childGroup in civ.Groups) { childGroup.Identicon = civ.Identicon; childGroup.LineColor = civ.LineColor; } } foreach (var entity in Entities.Where(entity => entity.Identicon == null)) { var identicon = Identicon.FromValue(entity.Name, 128); entity.Identicon = identicon.ToBitmap(); } }