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
lib/letter_avatar/colors.rb
1 | 1 | module LetterAvatar |
2 | 2 | module Colors |
3 | - PALETTES = [:google, :iwanthue] | |
3 | + PALETTES = [:google, :iwanthue, :custom] | |
4 | 4 | |
5 | 5 | GOOGLE_COLORS = [ |
6 | 6 | [226, 95, 81], # A |
... | ... | @@ -274,6 +274,19 @@ module LetterAvatar |
274 | 274 | end |
275 | 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 | 290 | # Colors form Google Inbox |
278 | 291 | # https://inbox.google.com |
279 | 292 | def self.google | ... | ... |
lib/letter_avatar/configuration.rb
... | ... | @@ -21,9 +21,19 @@ module LetterAvatar |
21 | 21 | end |
22 | 22 | |
23 | 23 | def colors_palette=(v) |
24 | - @colors_palette = v if v.in?(Colors::PALETTES) | |
24 | + @colors_palette = v if Colors::PALETTES.include?(v) | |
25 | 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 | 37 | def weight |
28 | 38 | @weight ||= 300 |
29 | 39 | end | ... | ... |