Sha256: d81bb92263c63fc0dcc5936606c99198b79f688ea4477ed7a337212f0589a776

Contents?: true

Size: 997 Bytes

Versions: 2

Compression:

Stored size: 997 Bytes

Contents

module T
  class Identicon
    # Six-bit number (0-63)
    attr_accessor :bits

    # Eight-bit number (0-255)
    attr_accessor :color

    def initialize(number)
      # Bottom six bits
      @bits = number & 0x3f

      # Next highest eight bits
      @fcolor = (number >> 6) & 0xff

      # Next highest eight bits
      @bcolor = (number >> 14) & 0xff
    end

    def lines
      ["#{bg @bits[0]}  #{bg @bits[1]}  #{bg @bits[0]}  #{reset}",
       "#{bg @bits[2]}  #{bg @bits[3]}  #{bg @bits[2]}  #{reset}",
       "#{bg @bits[4]}  #{bg @bits[5]}  #{bg @bits[4]}  #{reset}"]
    end

  private

    def reset
      "\033[0m"
    end

    def bg(bit)
      bit.zero? ? "\033[48;5;#{@bcolor}m" : "\033[48;5;#{@fcolor}m"
    end
  end

  class << Identicon
    def for_user_name(string)
      Identicon.new(digest(string))
    end

  private

    def digest(string)
      require 'digest'
      Digest::MD5.digest(string).chars.inject(0) { |acc, elem| (acc << 8) | elem.ord }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
t-3.1.0 lib/t/identicon.rb
t-3.0.0 lib/t/identicon.rb