Sha256: 8af5c0e0e7abdc1794f71518b7b93319b3f77e3d570637748929d2c4e504dd09

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

module InitialAvatar
  class Avatar
    attr_reader :text
    attr_reader :opts

    def initialize(text, options = {})
      @text = text.upcase
      @opts = InitialAvatar.configuration.default_options.merge(options)
    end

    def svg_tag
      <<~SVG
        <svg xmlns="http://www.w3.org/2000/svg" pointer-events="none" width="#{opts[:size]}" height="#{opts[:size]}" style="background-color: #{color}; width: #{opts[:size]}px; height: #{opts[:size]}px;">
        <text text-anchor="middle" y="50%" x="50%" dy="0.35em" pointer-events="auto" fill="#{opts[:text_color]}" font-family="#{opts[:font_family]}" style="font-weight: #{opts[:font_weight]}; font-size: #{font_size}px;">#{text}</text>
        </svg>
      SVG
    end

    def data_uri
      "data:image/svg+xml;base64,#{Base64.strict_encode64(svg_tag)}"
    end

    private

    def color
      return opts[:color] if opts[:color]
      index = text.ord % InitialAvatar.configuration.colors.length
      InitialAvatar.configuration.colors[index]
    end

    def font_size
      opts[:font_size] || opts[:size] / 2
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
initial_avatar-0.1.0 lib/initial_avatar/avatar.rb