Commit bb7cbf9547d0d9162c956d7df2516c6020df919e
Committed by
GitHub
Exists in
master
Merge pull request #19 from Dreamersoul/master
Users Can Define Custom Palettes
Showing
3 changed files
with
26 additions
and
3 deletions
Show diff stats
lib/letter_avatar/avatar.rb
@@ -5,7 +5,7 @@ module LetterAvatar | @@ -5,7 +5,7 @@ module LetterAvatar | ||
5 | 5 | ||
6 | # Largest avatar generated, one day when pixel ratio hit 3 | 6 | # Largest avatar generated, one day when pixel ratio hit 3 |
7 | # we will need to change this | 7 | # we will need to change this |
8 | - FULLSIZE = 240 | 8 | + FULLSIZE = 600 |
9 | 9 | ||
10 | FILL_COLOR = 'rgba(255, 255, 255, 0.65)'.freeze | 10 | FILL_COLOR = 'rgba(255, 255, 255, 0.65)'.freeze |
11 | 11 |
lib/letter_avatar/colors.rb
1 | module LetterAvatar | 1 | module LetterAvatar |
2 | module Colors | 2 | module Colors |
3 | - PALETTES = [:google, :iwanthue] | 3 | + PALETTES = [:google, :iwanthue, :custom] |
4 | 4 | ||
5 | GOOGLE_COLORS = [ | 5 | GOOGLE_COLORS = [ |
6 | [226, 95, 81], # A | 6 | [226, 95, 81], # A |
@@ -274,6 +274,19 @@ module LetterAvatar | @@ -274,6 +274,19 @@ module LetterAvatar | ||
274 | end | 274 | end |
275 | end | 275 | end |
276 | 276 | ||
277 | + def self.with_custom(username) | ||
278 | + custom_palette = LetterAvatar.custom_palette | ||
279 | + custom_palette[Digest::MD5.hexdigest(username)[0...15].to_i(16) % custom_palette.length] | ||
280 | + end | ||
281 | + | ||
282 | + def self.valid_custom_palette?(palette) | ||
283 | + return false unless palette.is_a?(Array) | ||
284 | + return palette.all? do |color| | ||
285 | + false unless color.is_a?(Array) | ||
286 | + color.all? { |i| i.is_a?(Integer) } | ||
287 | + end | ||
288 | + end | ||
289 | + | ||
277 | # Colors form Google Inbox | 290 | # Colors form Google Inbox |
278 | # https://inbox.google.com | 291 | # https://inbox.google.com |
279 | def self.google | 292 | def self.google |
lib/letter_avatar/configuration.rb
@@ -21,9 +21,19 @@ module LetterAvatar | @@ -21,9 +21,19 @@ module LetterAvatar | ||
21 | end | 21 | end |
22 | 22 | ||
23 | def colors_palette=(v) | 23 | def colors_palette=(v) |
24 | - @colors_palette = v if v.in?(Colors::PALETTES) | 24 | + @colors_palette = v if Colors::PALETTES.include?(v) |
25 | end | 25 | end |
26 | 26 | ||
27 | + def custom_palette | ||
28 | + @custom_palette ||= nil | ||
29 | + end | ||
30 | + | ||
31 | + def custom_palette=(v) | ||
32 | + @custom_palette = v | ||
33 | + raise "Missing Custom Palette, please set config.custom_palette if using :custom" if @custom_palette.nil? && @colors_palette == :custom | ||
34 | + raise "Invalid Custom Palette, please update config.custom_palette" unless Colors::valid_custom_palette?(@custom_palette) | ||
35 | + end | ||
36 | + | ||
27 | def weight | 37 | def weight |
28 | @weight ||= 300 | 38 | @weight ||= 300 |
29 | end | 39 | end |