Skip to content

Add Multi-Layered Avatar Generation with Per-Layer Color Customization#16

Open
Sheikh566 wants to merge 5 commits intoMuhammadSaim:masterfrom
Sheikh566:master
Open

Add Multi-Layered Avatar Generation with Per-Layer Color Customization#16
Sheikh566 wants to merge 5 commits intoMuhammadSaim:masterfrom
Sheikh566:master

Conversation

@Sheikh566
Copy link
Copy Markdown
Contributor

Summary

This PR introduces a powerful multi-layered avatar generation feature to goavatar, enabling the creation of more complex and visually distinctive avatars. Users can now compose avatars from multiple layers (1-3), each with its own pattern derived from a unique hash and customizable foreground color.

Key Features

1. Multi-Layer Support

  • New WithLayers(n) option to specify the number of avatar layers (1-3)
  • Each layer is rendered sequentially, creating composite patterns
  • Subsequent layers use derived hashes for unique pattern variation

2. Per-Layer Color Customization

  • New WithLayerColor(layerIndex, r, g, b, a) option to set custom colors for specific layers
  • Flexible color assignment: set colors for some or all layers
  • Unspecified layers automatically inherit colors from their derived hash values

3. Performance Improvements

  • Optimized pixel drawing using image/draw package's Draw() function with draw.Uniform and draw.Over
  • Significantly faster bulk fill operations compared to per-pixel Set() calls
  • Background pre-fill optimization using draw.Src composite operator

4. Code Quality Improvements

  • Changed color storage from single color.RGBA to slice of color.NRGBA for proper alpha handling
  • Improved type documentation (renamed optFunc to OptFunc for exported visibility)
  • Enhanced test coverage for multi-layer scenarios
  • Updated examples demonstrating 2-layer and 3-layer avatars with custom colors

Technical Details

Color Space Update:

  • Migrated from color.RGBA to color.NRGBA for more accurate alpha channel handling
  • NRGBA (Non-premultiplied alpha) is the standard Go image color model

Multi-Layer Architecture:

  • Background is drawn once with the specified background color
  • Each foreground layer:
    • Derives its hash from the previous layer's hash using generateHash()
    • Uses either user-specified or hash-derived colors
    • Draws using the current layer's hash pattern
    • Overwrites previous pixels when pixelOn is true (cumulative composition)

Drawing Optimization:

  • Uses image.Rect() to define rectangular regions
  • Employs draw.Draw() with image.Uniform source for efficient fills
  • Reduces drawing operations from O(width × height) nested loops to O(1) fill

Examples

2-Layer Avatar:

image := goavatar.Make("MultiLayer2",
    goavatar.WithSize(512),
    goavatar.WithLayers(2),
)

3-Layer Avatar with Custom Colors:

image := goavatar.Make("MultiLayer3Custom",
    goavatar.WithSize(512),
    goavatar.WithLayers(3),
    goavatar.WithLayerColor(0, 255, 0, 0, 255),   // Red
    goavatar.WithLayerColor(1, 0, 255, 0, 255),   // Green
    goavatar.WithLayerColor(2, 0, 0, 255, 255),   // Blue
)

Files Changed

  • goavatar.go - Core implementation of multi-layer logic and drawing optimizations
  • goavatar_test.go - Enhanced test coverage for multi-layer scenarios
  • example/main.go - New examples demonstrating multi-layer functionality
  • arts/avatar_7.png, arts/avatar_8.png - Example outputs
  • arts/.gitignore - Updated to track new example images

Backward Compatibility

✅ Fully backward compatible. Existing code continues to work without modifications. The WithLayers() and WithLayerColor() options are opt-in.

Testing

All tests updated to support the new multi-layer architecture while maintaining existing test behavior for single-layer avatars.

cursoragent and others added 5 commits January 13, 2026 07:36
Implements multi-layered foregrounds for avatars.
- Updates `options` struct to support multiple foreground colors and layers.
- Adds `WithLayers` and `WithLayerColor` options.
- Updates `Make` function to render multiple layers with different hashes.
- Updates example code to demonstrate new features.
- Updates tests to verify multi-layered rendering logic.
…alpha colors

- Updated the `options` struct to use `color.NRGBA` for background and foreground colors.
- Adjusted related functions and tests to accommodate the new color type.
- Ensured consistent color handling across avatar generation logic.
…avatars-053d

Multi-layered foreground avatars
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants