scarf 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4026733a01ae99c008ae69f75a4135e8cd7826a1
4
- data.tar.gz: 1c4f9658d3e320298f623b5448b494f878928e7f
3
+ metadata.gz: d84e27b99920a8976b7b878a7d5206a645a1cb55
4
+ data.tar.gz: 1368f9986144687835bf8540b43a132b516bff29
5
5
  SHA512:
6
- metadata.gz: 27ea92c4698fc7d5bc7aa44d58027b97fd55b014c156eb50e814d2228fcf2583c13a151afa38c3359c76a25325e659396e772212d2421e21097dad1938d49a48
7
- data.tar.gz: 2dc7c53f1c897c2a31c8aecec84a0feff774951c401b9219fe1c5a5cce68c4ea0f88c13fdb5f06dba9c8a747449aa460e8f0c65643fbfdfbee0b3dacf5655f50
6
+ metadata.gz: 316a54c777221bfe66258a8edee4a64b1e9e17ad75b96a4ce5ad167872c2b61f97dd642a0df017b4526cd294aa51afdb5d08cf45fef5bb97e553239f1268ca96
7
+ data.tar.gz: 9dbf9668a12e450f83ac93a8aa944ba8ee2999ae40fd969bf503ae3cdfe1a089357db776de56402377365be4b7aba134387b08d5677271b3248c8486f75a9390
@@ -10,8 +10,8 @@ module Scarf
10
10
  def svg
11
11
  <<~WRAPPER.gsub(/$\s+/, '').strip
12
12
  <svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 100 100">
13
- <circle cx="50" cy="50" r="50" fill="#{fill}" />
14
- <text fill="#FFFFFF" font-size="50" font-family="#{font_family}" x="50" y="50" text-anchor="middle" alignment-baseline="central">#{initials}</text>
13
+ <circle cx="50" cy="50" r="50" fill="#{background_fill}" />
14
+ <text fill="#{foreground_fill}" font-size="50" font-family="#{font_family}" x="50" y="50" text-anchor="middle" alignment-baseline="central">#{initials}</text>
15
15
  </svg>
16
16
  WRAPPER
17
17
  end
@@ -32,9 +32,21 @@ module Scarf
32
32
  end
33
33
  end
34
34
 
35
- # TODO: Need to add a hashing logic to ensure a supplied name always returns the same colour.
36
- def fill
37
- BACKGROUND.sample
35
+ # Returns a deterministic background fill for a given name.
36
+ def background_fill
37
+ @background_fill ||= begin
38
+ digest = "0.#{Digest::MD5.hexdigest(@name).to_i(16).to_s}".to_f
39
+ index = (digest * (BACKGROUND.length - 1)).round
40
+ BACKGROUND[index]
41
+ end
42
+ end
43
+
44
+ # Returns either black or white, optimizing contrast against background color.
45
+ def foreground_fill
46
+ red = background_fill[1..2].to_i(16)
47
+ green = background_fill[3..4].to_i(16)
48
+ blue = background_fill[5..6].to_i(16)
49
+ (red * 0.299 + green * 0.587 + blue * 0.114) > 186 ? '#000000' : '#FFFFFF'
38
50
  end
39
51
 
40
52
  # Upcase first letter of up to first two segments of the supplied name.
@@ -1,3 +1,3 @@
1
1
  module Scarf
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scarf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
- - harigopal
7
+ - Hari Gopal
8
8
  - swdyh
9
9
  autorequire:
10
10
  bindir: bin
@@ -53,9 +53,7 @@ dependencies:
53
53
  - - "~>"
54
54
  - !ruby/object:Gem::Version
55
55
  version: '3.5'
56
- description: |-
57
- A Ruby library for generating initial avatars and identicons, forked from swdyh/quilt
58
- https://github.com/harigopal/quilt
56
+ description: A Ruby library for generating initial avatars and identicons.
59
57
  email: mail@harigopal.in
60
58
  executables: []
61
59
  extensions: []