Module: Lavatar::Helpers

Defined in:
lib/lavatar/helpers.rb

Instance Method Summary collapse

Instance Method Details

#lavatar_tag(letters, size, options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/lavatar/helpers.rb', line 3

def lavatar_tag(letters, size, options = {})
  key         = options[:key]         || letters
  saturation  = options[:saturation]  || Lavatar.configuration.saturation
  lightness   = options[:lightness]   || Lavatar.configuration.lightness
  color       = options[:color]       || key_to_color(key, saturation, lightness)
  font_color  = options[:font_color]  || Lavatar.configuration.font_color
  font_size   = options[:font_size]   || Lavatar.configuration.font_size
  font_weight = options[:font_weight] || Lavatar.configuration.font_weight
  klass       = options[:class]

  svg_attrs = {
    width: size,
    height: size,
    class: klass,
    viewBox: "0 0 100 100",
  }

  rect_attrs = {
    x: "0",
    y: "0",
    width: "100",
    height: "100",
    fill: color,
  }

  text_attrs = {
    x: "50%",
    y: "50%",
    fill: font_color,
    "font-size": font_size,
    "font-weight": font_weight,
    "alignment-baseline": "central",
    "text-anchor": "middle",
  }

   :svg, svg_attrs do
    tag(:rect, rect_attrs) + (:text, letters, text_attrs)
  end
end