class T::Identicon

Attributes

bits[RW]

Six-bit number (0-63)

color[RW]

Eight-bit number (0-255)

Public Class Methods

for_user_name(string) click to toggle source
# File lib/t/identicon.rb, line 38
def for_user_name(string)
  Identicon.new(digest(string))
end
new(number) click to toggle source
# File lib/t/identicon.rb, line 9
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

Private Class Methods

digest(string) click to toggle source
# File lib/t/identicon.rb, line 44
def digest(string)
  require "digest"
  Digest::MD5.digest(string).chars.inject(0) { |acc, elem| (acc << 8) | elem.ord }
end

Public Instance Methods

lines() click to toggle source
# File lib/t/identicon.rb, line 20
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 Instance Methods

bg(bit) click to toggle source
# File lib/t/identicon.rb, line 32
def bg(bit)
  bit.zero? ? "\033[48;5;#{@bcolor}m" : "\033[48;5;#{@fcolor}m"
end
reset() click to toggle source
# File lib/t/identicon.rb, line 28
def reset
  "\033[0m"
end