initials 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b1ddb7223bac6b77c96253275a3fcdc39d6de169c445650df95694f9897ad25b
4
- data.tar.gz: 33af3be7bf7dbb56b6c2ad947fa2aa157b35f63bf98e515f4b8d852fa69ff514
3
+ metadata.gz: 74189037b48b4afad67b1139cb85e50385a64cfcfb4f5fc49aa9cc7d1f71332e
4
+ data.tar.gz: dbbeacbc6d9a6ea6705aab982a822ff51423271fc3dc0425f00f45298b6416fa
5
5
  SHA512:
6
- metadata.gz: a8f6b21b1f49b69cb49693b3f157f9597542597140e96b1124f8c2c13087326d5e1a4183b353393740fa82e91e3d6e9e789c8eb74eab9688b9069129708f1265
7
- data.tar.gz: 3b62b60389a157f0713717bb086ec40a3c4b74edbe139609fef642535cd33df84672a61b50661ffd8ac3332e839409a376a9146dc977356e7b62b1434b8fa0e6
6
+ metadata.gz: 6f62f412c72180e8430fab32b54ad3dfc9fe13e722e7e076fc4112bb04abdb5987570cf6b07254367b13f8335aea494689cfc05d0f970f3d9fc701d3fa413eaf
7
+ data.tar.gz: 683b723bb2fac2f305558966614c4e6baf3407c1b441335667b65350e34a8f4fbbc2be22b84b540e85159ed6cd8b9798cf2374b4834404cc7f667827c829e1b2
data/README.md CHANGED
@@ -50,9 +50,11 @@ Initials automatically marks its created SVG strings as `html_safe`.
50
50
  You can pass the following options into `Initials.svg` or your `user_avatar` helper:
51
51
 
52
52
  ```ruby
53
- user_avatar(current_user.name,
54
- limit: 1, # sets max length for initials
55
- size: 96 # sets SVG height and width in pixel
53
+ user_avatar(current_user.name,
54
+ colors: 8, # number of different colors, default: 12
55
+ limit: 1, # maximal initials length, default: 3
56
+ shape: :rect, # background shape, default: :cirlce
57
+ size: 96 # SVG height and width in pixel, default: 32
56
58
  )
57
59
  ```
58
60
 
data/demo.png CHANGED
Binary file
data/lib/initials/svg.rb CHANGED
@@ -1,18 +1,28 @@
1
1
  module Initials
2
2
  class SVG
3
- attr_reader :name, :limit, :size
3
+ HUE_WHEEL = 360
4
4
 
5
- def initialize(name, limit: 3, size: 32)
6
- raise Initials::Error.new("Name is not a string or empty") unless (name.respond_to?(:to_s) && name.to_s.length > 0)
5
+ attr_reader :name, :colors, :limit, :shape, :size
6
+
7
+ def initialize(name, colors: 12, limit: 3, shape: :circle, size: 32)
7
8
  @name = name
9
+ @colors = colors
8
10
  @limit = limit
11
+ @shape = shape
9
12
  @size = size
13
+
14
+ raise Initials::Error.new("Name is not a string or empty.") unless (name.respond_to?(:to_s) && name.to_s.length > 0)
15
+ raise Initials::Error.new("Colors must be a divider of 360 e.g. 24 but not 16.") unless valid_colors?
16
+ raise Initials::Error.new("Size is not a positive integer.") unless valid_size?
10
17
  end
11
18
 
12
19
  def to_s
13
20
  svg = [
14
21
  "<svg width='#{size}' height='#{size}'>",
15
- "<circle cx='#{size / 2}' cy='#{size / 2}' r='#{size / 2}' fill='#{fill}' />",
22
+ shape == :rect ?
23
+ "<rect width='#{size}' height='#{size}' rx='#{size / 32}' ry='#{size / 32}' fill='#{fill}' />"
24
+ :
25
+ "<circle cx='#{size / 2}' cy='#{size / 2}' r='#{size / 2}' fill='#{fill}' />",
16
26
  "<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;'>",
17
27
  "#{initials}",
18
28
  "</text>",
@@ -23,7 +33,15 @@ module Initials
23
33
  end
24
34
 
25
35
  def fill
26
- hue = name.split("").sum { |c| c.ord } % 360
36
+ hue_step = HUE_WHEEL / colors
37
+ char_sum = name.split("").sum do |c|
38
+ # Multiplication makes sure neighboring characters (like A and B) are one hue step apart.
39
+ c.ord * hue_step
40
+ end
41
+
42
+ # Spin the wheel!
43
+ hue = char_sum % HUE_WHEEL
44
+
27
45
  "hsl(#{hue}, 40%, 40%)"
28
46
  end
29
47
 
@@ -34,5 +52,18 @@ module Initials
34
52
  def initials
35
53
  name.split(' ')[0, limit].map { |s| s[0].capitalize }.join
36
54
  end
55
+
56
+ private
57
+
58
+ def valid_colors?
59
+ return false unless colors.respond_to?(:to_i)
60
+ return false unless colors > 0
61
+ HUE_WHEEL % colors == 0
62
+ end
63
+
64
+ def valid_size?
65
+ return false unless size.respond_to?(:to_i)
66
+ size.to_i > 0
67
+ end
37
68
  end
38
69
  end
@@ -1,3 +1,3 @@
1
1
  module Initials
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: initials
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Hutterer
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-06-19 00:00:00.000000000 Z
11
+ date: 2021-06-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler