letter_avatar 0.3.5 → 0.3.9

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
- SHA1:
3
- metadata.gz: f648e3d3e303081858371e1bbe2e164b76b68085
4
- data.tar.gz: 5b55acfe7f9f5df46eea21b8d0b3867485950102
2
+ SHA256:
3
+ metadata.gz: b6dacffd37b1ed6ffe0bd73e45b53e78633bad53c59ab6efd0aa130e468fde8c
4
+ data.tar.gz: 73a8007813254f22ab63d4c4146fd4ee864f7f3d391fc6d3b268112ad3f521b9
5
5
  SHA512:
6
- metadata.gz: c3d835fedd766bda7395275f8e936703ffa323f2a6f0dc3c5050e1a6c24304cdd718c827df43af2ef0afdfbfa6d45d49bf0ba61d9e963d12ea173fcccf8bfaba
7
- data.tar.gz: 4db45918f3221758b818cecc57e008c299d1de98ce0ea766c1a67341e0f265baab4f1950d462c9dc4c4db9b7bdf04d129c45767f68a2624f5fbb1a7044ead5a9
6
+ metadata.gz: 06a5a887dfcba06b5e1832127ca346ab8f95ff425dca8cb366c1dcab7bab967d3d47112c46f3ddc8c8915ea037a33d8bf96b80fd1163b2f4b69317d9e4c769b4
7
+ data.tar.gz: 4152a78ea6534e2391434a3923932e6548b59228ecf515e131220e1ea390ce20691122654310a634018a58f99d15b699321f8de900dcf25e651272753258f082
data/CHANGELOG.md CHANGED
@@ -1,3 +1,20 @@
1
+ 0.3.8
2
+ -----
3
+
4
+ - added `LetterAvatar.font` config option.
5
+
6
+ 0.3.7
7
+ -----
8
+
9
+ - added `LetterAvatar.custom_palette` config option.
10
+ - bumped `FULLSIZE` to 600.
11
+
12
+ 0.3.6
13
+ -----
14
+
15
+ - added `LetterAvatar.letters_count` config option.
16
+ - added `LetterAvatar.pointsize` config option.
17
+
1
18
  0.3.5
2
19
  -----
3
20
 
data/README.md CHANGED
@@ -4,6 +4,7 @@ Gem for creating letter avatar from user's name (or any other strong / character
4
4
 
5
5
  Code extracted from [discourse](https://www.discourse.org/) source (thanks guys!) - I needed this functionality in three projects, so here's the gem! :-)
6
6
 
7
+ [![Gem Version](https://badge.fury.io/rb/letter_avatar.svg)](https://badge.fury.io/rb/letter_avatar)
7
8
  [![Code Climate](https://codeclimate.com/github/ksz2k/letter_avatar/badges/gpa.svg)](https://codeclimate.com/github/ksz2k/letter_avatar)
8
9
 
9
10
  ## Examples
@@ -53,17 +54,40 @@ LetterAvatar.setup do |config|
53
54
  config.colors_palette = :iwanthue # default is :google
54
55
  config.weight = 500 # default is 300
55
56
  config.annotate_position = '-0+10' # default is -0+5
57
+ config.letters_count = 2 # default is 1
58
+ config.pointsize = 70 # default is 140
56
59
  end
57
60
  ```
58
61
 
59
62
  #### Color palette
60
63
 
61
- We have two color palettes implemented: `iwanthue` and `google`.
64
+ We have three color palettes implemented: `iwanthue`, `google` and `custom`.
62
65
 
63
66
  Each of them have different colors, but the `iwanthue` also differently calculates the color for specified username.
64
67
 
65
68
  The `google` selected will generate the same avatar for both, "Krzysiek" and "ksz2k" usernames given (both of them starts with letter "k"), but `iwanthue` will calculate it's md5 and then selects color, so there's huge chance that these usernames get different colors.
66
69
 
70
+ ##### Custom palette definition
71
+
72
+ You can define your own `custom` palette:
73
+
74
+ ```ruby
75
+ LetterAvatar.setup do |config|
76
+ config.colors_palette = :custom
77
+ config.custom_palette = [[120, 132, 205], [91, 149, 249], [72, 194, 249], [69, 208, 226]]
78
+ end
79
+ ```
80
+
81
+ ##### Custom font definition
82
+
83
+ You can define your own `custom` palette:
84
+
85
+ ```ruby
86
+ LetterAvatar.setup do |config|
87
+ config.font = File.join(File.expand_path('../../', File.dirname(__FILE__)), 'app/assets/fonts', 'font_name.ext')
88
+ end
89
+ ```
90
+
67
91
  ## Usage
68
92
 
69
93
  ```ruby
@@ -99,6 +123,8 @@ There's also helper for this. To use it, you need:
99
123
  Say, you have a model `User` (which must have attribute or method `name`)
100
124
 
101
125
  ```ruby
126
+ require 'letter_avatar/has_avatar'
127
+
102
128
  class User
103
129
  include LetterAvatar::HasAvatar
104
130
  ...
@@ -1,3 +1,5 @@
1
+ require 'fileutils'
2
+
1
3
  module LetterAvatar
2
4
  class Avatar
3
5
  # BUMP UP if avatar algorithm changes
@@ -5,7 +7,7 @@ module LetterAvatar
5
7
 
6
8
  # Largest avatar generated, one day when pixel ratio hit 3
7
9
  # we will need to change this
8
- FULLSIZE = 240
10
+ FULLSIZE = 600
9
11
 
10
12
  FILL_COLOR = 'rgba(255, 255, 255, 0.65)'.freeze
11
13
 
@@ -18,7 +20,8 @@ module LetterAvatar
18
20
  def self.from_username(username)
19
21
  identity = new
20
22
  identity.color = LetterAvatar::Colors.for(username)
21
- identity.letter = username[0].upcase
23
+ letters = username.split(/\s+/).map {|word| word[0]}.join('')[0..LetterAvatar.letters_count - 1]
24
+ identity.letter = letters.upcase
22
25
 
23
26
  identity
24
27
  end
@@ -40,9 +43,11 @@ module LetterAvatar
40
43
  return filename if cache && File.exist?(filename)
41
44
 
42
45
  fullsize = fullsize_path(identity)
43
- generate_fullsize(identity) if !cache || !File.exist?(fullsize)
46
+ if !cache || !File.exist?(fullsize)
47
+ generate_fullsize(identity)
48
+ end
49
+ LetterAvatar.resize(fullsize, filename, size, size) if size != FULLSIZE
44
50
 
45
- LetterAvatar.resize(fullsize, filename, size, size)
46
51
  filename
47
52
  end
48
53
 
@@ -65,8 +70,8 @@ module LetterAvatar
65
70
  convert
66
71
  -size #{FULLSIZE}x#{FULLSIZE}
67
72
  xc:#{to_rgb(identity.color)}
68
- -pointsize 140
69
- -font #{FONT_FILENAME}
73
+ -pointsize #{LetterAvatar.pointsize}
74
+ -font #{LetterAvatar.font}
70
75
  -weight #{LetterAvatar.weight}
71
76
  -fill '#{LetterAvatar.fill_color}'
72
77
  -gravity Center
@@ -1,6 +1,8 @@
1
+ require 'digest'
2
+
1
3
  module LetterAvatar
2
4
  module Colors
3
- PALETTES = [:google, :iwanthue]
5
+ PALETTES = [:google, :iwanthue, :custom]
4
6
 
5
7
  GOOGLE_COLORS = [
6
8
  [226, 95, 81], # A
@@ -256,7 +258,7 @@ module LetterAvatar
256
258
 
257
259
  def self.with_iwanthue(username)
258
260
  iwanthue[
259
- Digest::MD5.hexdigest(username)[0...15].to_i(16) % iwanthue.length
261
+ ::Digest::MD5.hexdigest(username)[0...15].to_i(16) % iwanthue.length
260
262
  ]
261
263
  end
262
264
 
@@ -270,10 +272,23 @@ module LetterAvatar
270
272
  elsif /[\d]/ =~ char
271
273
  google[char.to_i]
272
274
  else
273
- google[Digest::MD5.hexdigest(username)[0...15].to_i(16) % google.length]
275
+ google[::Digest::MD5.hexdigest(username)[0...15].to_i(16) % google.length]
274
276
  end
275
277
  end
276
278
 
279
+ def self.with_custom(username)
280
+ custom_palette = LetterAvatar.custom_palette
281
+ custom_palette[::Digest::MD5.hexdigest(username)[0...15].to_i(16) % custom_palette.length]
282
+ end
283
+
284
+ def self.valid_custom_palette?(palette)
285
+ return false unless palette.is_a?(Array)
286
+ return palette.all? do |color|
287
+ false unless color.is_a?(Array)
288
+ color.all? { |i| i.is_a?(Integer) }
289
+ end
290
+ end
291
+
277
292
  # Colors form Google Inbox
278
293
  # https://inbox.google.com
279
294
  def self.google
@@ -8,6 +8,14 @@ module LetterAvatar
8
8
  @cache_base_path = v
9
9
  end
10
10
 
11
+ def font
12
+ @font || Avatar::FONT_FILENAME
13
+ end
14
+
15
+ def font=(v)
16
+ @font = v
17
+ end
18
+
11
19
  def fill_color
12
20
  @fill_color || Avatar::FILL_COLOR
13
21
  end
@@ -21,9 +29,19 @@ module LetterAvatar
21
29
  end
22
30
 
23
31
  def colors_palette=(v)
24
- @colors_palette = v if v.in?(Colors::PALETTES)
32
+ @colors_palette = v if Colors::PALETTES.include?(v)
25
33
  end
26
34
 
35
+ def custom_palette
36
+ @custom_palette ||= nil
37
+ end
38
+
39
+ def custom_palette=(v)
40
+ @custom_palette = v
41
+ raise "Missing Custom Palette, please set config.custom_palette if using :custom" if @custom_palette.nil? && @colors_palette == :custom
42
+ raise "Invalid Custom Palette, please update config.custom_palette" unless Colors::valid_custom_palette?(@custom_palette)
43
+ end
44
+
27
45
  def weight
28
46
  @weight ||= 300
29
47
  end
@@ -39,5 +57,21 @@ module LetterAvatar
39
57
  def annotate_position=(v)
40
58
  @annotate_position = v
41
59
  end
60
+
61
+ def letters_count
62
+ @letters_count ||= 1
63
+ end
64
+
65
+ def letters_count=(v)
66
+ @letters_count = v
67
+ end
68
+
69
+ def pointsize
70
+ @pointsize ||= 140
71
+ end
72
+
73
+ def pointsize=(v)
74
+ @pointsize = v
75
+ end
42
76
  end
43
77
  end
@@ -1,3 +1,3 @@
1
1
  module LetterAvatar
2
- VERSION = '0.3.5'
2
+ VERSION = '0.3.9'
3
3
  end
data/lib/letter_avatar.rb CHANGED
@@ -31,7 +31,7 @@ module LetterAvatar
31
31
  ).join(' ')
32
32
  )
33
33
  true
34
- rescue
34
+ rescue => e
35
35
  false
36
36
  end
37
37
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: letter_avatar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.5
4
+ version: 0.3.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Discourse Developers
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2017-03-29 00:00:00.000000000 Z
14
+ date: 2021-09-06 00:00:00.000000000 Z
15
15
  dependencies: []
16
16
  description: Gem for creating letter avatar from user's name
17
17
  email:
@@ -49,8 +49,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
49
49
  - !ruby/object:Gem::Version
50
50
  version: '0'
51
51
  requirements: []
52
- rubyforge_project:
53
- rubygems_version: 2.5.1
52
+ rubygems_version: 3.1.3
54
53
  signing_key:
55
54
  specification_version: 4
56
55
  summary: Create nice initals avatars from your users usernames