Class: Initials::SVG
- Inherits:
-
Object
- Object
- Initials::SVG
- Defined in:
- lib/initials/svg.rb
Constant Summary collapse
- HUE_WHEEL =
360
Instance Attribute Summary collapse
-
#colors ⇒ Object
readonly
Returns the value of attribute colors.
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#shape ⇒ Object
readonly
Returns the value of attribute shape.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
- #fill ⇒ Object
- #font_size ⇒ Object
-
#initialize(name, colors: 12, limit: 3, shape: :circle, size: 32) ⇒ SVG
constructor
A new instance of SVG.
- #initials ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(name, colors: 12, limit: 3, shape: :circle, size: 32) ⇒ SVG
Returns a new instance of SVG.
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
#colors ⇒ Object (readonly)
Returns the value of attribute colors.
5 6 7 |
# File 'lib/initials/svg.rb', line 5 def colors @colors end |
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
5 6 7 |
# File 'lib/initials/svg.rb', line 5 def limit @limit end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/initials/svg.rb', line 5 def name @name end |
#shape ⇒ Object (readonly)
Returns the value of attribute shape.
5 6 7 |
# File 'lib/initials/svg.rb', line 5 def shape @shape end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
5 6 7 |
# File 'lib/initials/svg.rb', line 5 def size @size end |
Instance Method Details
#fill ⇒ Object
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_size ⇒ Object
53 54 55 |
# File 'lib/initials/svg.rb', line 53 def font_size size/2 + size/16 - (initials.length * size/16) end |
#initials ⇒ Object
57 58 59 |
# File 'lib/initials/svg.rb', line 57 def initials name.split(' ')[0, limit].map { |s| s[0].capitalize }.join end |
#to_s ⇒ Object
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 |