Class: Initials::SVG

Inherits:
Object
  • Object
show all
Defined in:
lib/initials/svg.rb

Constant Summary collapse

HUE_WHEEL =
360

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, colors: 12, limit: 3, shape: :circle, size: 32) ⇒ SVG

Returns a new instance of SVG.

Raises:



7
8
9
10
11
12
13
14
15
16
# File 'lib/initials/svg.rb', line 7

def initialize(name, colors: 12, limit: 3, shape: :circle, size: 32)
  @name = name.to_s.strip
  @colors = colors
  @limit = limit
  @shape = shape
  @size = size

  raise Initials::Error.new("Colors must be a divider of 360 e.g. 24 but not 16.") unless valid_colors?
  raise Initials::Error.new("Size is not a positive integer.") unless valid_size?
end

Instance Attribute Details

#colorsObject (readonly)

Returns the value of attribute colors.



5
6
7
# File 'lib/initials/svg.rb', line 5

def colors
  @colors
end

#limitObject (readonly)

Returns the value of attribute limit.



5
6
7
# File 'lib/initials/svg.rb', line 5

def limit
  @limit
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/initials/svg.rb', line 5

def name
  @name
end

#shapeObject (readonly)

Returns the value of attribute shape.



5
6
7
# File 'lib/initials/svg.rb', line 5

def shape
  @shape
end

#sizeObject (readonly)

Returns the value of attribute size.



5
6
7
# File 'lib/initials/svg.rb', line 5

def size
  @size
end

Instance Method Details

#fillObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/initials/svg.rb', line 38

def fill
  return "hsl(0, 0%, 67%)" if @name.empty?

  hue_step = HUE_WHEEL / colors
  char_sum = name.split("").sum do |c|
    # Multiplication makes sure neighboring characters (like A and B) are one hue step apart.
    c.ord * hue_step
  end

  # Spin the wheel!
  hue = char_sum % HUE_WHEEL

  "hsl(#{hue}, 40%, 40%)"
end

#font_sizeObject



53
54
55
# File 'lib/initials/svg.rb', line 53

def font_size
  size/2 + size/16 - (initials.length * size/16)
end

#initialsObject



57
58
59
# File 'lib/initials/svg.rb', line 57

def initials
  name.split(' ')[0, limit].map { |s| s[0].capitalize }.join
end

#to_sObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/initials/svg.rb', line 22

def to_s
  svg = [
    "<svg xmlns='http://www.w3.org/2000/svg' width='#{size}' height='#{size}'>",
      shape == :rect ?
        "<rect width='#{size}' height='#{size}' rx='#{size / 32}' ry='#{size / 32}' fill='#{fill}' />"
      :
        "<circle cx='#{size / 2}' cy='#{size / 2}' r='#{size / 2}' fill='#{fill}' />",
      "<text x='50%' y='50%' fill='white' fill-opacity='0.75' dominant-baseline='central' text-anchor='middle' style='font-size: #{font_size}px; font-family: -apple-system, BlinkMacSystemFont, \"Segoe UI\", Roboto, Oxygen-Sans, Ubuntu, Cantarell, \"Helvetica Neue\", sans-serif; user-select: none;'>",
        "#{initials}",
      "</text>",
    "</svg>"
  ].join

  svg.html_safe rescue svg
end