/// <summary> /// Gets an enumeration of individual shapes to be rendered in an icon for a specific hash. /// </summary> /// <param name="colorTheme">A color theme specifying the colors to be used in the icon.</param> /// <param name="hash">The hash for which the shapes will be returned.</param> protected virtual IEnumerable <Shape> GetShapes(ColorTheme colorTheme, byte[] hash) { var usedColorThemeIndexes = new List <int>(); foreach (var category in GetCategories()) { var colorThemeIndex = GetOctet(hash, category.ColorIndex) % colorTheme.Count; if (IsDuplicate(usedColorThemeIndexes, colorThemeIndex, 0, 4) || // Disallow dark gray and dark color combo IsDuplicate(usedColorThemeIndexes, colorThemeIndex, 2, 3)) // Disallow light gray and light color combo { colorThemeIndex = 1; } usedColorThemeIndexes.Add(colorThemeIndex); var startRotationIndex = category.RotationIndex == null ? 0 : GetOctet(hash, category.RotationIndex.Value); var shape = category.Shapes[GetOctet(hash, category.ShapeIndex) % category.Shapes.Count]; yield return(new Shape { Definition = shape, Color = colorTheme[colorThemeIndex], Positions = category.Positions, StartRotationIndex = startRotationIndex }); } }
/// <summary> /// Generates an identicon for the specified hash. /// </summary> /// <param name="renderer">The <see cref="Renderer"/> to be used for rendering the icon on the target surface.</param> /// <param name="rect">The outer bounds of the icon.</param> /// <param name="style">The style of the icon.</param> /// <param name="hash">The hash to be used as basis for the generated icon.</param> public void Generate(Renderer renderer, Rectangle rect, IdenticonStyle style, byte[] hash) { var hue = GetHue(hash); var colorTheme = new ColorTheme(hue, style); RenderBackground(renderer, rect, style, colorTheme, hash); RenderForeground(renderer, rect, style, colorTheme, hash); }
public void Generate(Renderer renderer, Rectangle rect, IdenticonStyle style, byte[] hash) { if (renderer == null) { throw new ArgumentNullException(nameof(renderer)); } if (style == null) { throw new ArgumentNullException(nameof(style)); } if (hash == null) { throw new ArgumentNullException(nameof(hash)); } var hue = GetHue(hash); var colorTheme = new ColorTheme(hue, style); RenderBackground(renderer, rect, style, colorTheme, hash); RenderForeground(renderer, rect, style, colorTheme, hash); }
/// <summary> /// Renders the foreground of an icon. /// </summary> /// <param name="renderer">The <see cref="Renderer"/> to be used for rendering the icon on the target surface.</param> /// <param name="rect">The outer bounds of the icon.</param> /// <param name="style">The style of the icon.</param> /// <param name="colorTheme">A color theme specifying the colors to be used in the icon.</param> /// <param name="hash">The hash to be used as basis for the generated icon.</param> protected virtual void RenderForeground(Renderer renderer, Rectangle rect, IdenticonStyle style, ColorTheme colorTheme, byte[] hash) { // Ensure rect is quadratic and a multiple of the cell count var normalizedRect = NormalizeRectangle(rect); var cellSize = normalizedRect.Width / CellCount; foreach (var shape in GetShapes(colorTheme, hash)) { var rotation = shape.StartRotationIndex; using (renderer.BeginShape(shape.Color)) { for (var i = 0; i < shape.Positions.Count; i++) { var position = shape.Positions[i]; renderer.Transform = new Transform( normalizedRect.X + position.X * cellSize, normalizedRect.Y + position.Y * cellSize, cellSize, rotation++ % 4); shape.Definition(renderer, cellSize, i); } } } }
/// <summary> /// Renders the background of an icon. /// </summary> /// <param name="renderer">The <see cref="Renderer"/> to be used for rendering the icon on the target surface.</param> /// <param name="rect">The outer bounds of the icon.</param> /// <param name="style">The style of the icon.</param> /// <param name="colorTheme">A color theme specifying the colors to be used in the icon.</param> /// <param name="hash">The hash to be used as basis for the generated icon.</param> protected virtual void RenderBackground(Renderer renderer, Rectangle rect, IdenticonStyle style, ColorTheme colorTheme, byte[] hash) { renderer.SetBackground(style.BackColor); }