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
600
- 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
30 31 32 |
# File 'lib/letter_avatar/avatar.rb', line 30 def cache_path "#{LetterAvatar.cache_base_path || 'public/system'}/letter_avatars/#{VERSION}" end |
.cached_path(identity, size) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/letter_avatar/avatar.rb', line 54 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
86 87 88 89 90 |
# File 'lib/letter_avatar/avatar.rb', line 86 def darken(color, pct) color.map do |n| (n.to_f * pct).to_i end end |
.fullsize_path(identity) ⇒ Object
61 62 63 |
# File 'lib/letter_avatar/avatar.rb', line 61 def fullsize_path(identity) cached_path(identity, FULLSIZE) end |
.generate(username, size, opts = nil) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/letter_avatar/avatar.rb', line 34 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) if !cache || !File.exist?(fullsize) generate_fullsize(identity) end LetterAvatar.resize(fullsize, filename, size, size) if size != FULLSIZE filename end |
.generate_fullsize(identity) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/letter_avatar/avatar.rb', line 65 def generate_fullsize(identity) filename = fullsize_path(identity) LetterAvatar.execute( %W( convert -size #{FULLSIZE}x#{FULLSIZE} xc:#{to_rgb(identity.color)} -pointsize #{LetterAvatar.pointsize} -font #{LetterAvatar.font} -weight #{LetterAvatar.weight} -fill '#{LetterAvatar.fill_color}' -gravity Center -annotate #{LetterAvatar.annotate_position} '#{identity.letter}' '#{filename}' ).join(' ') ) filename end |
.to_rgb(color) ⇒ Object
92 93 94 95 |
# File 'lib/letter_avatar/avatar.rb', line 92 def to_rgb(color) r, g, b = color "'rgb(#{r},#{g},#{b})'" end |