Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move setting color logic from convert_hash into initialization. #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 17 additions & 21 deletions lib/identicon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ def blob_for(data, size = 420, background = nil)
def initialize(data, size)
@hash = Digest::MD5.hexdigest(data.to_s)
@chars = @hash.scan(CHARS_REGEX)
@color = [0, 0, 0]
@color = COLORS[@hash.chars[11].to_sym]
@size = size
@pixel_ratio = (size / 6).round
@image_size = @size - (@pixel_ratio / 1.5).round
@square_array = Hash.new { Hash.new(false) }
@square_array = Hash.new { |h, k| h[k] = Array.new(5, false) }
@background = [240, 240, 240]
convert_hash
end
Expand Down Expand Up @@ -159,27 +159,23 @@ def to_blob
private

def convert_hash
chars = @chars.map { |p| p[1] }
chars.each_index do |i|
char = chars[i]
sq_index = (i / 3).round
@square_array[i / 3] = [] unless @square_array.member? sq_index
if i % 3 == 0
@square_array[i / 3][0] = h_to_b char
@square_array[i / 3][4] = h_to_b char
elsif i % 3 == 1
@square_array[i / 3][1] = h_to_b char
@square_array[i / 3][3] = h_to_b char
else
@square_array[i / 3][2] = h_to_b char
end
outer_inner_middle_columns = @chars.map { |p| p[1].ord.even? }.each_slice(3).to_a
outer_inner_middle_columns.each_with_index do |arr, row|
paint_two_outer_columns?(arr[0] , row)
paint_two_inner_columns?(arr[1] || false, row)
paint_middle_column?( arr[2] || false, row)
end
color = :a
0.upto(5) { color = chars.shift.to_sym }
@color = COLORS[color]
end

def h_to_b(value)
value.bytes.first.even?
def paint_two_outer_columns?(boolean, row)
@square_array[row][0] = boolean && @square_array[row][4] = boolean
end

def paint_two_inner_columns?(boolean, row)
@square_array[row][1] = boolean && @square_array[row][3] = boolean
end

def paint_middle_column?(boolean, row)
@square_array[row][2] = boolean
end
end