├── Rakefile ├── Gemfile ├── Roboto-Medium ├── lib ├── letter_avatar │ ├── version.rb │ ├── has_avatar.rb │ ├── avatar_helper.rb │ ├── configuration.rb │ ├── avatar.rb │ └── colors.rb └── letter_avatar.rb ├── letter_avatar.gemspec ├── .gitignore ├── CHANGELOG.md ├── README.md └── LICENSE.txt /Rakefile: -------------------------------------------------------------------------------- 1 | require 'bundler/gem_tasks' 2 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | gemspec 4 | -------------------------------------------------------------------------------- /Roboto-Medium: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ksz2k/letter_avatar/HEAD/Roboto-Medium -------------------------------------------------------------------------------- /lib/letter_avatar/version.rb: -------------------------------------------------------------------------------- 1 | module LetterAvatar 2 | VERSION = '0.3.9' 3 | end 4 | -------------------------------------------------------------------------------- /lib/letter_avatar/has_avatar.rb: -------------------------------------------------------------------------------- 1 | module LetterAvatar 2 | module HasAvatar 3 | def self.included(base) 4 | base.send :include, InstanceMethods 5 | base.extend ClassMethods 6 | end 7 | 8 | module ClassMethods 9 | end 10 | 11 | module InstanceMethods 12 | def self.included(base) 13 | base.extend ClassMethods 14 | end 15 | 16 | def avatar_path(size = 64) 17 | LetterAvatar.generate(name, size) 18 | end 19 | 20 | def avatar_url(size = 64) 21 | LetterAvatar.path_to_url(avatar_path(size)) 22 | end 23 | 24 | end 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /lib/letter_avatar/avatar_helper.rb: -------------------------------------------------------------------------------- 1 | module LetterAvatar 2 | module AvatarHelper 3 | def letter_avatar_for(name, size = 64) 4 | LetterAvatar.generate(name, size) 5 | end 6 | 7 | def letter_avatar_url_for(avatar_path) 8 | LetterAvatar.path_to_url(avatar_path) 9 | end 10 | 11 | def letter_avatar_url(name, size = 64) 12 | letter_avatar_url_for(letter_avatar_for(name, size)) 13 | end 14 | 15 | def letter_avatar_tag(name, size = 64, options = {}) 16 | if defined?(ActionView::Helpers::AssetTagHelper) 17 | extend ActionView::Helpers::AssetTagHelper 18 | image_tag(letter_avatar_url(name, size), options.merge(alt: name)) 19 | else 20 | "\"#{name}\"" 21 | end 22 | end 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /letter_avatar.gemspec: -------------------------------------------------------------------------------- 1 | lib = File.expand_path('../lib', __FILE__) 2 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 3 | require 'letter_avatar/version' 4 | 5 | Gem::Specification.new do |spec| 6 | spec.name = 'letter_avatar' 7 | spec.version = LetterAvatar::VERSION 8 | spec.authors = ['Discourse Developers', 'Krzysiek Szczuka', 'Mateusz Mróz', 'Jason Lee'] 9 | spec.email = ['kszczuka@gmail.com'] 10 | spec.description = "Gem for creating letter avatar from user's name" 11 | spec.summary = 'Create nice initals avatars from your users usernames' 12 | spec.homepage = 'https://github.com/ksz2k/letter_avatar' 13 | spec.license = 'GPL-2.0' 14 | spec.files = Dir.glob('lib/**/*') + %w(README.md CHANGELOG.md Roboto-Medium) 15 | spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } 16 | spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) 17 | spec.require_paths = ['lib'] 18 | end 19 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.rbc 2 | capybara-*.html 3 | .rspec 4 | /log 5 | /tmp 6 | /db/*.sqlite3 7 | /public/system 8 | /coverage/ 9 | /spec/tmp 10 | **.orig 11 | rerun.txt 12 | pickle-email-*.html 13 | 14 | # TODO Comment out these rules if you are OK with secrets being uploaded to the repo 15 | config/initializers/secret_token.rb 16 | config/secrets.yml 17 | 18 | ## Environment normalisation: 19 | /.bundle 20 | /vendor/bundle 21 | 22 | # these should all be checked in to normalise the environment: 23 | # Gemfile.lock, .ruby-version, .ruby-gemset 24 | 25 | # unless supporting rvm < 1.11.0 or doing something fancy, ignore this: 26 | .rvmrc 27 | 28 | # if using bower-rails ignore default bower_components path bower.json files 29 | /vendor/assets/bower_components 30 | *.bowerrc 31 | bower.json 32 | *.gem 33 | *.rbc 34 | .bundle 35 | .config 36 | .yardoc 37 | Gemfile.lock 38 | InstalledFiles 39 | _yardoc 40 | coverage 41 | doc/ 42 | lib/bundler/man 43 | pkg 44 | rdoc 45 | spec/reports 46 | test/tmp 47 | test/version_tmp 48 | tmp 49 | -------------------------------------------------------------------------------- /lib/letter_avatar.rb: -------------------------------------------------------------------------------- 1 | require 'open3' 2 | 3 | require 'letter_avatar/version' 4 | require 'letter_avatar/configuration' 5 | require 'letter_avatar/avatar' 6 | require 'letter_avatar/avatar_helper' 7 | require 'letter_avatar/colors' 8 | 9 | module LetterAvatar 10 | extend LetterAvatar::Configuration 11 | 12 | class ExecutionError < StandardError; end 13 | 14 | def self.setup(&_block) 15 | yield(self) 16 | end 17 | 18 | def self.resize(from, to, width, height) 19 | execute( 20 | # NOTE: ORDER is important! 21 | %W( 22 | convert 23 | #{from} 24 | -background transparent 25 | -gravity center 26 | -thumbnail #{width}x#{height}^ 27 | -extent #{width}x#{height} 28 | -unsharp 2x0.5+0.7+0 29 | -quality 98 30 | #{to} 31 | ).join(' ') 32 | ) 33 | true 34 | rescue => e 35 | false 36 | end 37 | 38 | def self.generate(username, size) 39 | Avatar.generate(username, size) 40 | end 41 | 42 | def self.execute(cmd) 43 | cmd = cmd.join(' ') if cmd.is_a?(Array) 44 | if Gem.win_platform? 45 | cmd.tr!("'", '"') 46 | end 47 | 48 | _stdout_str, err = Open3.capture3(cmd) 49 | if !err.nil? && !err.empty? 50 | raise ExecutionError.new("letter_avatar execution error (when calling '#{cmd}'): '#{err.strip}'") 51 | end 52 | 53 | true 54 | end 55 | 56 | def self.path_to_url(path) 57 | path.to_s.sub('public/', '/') 58 | end 59 | end 60 | -------------------------------------------------------------------------------- /lib/letter_avatar/configuration.rb: -------------------------------------------------------------------------------- 1 | module LetterAvatar 2 | module Configuration 3 | def cache_base_path 4 | @cache_base_path 5 | end 6 | 7 | def cache_base_path=(v) 8 | @cache_base_path = v 9 | end 10 | 11 | def font 12 | @font || Avatar::FONT_FILENAME 13 | end 14 | 15 | def font=(v) 16 | @font = v 17 | end 18 | 19 | def fill_color 20 | @fill_color || Avatar::FILL_COLOR 21 | end 22 | 23 | def fill_color=(v) 24 | @fill_color = v 25 | end 26 | 27 | def colors_palette 28 | @colors_palette ||= :google 29 | end 30 | 31 | def colors_palette=(v) 32 | @colors_palette = v if Colors::PALETTES.include?(v) 33 | end 34 | 35 | def custom_palette 36 | @custom_palette ||= nil 37 | end 38 | 39 | def custom_palette=(v) 40 | @custom_palette = v 41 | raise "Missing Custom Palette, please set config.custom_palette if using :custom" if @custom_palette.nil? && @colors_palette == :custom 42 | raise "Invalid Custom Palette, please update config.custom_palette" unless Colors::valid_custom_palette?(@custom_palette) 43 | end 44 | 45 | def weight 46 | @weight ||= 300 47 | end 48 | 49 | def weight=(v) 50 | @weight = v 51 | end 52 | 53 | def annotate_position 54 | @annotate_position ||= '-0+5' 55 | end 56 | 57 | def annotate_position=(v) 58 | @annotate_position = v 59 | end 60 | 61 | def letters_count 62 | @letters_count ||= 1 63 | end 64 | 65 | def letters_count=(v) 66 | @letters_count = v 67 | end 68 | 69 | def pointsize 70 | @pointsize ||= 140 71 | end 72 | 73 | def pointsize=(v) 74 | @pointsize = v 75 | end 76 | end 77 | end 78 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | 0.3.9 2 | ----- 3 | 4 | - small minor fixes and cleanups. 5 | 6 | 0.3.8 7 | ----- 8 | 9 | - added `LetterAvatar.font` config option. 10 | 11 | 0.3.7 12 | ----- 13 | 14 | - added `LetterAvatar.custom_palette` config option. 15 | - bumped `FULLSIZE` to 600. 16 | 17 | 0.3.6 18 | ----- 19 | 20 | - added `LetterAvatar.letters_count` config option. 21 | - added `LetterAvatar.pointsize` config option. 22 | 23 | 0.3.5 24 | ----- 25 | 26 | - `LetterAvatar::HasAvatar` helper module cleanup 27 | - `LetterAvatar.path_to_url` helper method 28 | 29 | 0.3.4 30 | ----- 31 | 32 | - enhanced `LetterAvatar::HasAvatar` helper module 33 | 34 | 0.3.3 35 | ----- 36 | 37 | - removed `-interpolate` imagemagick option - it's removed in IMv7 38 | 39 | 0.3.2 40 | ----- 41 | 42 | - extend objects with `ActionView::Helpers::AssetTagHelper` 43 | 44 | 0.3.1 45 | ----- 46 | 47 | - added `LetterAvatar.fill_color` config option. 48 | 49 | 0.3.0 50 | ----- 51 | 52 | - Removed `activesupport` dependency. 53 | 54 | 0.2.0 55 | ----- 56 | 57 | - Removed `posix-spawn` dependency, Open3 speed is ok. 58 | 59 | 0.1.11 60 | ------ 61 | 62 | - Windows support. 63 | 64 | 0.1.10 65 | ------ 66 | 67 | - `LetterAvatar::AvatarHelper` module tweaks 68 | - `letter_avatar_url` method, returning avatar proper url 69 | - `letter_avatar_tag` doeas not require `ActionView::Helpers::AssetTagHelper` 70 | 71 | 0.1.9 72 | ----- 73 | 74 | - `LetterAvatar::Configuration` module, `mattr_accessor` (depending on `ActiveSupport`) removed 75 | 76 | fixes [#7](https://github.com/ksz2k/letter_avatar/issues/7) 77 | 78 | 0.1.5 79 | ----- 80 | 81 | - Use [posix-spawn](https://github.com/rtomayko/posix-spawn) to instead of Kernel#exec to get better performance. 82 | 83 | > fork(2) calls slow down as the parent process uses more memory due to the need to copy page tables. 84 | -------------------------------------------------------------------------------- /lib/letter_avatar/avatar.rb: -------------------------------------------------------------------------------- 1 | require 'fileutils' 2 | 3 | module LetterAvatar 4 | class Avatar 5 | # BUMP UP if avatar algorithm changes 6 | VERSION = 2 7 | 8 | # Largest avatar generated, one day when pixel ratio hit 3 9 | # we will need to change this 10 | FULLSIZE = 600 11 | 12 | FILL_COLOR = 'rgba(255, 255, 255, 0.65)'.freeze 13 | 14 | FONT_FILENAME = File.join(File.expand_path('../../', File.dirname(__FILE__)), 'Roboto-Medium') 15 | 16 | class << self 17 | class Identity 18 | attr_accessor :color, :letter 19 | 20 | def self.from_username(username) 21 | identity = new 22 | identity.color = LetterAvatar::Colors.for(username) 23 | letters = username.split(/\s+/).map {|word| word[0]}.join('')[0..LetterAvatar.letters_count - 1] 24 | identity.letter = letters.upcase 25 | 26 | identity 27 | end 28 | end 29 | 30 | def cache_path 31 | "#{LetterAvatar.cache_base_path || 'public/system'}/letter_avatars/#{VERSION}" 32 | end 33 | 34 | def generate(username, size, opts = nil) 35 | identity = Identity.from_username(username) 36 | 37 | cache = true 38 | cache = false if opts && opts[:cache] == false 39 | 40 | size = FULLSIZE if size > FULLSIZE 41 | filename = cached_path(identity, size) 42 | 43 | return filename if cache && File.exist?(filename) 44 | 45 | fullsize = fullsize_path(identity) 46 | if !cache || !File.exist?(fullsize) 47 | generate_fullsize(identity) 48 | end 49 | LetterAvatar.resize(fullsize, filename, size, size) if size != FULLSIZE 50 | 51 | filename 52 | end 53 | 54 | def cached_path(identity, size) 55 | dir = "#{cache_path}/#{identity.letter}/#{identity.color.join('_')}" 56 | FileUtils.mkdir_p(dir) 57 | 58 | "#{dir}/#{size}.png" 59 | end 60 | 61 | def fullsize_path(identity) 62 | cached_path(identity, FULLSIZE) 63 | end 64 | 65 | def generate_fullsize(identity) 66 | filename = fullsize_path(identity) 67 | 68 | LetterAvatar.execute( 69 | %W( 70 | convert 71 | -size #{FULLSIZE}x#{FULLSIZE} 72 | xc:#{to_rgb(identity.color)} 73 | -pointsize #{LetterAvatar.pointsize} 74 | -font #{LetterAvatar.font} 75 | -weight #{LetterAvatar.weight} 76 | -fill '#{LetterAvatar.fill_color}' 77 | -gravity Center 78 | -annotate #{LetterAvatar.annotate_position} '#{identity.letter}' 79 | '#{filename}' 80 | ).join(' ') 81 | ) 82 | 83 | filename 84 | end 85 | 86 | def darken(color, pct) 87 | color.map do |n| 88 | (n.to_f * pct).to_i 89 | end 90 | end 91 | 92 | def to_rgb(color) 93 | r, g, b = color 94 | "'rgb(#{r},#{g},#{b})'" 95 | end 96 | end 97 | end 98 | end 99 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LetterAvatar 2 | 3 | Gem for creating letter avatar from user's name (or any other strong / character). 4 | 5 | Code extracted from [discourse](https://www.discourse.org/) source (thanks guys!) - I needed this functionality in three projects, so here's the gem! :-) 6 | 7 | [![Gem Version](https://badge.fury.io/rb/letter_avatar.svg)](https://badge.fury.io/rb/letter_avatar) 8 | [![Code Climate](https://codeclimate.com/github/ksz2k/letter_avatar/badges/gpa.svg)](https://codeclimate.com/github/ksz2k/letter_avatar) 9 | 10 | ## Examples 11 | 12 | #### Google's Inbox Palette 13 | 14 | 15 | 16 | ## Installation 17 | 18 | System requirements 19 | 20 | ```bash 21 | $ sudo apt-get install -y imagemagick 22 | ``` 23 | 24 | Mac OS X 25 | 26 | ```bash 27 | $ brew install imagemagick ghostscript 28 | ``` 29 | 30 | Add this line to your application's Gemfile: 31 | 32 | ```ruby 33 | gem 'letter_avatar' 34 | ``` 35 | 36 | And then execute: 37 | 38 | ```bash 39 | $ bundle 40 | ``` 41 | 42 | Or install it yourself as: 43 | 44 | ```bash 45 | $ gem install letter_avatar 46 | ``` 47 | 48 | ## Configuration 49 | 50 | ```ruby 51 | LetterAvatar.setup do |config| 52 | config.fill_color = 'rgba(255, 255, 255, 1)' # default is 'rgba(255, 255, 255, 0.65)' 53 | config.cache_base_path = 'public/system/lets' # default is 'public/system' 54 | config.colors_palette = :iwanthue # default is :google 55 | config.weight = 500 # default is 300 56 | config.annotate_position = '-0+10' # default is -0+5 57 | config.letters_count = 2 # default is 1 58 | config.pointsize = 70 # default is 140 59 | end 60 | ``` 61 | 62 | #### Color palette 63 | 64 | We have three color palettes implemented: `iwanthue`, `google` and `custom`. 65 | 66 | Each of them have different colors, but the `iwanthue` also differently calculates the color for specified username. 67 | 68 | The `google` selected will generate the same avatar for both, "Krzysiek" and "ksz2k" usernames given (both of them starts with letter "k"), but `iwanthue` will calculate it's md5 and then selects color, so there's huge chance that these usernames get different colors. 69 | 70 | ##### Custom palette definition 71 | 72 | You can define your own `custom` palette: 73 | 74 | ```ruby 75 | LetterAvatar.setup do |config| 76 | config.colors_palette = :custom 77 | config.custom_palette = [[120, 132, 205], [91, 149, 249], [72, 194, 249], [69, 208, 226]] 78 | end 79 | ``` 80 | 81 | ##### Custom font definition 82 | 83 | You can define your own `custom` palette: 84 | 85 | ```ruby 86 | LetterAvatar.setup do |config| 87 | config.font = File.join(File.expand_path('../../', File.dirname(__FILE__)), 'app/assets/fonts', 'font_name.ext') 88 | end 89 | ``` 90 | 91 | ## Usage 92 | 93 | ```ruby 94 | LetterAvatar.generate 'ksz2k', 200 95 | => "public/system/letter_avatars/2/K/87_178_230/200.png" 96 | ``` 97 | 98 | ### In your controllers / views 99 | 100 | There's also helper for this. To use it, you need: 101 | 102 | * in your helper (eg. `ApplicationHelper`) or controller: 103 | 104 | ```ruby 105 | include LetterAvatar::AvatarHelper 106 | ``` 107 | 108 | * and in your view: 109 | 110 | ```ruby 111 | letter_avatar_for('ksz2k', 200) 112 | => "public/system/letter_avatars/2/K/87_178_230/200.png" 113 | # or 114 | letter_avatar_url('ksz2k', 200) 115 | => "/system/letter_avatars/2/K/87_178_230/200.png" 116 | # or even 117 | letter_avatar_tag('ksz2k', 200, class: 'av') 118 | => "\"ksz2k\"" 119 | ``` 120 | 121 | ### In your model 122 | 123 | Say, you have a model `User` (which must have attribute or method `name`) 124 | 125 | ```ruby 126 | require 'letter_avatar/has_avatar' 127 | 128 | class User 129 | include LetterAvatar::HasAvatar 130 | ... 131 | 132 | def name 133 | 'ksz2k' 134 | end 135 | end 136 | ``` 137 | 138 | Then, in your views you can use: 139 | 140 | ```ruby 141 | @user.avatar_path(200) 142 | => "public/system/letter_avatars/2/K/87_178_230/200.png" 143 | # or 144 | @user.avatar_url(200) 145 | => "/system/letter_avatars/2/K/87_178_230/200.png" 146 | ``` 147 | 148 | ### Way to support non [a-z0-9] charsets 149 | 150 | ```rb 151 | class User 152 | def username_for_avatar 153 | # Translate Chinese hanzi to pinyin 154 | # https://github.com/flyerhzm/chinese_pinyin 155 | Pinyin.t(self.username) 156 | end 157 | end 158 | ``` 159 | 160 | Then you can get right avatar now: 161 | 162 | ```rb 163 | letter_avatar_for(user.username_for_avatar, 200) 164 | # or 165 | letter_avatar_tag(user.username_for_avatar, 200, class: 'av') 166 | ``` 167 | 168 | ## Contributing 169 | 170 | 1. Fork it 171 | 2. Create your feature branch (`git checkout -b my-new-feature`) 172 | 3. Commit your changes (`git commit -am 'Add some feature'`) 173 | 4. Push to the branch (`git push origin my-new-feature`) 174 | 5. Create new Pull Request 175 | -------------------------------------------------------------------------------- /lib/letter_avatar/colors.rb: -------------------------------------------------------------------------------- 1 | require 'digest' 2 | 3 | module LetterAvatar 4 | module Colors 5 | PALETTES = [:google, :iwanthue, :custom] 6 | 7 | GOOGLE_COLORS = [ 8 | [226, 95, 81], # A 9 | [242, 96, 145], # B 10 | [187, 101, 202], # C 11 | [149, 114, 207], # D 12 | [120, 132, 205], # E 13 | [91, 149, 249], # F 14 | [72, 194, 249], # G 15 | [69, 208, 226], # H 16 | [72, 182, 172], # I 17 | [82, 188, 137], # J 18 | [155, 206, 95], # K 19 | [212, 227, 74], # L 20 | [254, 218, 16], # M 21 | [247, 192, 0], # N 22 | [255, 168, 0], # O 23 | [255, 138, 96], # P 24 | [194, 194, 194], # Q 25 | [143, 164, 175], # R 26 | [162, 136, 126], # S 27 | [163, 163, 163], # T 28 | [175, 181, 226], # U 29 | [179, 155, 221], # V 30 | [194, 194, 194], # W 31 | [124, 222, 235], # X 32 | [188, 170, 164], # Y 33 | [173, 214, 125] # Z 34 | ].freeze 35 | 36 | IWANTHUE_COLORS = [ 37 | [198, 125, 40], 38 | [61, 155, 243], 39 | [74, 243, 75], 40 | [238, 89, 166], 41 | [52, 240, 224], 42 | [177, 156, 155], 43 | [240, 120, 145], 44 | [111, 154, 78], 45 | [237, 179, 245], 46 | [237, 101, 95], 47 | [89, 239, 155], 48 | [43, 254, 70], 49 | [163, 212, 245], 50 | [65, 152, 142], 51 | [165, 135, 246], 52 | [181, 166, 38], 53 | [187, 229, 206], 54 | [77, 164, 25], 55 | [179, 246, 101], 56 | [234, 93, 37], 57 | [225, 155, 115], 58 | [142, 140, 188], 59 | [223, 120, 140], 60 | [249, 174, 27], 61 | [244, 117, 225], 62 | [137, 141, 102], 63 | [75, 191, 146], 64 | [188, 239, 142], 65 | [164, 199, 145], 66 | [173, 120, 149], 67 | [59, 195, 89], 68 | [222, 198, 220], 69 | [68, 145, 187], 70 | [236, 204, 179], 71 | [159, 195, 72], 72 | [188, 121, 189], 73 | [166, 160, 85], 74 | [181, 233, 37], 75 | [236, 177, 85], 76 | [121, 147, 160], 77 | [234, 218, 110], 78 | [241, 157, 191], 79 | [62, 200, 234], 80 | [133, 243, 34], 81 | [88, 149, 110], 82 | [59, 228, 248], 83 | [183, 119, 118], 84 | [251, 195, 45], 85 | [113, 196, 122], 86 | [197, 115, 70], 87 | [80, 175, 187], 88 | [103, 231, 238], 89 | [240, 72, 133], 90 | [228, 149, 241], 91 | [180, 188, 159], 92 | [172, 132, 85], 93 | [180, 135, 251], 94 | [236, 194, 58], 95 | [217, 176, 109], 96 | [88, 244, 199], 97 | [186, 157, 239], 98 | [113, 230, 96], 99 | [206, 115, 165], 100 | [244, 178, 163], 101 | [230, 139, 26], 102 | [241, 125, 89], 103 | [83, 160, 66], 104 | [107, 190, 166], 105 | [197, 161, 210], 106 | [198, 203, 245], 107 | [238, 117, 19], 108 | [228, 119, 116], 109 | [131, 156, 41], 110 | [145, 178, 168], 111 | [139, 170, 220], 112 | [233, 95, 125], 113 | [87, 178, 230], 114 | [157, 200, 119], 115 | [237, 140, 76], 116 | [229, 185, 186], 117 | [144, 206, 212], 118 | [236, 209, 158], 119 | [185, 189, 79], 120 | [34, 208, 66], 121 | [84, 238, 129], 122 | [133, 140, 134], 123 | [67, 157, 94], 124 | [168, 179, 25], 125 | [140, 145, 240], 126 | [151, 241, 125], 127 | [67, 162, 107], 128 | [200, 156, 21], 129 | [169, 173, 189], 130 | [226, 116, 189], 131 | [133, 231, 191], 132 | [194, 161, 63], 133 | [241, 77, 99], 134 | [241, 217, 53], 135 | [123, 204, 105], 136 | [210, 201, 119], 137 | [229, 108, 155], 138 | [240, 91, 72], 139 | [187, 115, 210], 140 | [240, 163, 100], 141 | [178, 217, 57], 142 | [179, 135, 116], 143 | [204, 211, 24], 144 | [186, 135, 57], 145 | [223, 176, 135], 146 | [204, 148, 151], 147 | [116, 223, 50], 148 | [95, 195, 46], 149 | [123, 160, 236], 150 | [181, 172, 131], 151 | [142, 220, 202], 152 | [240, 140, 112], 153 | [172, 145, 164], 154 | [228, 124, 45], 155 | [135, 151, 243], 156 | [42, 205, 125], 157 | [192, 233, 116], 158 | [119, 170, 114], 159 | [158, 138, 26], 160 | [73, 190, 183], 161 | [185, 229, 243], 162 | [227, 107, 55], 163 | [196, 205, 202], 164 | [132, 143, 60], 165 | [233, 192, 237], 166 | [62, 150, 220], 167 | [205, 201, 141], 168 | [106, 140, 190], 169 | [161, 131, 205], 170 | [135, 134, 158], 171 | [198, 139, 81], 172 | [115, 171, 32], 173 | [101, 181, 67], 174 | [149, 137, 119], 175 | [37, 142, 183], 176 | [183, 130, 175], 177 | [168, 125, 133], 178 | [124, 142, 87], 179 | [236, 156, 171], 180 | [232, 194, 91], 181 | [219, 200, 69], 182 | [144, 219, 34], 183 | [219, 95, 187], 184 | [145, 154, 217], 185 | [165, 185, 100], 186 | [127, 238, 163], 187 | [224, 178, 198], 188 | [119, 153, 120], 189 | [124, 212, 92], 190 | [172, 161, 105], 191 | [231, 155, 135], 192 | [157, 132, 101], 193 | [122, 185, 146], 194 | [53, 166, 51], 195 | [70, 163, 90], 196 | [150, 190, 213], 197 | [210, 107, 60], 198 | [166, 152, 185], 199 | [159, 194, 159], 200 | [39, 141, 222], 201 | [202, 176, 161], 202 | [95, 140, 229], 203 | [168, 142, 87], 204 | [93, 170, 203], 205 | [159, 142, 54], 206 | [14, 168, 39], 207 | [94, 150, 149], 208 | [187, 206, 136], 209 | [157, 224, 166], 210 | [235, 158, 208], 211 | [109, 232, 216], 212 | [141, 201, 87], 213 | [208, 124, 118], 214 | [142, 125, 214], 215 | [19, 237, 174], 216 | [72, 219, 41], 217 | [234, 102, 111], 218 | [168, 142, 79], 219 | [188, 135, 35], 220 | [95, 155, 143], 221 | [148, 173, 116], 222 | [223, 112, 95], 223 | [228, 128, 236], 224 | [206, 114, 54], 225 | [195, 119, 88], 226 | [235, 140, 94], 227 | [235, 202, 125], 228 | [233, 155, 153], 229 | [214, 214, 238], 230 | [246, 200, 35], 231 | [151, 125, 171], 232 | [132, 145, 172], 233 | [131, 142, 118], 234 | [199, 126, 150], 235 | [61, 162, 123], 236 | [58, 176, 151], 237 | [215, 141, 69], 238 | [225, 154, 220], 239 | [220, 77, 167], 240 | [233, 161, 64], 241 | [130, 221, 137], 242 | [81, 191, 129], 243 | [169, 162, 140], 244 | [174, 177, 222], 245 | [236, 174, 47], 246 | [233, 188, 180], 247 | [69, 222, 172], 248 | [71, 232, 93], 249 | [118, 211, 238], 250 | [157, 224, 83], 251 | [218, 105, 73], 252 | [126, 169, 36] 253 | ].freeze 254 | 255 | def self.for(username) 256 | public_send("with_#{LetterAvatar.colors_palette}", username) 257 | end 258 | 259 | def self.with_iwanthue(username) 260 | iwanthue[ 261 | ::Digest::MD5.hexdigest(username)[0...15].to_i(16) % iwanthue.length 262 | ] 263 | end 264 | 265 | def self.with_google(username) 266 | char = username[0].upcase 267 | 268 | if /[A-Z]/ =~ char 269 | # 65 is 'A' ord 270 | idx = char.ord - 65 271 | google[idx] 272 | elsif /[\d]/ =~ char 273 | google[char.to_i] 274 | else 275 | google[::Digest::MD5.hexdigest(username)[0...15].to_i(16) % google.length] 276 | end 277 | end 278 | 279 | def self.with_custom(username) 280 | custom_palette = LetterAvatar.custom_palette 281 | custom_palette[::Digest::MD5.hexdigest(username)[0...15].to_i(16) % custom_palette.length] 282 | end 283 | 284 | def self.valid_custom_palette?(palette) 285 | return false unless palette.is_a?(Array) 286 | return palette.all? do |color| 287 | false unless color.is_a?(Array) 288 | color.all? { |i| i.is_a?(Integer) } 289 | end 290 | end 291 | 292 | # Colors form Google Inbox 293 | # https://inbox.google.com 294 | def self.google 295 | GOOGLE_COLORS 296 | end 297 | 298 | # palette of optimally disctinct colors 299 | # cf. http://tools.medialab.sciences-po.fr/iwanthue/index.php 300 | # parameters used: 301 | # - H: 0 - 360 302 | # - C: 0 - 2 303 | # - L: 0.75 - 1.5 304 | def self.iwanthue 305 | IWANTHUE_COLORS 306 | end 307 | end 308 | end 309 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | --------------------------------------------------------------------------------