├── .rspec ├── lib ├── initial_avatar │ ├── version.rb │ ├── configuration.rb │ └── avatar.rb └── initial_avatar.rb ├── Rakefile ├── bin ├── setup └── console ├── spec ├── initial_avatar_spec.rb └── spec_helper.rb ├── .gitignore ├── Gemfile ├── CHANGELOG.md ├── .github └── workflows │ └── ci.yml ├── LICENSE.txt ├── initial_avatar.gemspec ├── README.md └── CODE_OF_CONDUCT.md /.rspec: -------------------------------------------------------------------------------- 1 | --format documentation 2 | --color 3 | --require spec_helper 4 | -------------------------------------------------------------------------------- /lib/initial_avatar/version.rb: -------------------------------------------------------------------------------- 1 | module InitialAvatar 2 | VERSION = '0.2.2' 3 | end 4 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | require "rspec/core/rake_task" 3 | 4 | RSpec::Core::RakeTask.new(:spec) 5 | 6 | task :default => :spec 7 | -------------------------------------------------------------------------------- /bin/setup: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -euo pipefail 3 | IFS=$'\n\t' 4 | set -vx 5 | 6 | bundle install 7 | 8 | # Do any other automated setup that you need to do here 9 | -------------------------------------------------------------------------------- /spec/initial_avatar_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe InitialAvatar do 2 | it 'has a version number' do 3 | expect(InitialAvatar::VERSION).not_to be nil 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.bundle/ 2 | /.yardoc 3 | /_yardoc/ 4 | /coverage/ 5 | /doc/ 6 | /pkg/ 7 | /spec/reports/ 8 | /tmp/ 9 | 10 | # rspec failure tracking 11 | .rspec_status 12 | 13 | Gemfile.lock 14 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | 3 | git_source(:github) {|repo_name| "https://github.com/#{repo_name}" } 4 | 5 | # Specify your gem's dependencies in initial_avatar.gemspec 6 | gemspec 7 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 0.2.2 2 | 3 | - Add `require 'base64'` (#1) 4 | 5 | ## 0.2.1 6 | 7 | - Update dependency 8 | 9 | ## 0.2.0 10 | 11 | - Remove size from style attribute 12 | 13 | ## 0.1.1 14 | 15 | - Add seed option 16 | 17 | ## 0.1.0 18 | 19 | - First Release 20 | -------------------------------------------------------------------------------- /bin/console: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require "bundler/setup" 4 | require "initial_avatar" 5 | 6 | # You can add fixtures and/or initialization code here to make experimenting 7 | # with your gem easier. You can also use a different console, if you like. 8 | 9 | # (If you use this, don't forget to add pry to your Gemfile!) 10 | # require "pry" 11 | # Pry.start 12 | 13 | require "irb" 14 | IRB.start(__FILE__) 15 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require "bundler/setup" 2 | require "initial_avatar" 3 | 4 | RSpec.configure do |config| 5 | # Enable flags like --only-failures and --next-failure 6 | config.example_status_persistence_file_path = ".rspec_status" 7 | 8 | # Disable RSpec exposing methods globally on `Module` and `main` 9 | config.disable_monkey_patching! 10 | 11 | config.expect_with :rspec do |c| 12 | c.syntax = :expect 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /lib/initial_avatar.rb: -------------------------------------------------------------------------------- 1 | require 'initial_avatar/version' 2 | require 'initial_avatar/avatar' 3 | require 'initial_avatar/configuration' 4 | 5 | module InitialAvatar 6 | class << self 7 | def configure 8 | yield(configuration) 9 | end 10 | 11 | def configuration 12 | @configuration ||= Configuration.new 13 | end 14 | 15 | def avatar_data_uri(name, options = {}) 16 | Avatar.new(name, options).data_uri 17 | end 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | test: 7 | 8 | runs-on: ubuntu-latest 9 | 10 | strategy: 11 | fail-fast: false 12 | matrix: 13 | ruby: ["2.5", "2.6", "2.7", "3.0", "3.1", ruby-head, jruby-9.2, jruby-9.3, jruby-head] 14 | 15 | steps: 16 | - uses: actions/checkout@v2 17 | - name: Set up Ruby 18 | uses: ruby/setup-ruby@v1 19 | with: 20 | bundler-cache: true # 'bundle install' and cache gems 21 | ruby-version: ${{ matrix.ruby }} 22 | - name: Run tests 23 | run: bundle exec rake 24 | -------------------------------------------------------------------------------- /lib/initial_avatar/configuration.rb: -------------------------------------------------------------------------------- 1 | module InitialAvatar 2 | class Configuration 3 | OPTIONS = %i[text_color size font_weight font_family seed].freeze 4 | 5 | attr_accessor :colors 6 | attr_accessor :text_color 7 | attr_accessor :size 8 | attr_accessor :font_weight 9 | attr_accessor :font_family 10 | attr_accessor :seed 11 | 12 | def initialize 13 | @colors = %w[ 14 | #1abc9c #16a085 #f1c40f #f39c12 #2ecc71 #27ae60 #e67e22 #d35400 #3498db 15 | #2980b9 #e74c3c #c0392b #9b59b6 #8e44ad #bdc3c7 #34495e #2c3e50 #95a5a6 16 | #7f8c8d #ec87bf #d870ad #f69785 #9ba37e #b49255 #b49255 #a94136 17 | ] 18 | @text_color = '#ffffff' 19 | @size = 100 20 | @font_weight = 400 21 | @font_family = 'HelveticaNeue-Light,Helvetica Neue Light,Helvetica Neue,Helvetica, Arial,Lucida Grande, sans-serif' 22 | @seed = 0 23 | end 24 | 25 | def default_options 26 | OPTIONS.map { |key| [key, public_send(key)] }.to_h 27 | end 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2018 aki 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /lib/initial_avatar/avatar.rb: -------------------------------------------------------------------------------- 1 | require 'base64' 2 | 3 | module InitialAvatar 4 | class Avatar 5 | attr_reader :text 6 | attr_reader :opts 7 | 8 | def initialize(text, options = {}) 9 | @text = text.upcase 10 | @opts = InitialAvatar.configuration.default_options.merge(options) 11 | end 12 | 13 | def svg_tag 14 | <<~SVG 15 | 16 | #{text} 17 | 18 | SVG 19 | end 20 | 21 | def data_uri 22 | "data:image/svg+xml;base64,#{Base64.strict_encode64(svg_tag)}" 23 | end 24 | 25 | private 26 | 27 | def color 28 | return opts[:color] if opts[:color] 29 | index = (text.ord + opts[:seed]) % InitialAvatar.configuration.colors.length 30 | InitialAvatar.configuration.colors[index] 31 | end 32 | 33 | def font_size 34 | opts[:font_size] || opts[:size] / 2 35 | end 36 | end 37 | end 38 | -------------------------------------------------------------------------------- /initial_avatar.gemspec: -------------------------------------------------------------------------------- 1 | 2 | lib = File.expand_path("../lib", __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require "initial_avatar/version" 5 | 6 | Gem::Specification.new do |spec| 7 | spec.name = "initial_avatar" 8 | spec.version = InitialAvatar::VERSION 9 | spec.authors = ["aki77"] 10 | spec.email = ["aki77@users.noreply.github.com"] 11 | 12 | spec.summary = %q{Create Gmail like text avatars.} 13 | spec.description = %q{Create Gmail like text avatars.} 14 | spec.homepage = "https://github.com/aki77/initial_avatar" 15 | spec.license = "MIT" 16 | 17 | # Specify which files should be added to the gem when it is released. 18 | # The `git ls-files -z` loads the files in the RubyGem that have been added into git. 19 | spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do 20 | `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } 21 | end 22 | spec.bindir = "exe" 23 | spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } 24 | spec.require_paths = ["lib"] 25 | 26 | spec.add_development_dependency "bundler" 27 | spec.add_development_dependency "rake" 28 | spec.add_development_dependency "rspec" 29 | end 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Initial Avatar 2 | 3 | Create Gmail like text avatars. 4 | 5 | ![](https://cldup.com/IbyArDI6fV.png) 6 | 7 | ## Installation 8 | 9 | Add this line to your application's Gemfile: 10 | 11 | ```ruby 12 | gem 'initial_avatar' 13 | ``` 14 | 15 | And then execute: 16 | 17 | ``` 18 | $ bundle 19 | ``` 20 | 21 | Or install it yourself as: 22 | 23 | ``` 24 | $ gem install initial_avatar 25 | ``` 26 | 27 | ## Usage 28 | 29 | CarrierWave example: 30 | 31 | ```ruby 32 | class ProfileImageUploader < CarrierWave::Uploader::Base 33 | include CarrierWave::MiniMagick 34 | 35 | def store_dir 36 | "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 37 | end 38 | 39 | def default_url 40 | InitialAvatar.avatar_data_uri(model.name[0], size: 100, font_size: 70) 41 | end 42 | 43 | version :thumb do 44 | process resize_to_fill: [100, 100] 45 | end 46 | end 47 | ``` 48 | 49 | ## Configuration 50 | 51 | In `config/initializers/initial_avatar.rb`, you can configure the following values. 52 | 53 | ```ruby 54 | InitialAvatar.configure do |config| 55 | # config.colors = %w[ 56 | # #1abc9c #16a085 #f1c40f #f39c12 #2ecc71 #27ae60 #e67e22 #d35400 #3498db 57 | # #2980b9 #e74c3c #c0392b #9b59b6 #8e44ad #bdc3c7 #34495e #2c3e50 #95a5a6 58 | # #7f8c8d #ec87bf #d870ad #f69785 #9ba37e #b49255 #b49255 #a94136 59 | # ] 60 | # config.text_color = '#ffffff' 61 | # config.size = 100 62 | # config.font_weight = 400 63 | # config.font_family = 'HelveticaNeue-Light,Helvetica Neue Light,Helvetica Neue,Helvetica, Arial,Lucida Grande, sans-serif' 64 | # config.seed = 0 65 | end 66 | ``` 67 | 68 | ## Contributing 69 | 70 | Bug reports and pull requests are welcome on GitHub at https://github.com/aki77/initial_avatar. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct. 71 | 72 | ## License 73 | 74 | The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). 75 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, gender identity and expression, level of experience, 9 | nationality, personal appearance, race, religion, or sexual identity and 10 | orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at lala.akira@gmail.com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at [http://contributor-covenant.org/version/1/4][version] 72 | 73 | [homepage]: http://contributor-covenant.org 74 | [version]: http://contributor-covenant.org/version/1/4/ 75 | --------------------------------------------------------------------------------