% Utility functions for Programming Assignment 10

def empty_image(size)
  image = Array.new(size)
  for i in 0..size-1 do
    image[i] = Array.new(size)
    for j in 0..size-1 do
      image[i][j] = [255,255,255]
    end
  end
  return image
end

def make_square!(image,size)
  color = [rand(256), rand(256), rand(256)]
  for i in 0..size-1 do
    image[0][i] = image[i][0] = image[size-1][i] = image[i][size-1] = color
  end
  plot(image,10)
  sleep(0.5)
end

def plot(matrix,mag=1)
  height = matrix.length
  width = matrix.first.length 
  Canvas.init(mag*width+2,mag*height+2, "Plot")
  Canvas::Rectangle.new(1,1,mag*(width+1),mag*(height+1),:width=>0, :fill=>"black")
  color = "white"
  for y in 0..height-1 do
    for x in 0..width-1 do
      rgb_tuple = matrix[y][x]
      color = "#%02X%02X%02X" % [rgb_tuple[0], rgb_tuple[1], rgb_tuple[2]]
      Canvas::Rectangle.new(1+mag*x,1+mag*y,1+mag*(x+1),1+mag*(y+1), :width=>0, :fill=>color)
    end
  end                                         
  return nil
end

