FEATURE: new letter avatars algorithm

This commit is contained in:
Régis Hanol 2015-06-02 16:33:18 +02:00
parent fa963e8f38
commit 1067cde0d0
1 changed files with 10 additions and 19 deletions

View File

@ -1,13 +1,13 @@
class LetterAvatar class LetterAvatar
# BUMP UP if avatar algorithm changes # BUMP UP if avatar algorithm changes
VERSION = 3 VERSION = 4
# Largest avatar generated, one day when pixel ratio hit 3 # CHANGE these values to support more pixel ratios
# we will need to change this FULLSIZE = 120 * 3
FULLSIZE = 240 POINTSIZE = 280
class<<self class << self
class Identity class Identity
attr_accessor :color, :letter attr_accessor :color, :letter
@ -46,6 +46,7 @@ class LetterAvatar
generate_fullsize(identity) if !cache || !File.exists?(fullsize) generate_fullsize(identity) if !cache || !File.exists?(fullsize)
OptimizedImage.resize(fullsize, filename, size, size) OptimizedImage.resize(fullsize, filename, size, size)
filename filename
end end
end end
@ -53,7 +54,6 @@ class LetterAvatar
def cached_path(identity, size) def cached_path(identity, size)
dir = "#{cache_path}/#{identity.letter}/#{identity.color.join("_")}" dir = "#{cache_path}/#{identity.letter}/#{identity.color.join("_")}"
FileUtils.mkdir_p(dir) FileUtils.mkdir_p(dir)
"#{dir}/#{size}.png" "#{dir}/#{size}.png"
end end
@ -66,18 +66,15 @@ class LetterAvatar
letter = identity.letter letter = identity.letter
filename = fullsize_path(identity) filename = fullsize_path(identity)
stroke = darken(color, 0.8)
instructions = %W{ instructions = %W{
-size #{FULLSIZE}x#{FULLSIZE} -size #{FULLSIZE}x#{FULLSIZE}
xc:#{to_rgb(color)} xc:#{to_rgb(color)}
-pointsize 180 -pointsize #{POINTSIZE}
-fill white -fill 'rgba(255,255,255,0.8)'
-gravity Center
-font 'Helvetica' -font 'Helvetica'
-stroke #{to_rgb(stroke)} -gravity Center
-strokewidth 2 -annotate -0+26 '#{letter}'
-annotate -0+20 '#{letter}'
-depth 8 -depth 8
'#{filename}' '#{filename}'
} }
@ -89,12 +86,6 @@ class LetterAvatar
filename filename
end end
def darken(color,pct)
color.map do |n|
(n.to_f * pct).to_i
end
end
def to_rgb(color) def to_rgb(color)
r,g,b = color r,g,b = color
"'rgb(#{r},#{g},#{b})'" "'rgb(#{r},#{g},#{b})'"