Class: Jazzicon::Icon

Inherits:
Object
  • Object
show all
Defined in:
lib/jazzicon/icon.rb

Overview

The Jazzicon::Icon class provides functionality to generate jazzy and colorful identicons. These identicons are generated based on a given seed and can be customized by specifying the diameter and the number of shapes that make up the icon.

The main steps involved in generating an icon include:

  • Hashing the seed to ensure deterministic randomness.

  • Randomly selecting and hue-shifting colors.

  • Generating and combining multiple layers of shapes (rectangles) to create a visually distinctive pattern.

  • Cropping the image into a circular form.

Key Features:

  • Deterministic: The same seed always produces the same icon.

  • Customizable: Supports customization of diameter and shape count.

Dependencies:

  • ‘chunky_png`: Used for image creation and manipulation.

Example Usage: “‘ require “jazzicon/icon”

seed = “example-seed” diameter = 500 shape_count = 4

icon = Jazzicon::Icon.generate(seed, diameter: diameter, shape_count: shape_count) icon.save(“icon.png”) “‘

Class Method Summary collapse

Class Method Details

.generate(seed, diameter: 500, shape_count: 4) ⇒ ChunkyPNG::Image

Generates a unique icon based on the given seed.

Parameters:

  • seed (String, Integer)

    The input seed used to generate the icon.

  • diameter (Integer) (defaults to: 500)

    The diameter of the generated icon.

  • shape_count (Integer) (defaults to: 4)

    The number of shapes to include in the icon.

Returns:

  • (ChunkyPNG::Image)

    The generated icon image.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/jazzicon/icon.rb', line 53

def self.generate(seed, diameter: 500, shape_count: 4)
  random = random(seed)
  colors = ColorHelpers.hue_shift(COLORS.shuffle(random: random), random)

  final_image = ChunkyPNG::Image.new(diameter, diameter, colors.delete(colors.sample(random: random)))

  # Generate each shape and adds it
  iterations(shape_count, colors, random).each do |iteration|
    rec = GeometryHelpers.random_rectangle(diameter, iteration.index, shape_count, iteration.random_numbers)
    final_image.polygon(rec.corners, iteration.color, iteration.color)
  end

  # Crop to a perfect circle
  ImageHelpers.crop_to_circle(final_image, diameter)

  # Profit!$!
  final_image
end