Skip to content

Commit

Permalink
Merge pull request #19 from Dreamersoul/master
Browse files Browse the repository at this point in the history
Users Can Define Custom Palettes
  • Loading branch information
ksz2k authored Feb 5, 2019
2 parents 19196df + e1fd154 commit bb7cbf9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/letter_avatar/avatar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Avatar

# Largest avatar generated, one day when pixel ratio hit 3
# we will need to change this
FULLSIZE = 240
FULLSIZE = 600

FILL_COLOR = 'rgba(255, 255, 255, 0.65)'.freeze

Expand Down
15 changes: 14 additions & 1 deletion lib/letter_avatar/colors.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module LetterAvatar
module Colors
PALETTES = [:google, :iwanthue]
PALETTES = [:google, :iwanthue, :custom]

GOOGLE_COLORS = [
[226, 95, 81], # A
Expand Down Expand Up @@ -274,6 +274,19 @@ def self.with_google(username)
end
end

def self.with_custom(username)
custom_palette = LetterAvatar.custom_palette
custom_palette[Digest::MD5.hexdigest(username)[0...15].to_i(16) % custom_palette.length]
end

def self.valid_custom_palette?(palette)
return false unless palette.is_a?(Array)
return palette.all? do |color|
false unless color.is_a?(Array)
color.all? { |i| i.is_a?(Integer) }
end
end

# Colors form Google Inbox
# https://inbox.google.com
def self.google
Expand Down
12 changes: 11 additions & 1 deletion lib/letter_avatar/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,19 @@ def colors_palette
end

def colors_palette=(v)
@colors_palette = v if v.in?(Colors::PALETTES)
@colors_palette = v if Colors::PALETTES.include?(v)
end

def custom_palette
@custom_palette ||= nil
end

def custom_palette=(v)
@custom_palette = v
raise "Missing Custom Palette, please set config.custom_palette if using :custom" if @custom_palette.nil? && @colors_palette == :custom
raise "Invalid Custom Palette, please update config.custom_palette" unless Colors::valid_custom_palette?(@custom_palette)
end

def weight
@weight ||= 300
end
Expand Down

0 comments on commit bb7cbf9

Please sign in to comment.