Class: LetterAvatar::Avatar
- Inherits:
-
Object
- Object
- LetterAvatar::Avatar
- Defined in:
- lib/letter_avatar/avatar.rb
Defined Under Namespace
Classes: Identity
Constant Summary collapse
- VERSION =
BUMP UP if avatar algorithm changes
2
- FULLSIZE =
Largest avatar generated, one day when pixel ratio hit 3 we will need to change this
240
- FILL_COLOR =
'rgba(255, 255, 255, 0.65)'.freeze
- FONT_FILENAME =
File.join(File.('../../', File.dirname(__FILE__)), 'Roboto-Medium')
Class Method Summary collapse
- .cache_path ⇒ Object
- .cached_path(identity, size) ⇒ Object
- .darken(color, pct) ⇒ Object
- .fullsize_path(identity) ⇒ Object
- .generate(username, size, opts = nil) ⇒ Object
- .generate_fullsize(identity) ⇒ Object
- .to_rgb(color) ⇒ Object
Class Method Details
.cache_path ⇒ Object
27 28 29 |
# File 'lib/letter_avatar/avatar.rb', line 27 def cache_path "#{LetterAvatar.cache_base_path || 'public/system'}/letter_avatars/#{VERSION}" end |
.cached_path(identity, size) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/letter_avatar/avatar.rb', line 49 def cached_path(identity, size) dir = "#{cache_path}/#{identity.letter}/#{identity.color.join('_')}" FileUtils.mkdir_p(dir) "#{dir}/#{size}.png" end |
.darken(color, pct) ⇒ Object
81 82 83 84 85 |
# File 'lib/letter_avatar/avatar.rb', line 81 def darken(color, pct) color.map do |n| (n.to_f * pct).to_i end end |
.fullsize_path(identity) ⇒ Object
56 57 58 |
# File 'lib/letter_avatar/avatar.rb', line 56 def fullsize_path(identity) cached_path(identity, FULLSIZE) end |
.generate(username, size, opts = nil) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/letter_avatar/avatar.rb', line 31 def generate(username, size, opts = nil) identity = Identity.from_username(username) cache = true cache = false if opts && opts[:cache] == false size = FULLSIZE if size > FULLSIZE filename = cached_path(identity, size) return filename if cache && File.exist?(filename) fullsize = fullsize_path(identity) generate_fullsize(identity) if !cache || !File.exist?(fullsize) LetterAvatar.resize(fullsize, filename, size, size) filename end |
.generate_fullsize(identity) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/letter_avatar/avatar.rb', line 60 def generate_fullsize(identity) filename = fullsize_path(identity) LetterAvatar.execute( %W( convert -size #{FULLSIZE}x#{FULLSIZE} xc:#{to_rgb(identity.color)} -pointsize 140 -font #{FONT_FILENAME} -weight #{LetterAvatar.weight} -fill '#{LetterAvatar.fill_color}' -gravity Center -annotate #{LetterAvatar.annotate_position} '#{identity.letter}' '#{filename}' ).join(' ') ) filename end |
.to_rgb(color) ⇒ Object
87 88 89 90 |
# File 'lib/letter_avatar/avatar.rb', line 87 def to_rgb(color) r, g, b = color "'rgb(#{r},#{g},#{b})'" end |