class Idecon::Identicon
Constants
- BACKGROUND_COLOR
- DEFAULT_PATH
- PIXEL_SIZE
- SQUARE_SIZE
Public Class Methods
new(user_name, path = DEFAULT_PATH)
click to toggle source
# File lib/idecon.rb, line 16 def initialize(user_name, path = DEFAULT_PATH) @hash = Digest::MD5.hexdigest(user_name) @path = path @color = color end
Public Instance Methods
generate()
click to toggle source
# File lib/idecon.rb, line 22 def generate create_matrix create_image save_image(@path) end
Private Instance Methods
color()
click to toggle source
Get color from hash
# File lib/idecon.rb, line 39 def color @hash[23..32].scan(/([\w\d])([\w\d])([\w\d])/).map do |arr| color = 0 # Convert every character to a two-digit number, then get second digit arr.each_with_index do |c, i| color += 10**i * c.to_i(36) end color % 256 end end
create_image()
click to toggle source
Create image using ChunkyPNG by painting squares
# File lib/idecon.rb, line 51 def create_image @image = ChunkyPNG::Image.new(SQUARE_SIZE, SQUARE_SIZE, ChunkyPNG::Color.rgb(0, 0, 0)) @matrix.each_with_index do |color, i| square = [i / 5, i % 5] draw_square(square, color) end end
create_matrix()
click to toggle source
Create matrix, where @colour - coloured square,
[255, 255, 255] - empty square
# File lib/idecon.rb, line 32 def create_matrix @matrix = @hash.chars[0..24].map do |c| c.to_i(36).odd? ? @color : BACKGROUND_COLOR end end
draw_square(square, color)
click to toggle source
# File lib/idecon.rb, line 60 def draw_square(square, color) @image.rect(square[0] * PIXEL_SIZE, square[1] * PIXEL_SIZE, (square[0] + 1) * PIXEL_SIZE, (square[1] + 1) * PIXEL_SIZE, ChunkyPNG::Color::TRANSPARENT, ChunkyPNG::Color.rgb(color[0], color[1], color[2])) end
save_image(path)
click to toggle source
# File lib/idecon.rb, line 67 def save_image(path) @image.save(path) end