def color_from_gradient(gradient, image_size, triangles): """ Color a graph of triangles using a gradient. Arguments: gradient is a Gradient object image_size is a tuple of the output dimensions, i.e. (width, height) triangles is a list of vertex-defined Triangle objects Returns: A list of Color objects, one per triangle such that colors[i] is the color of triangle[i] """ colors = [] # The size of the screen s = sqrt(image_size[0]**2 + image_size[1]**2) for t in triangles: # The color is determined by the location of the centroid tc = tri_centroid(t) # Bound centroid to boundaries of the image c = (min(max(0, tc[0]), image_size[0]), min(max(0, tc[1]), image_size[1])) frac = sqrt(c[0]**2 + c[1]**2) / s colors.append(calculate_color(gradient, frac)) return colors
def color_from_gradient(gradient, image_size, triangles): """ Color a graph of triangles using a gradient. Arguments: gradient is a Gradient object image_size is a tuple of the output dimensions, i.e. (width, height) triangles is a list of vertex-defined Triangle objects Returns: A list of Color objects, one per triangle such that colors[i] is the color of triangle[i] """ colors = [] # The size of the screen s = sqrt(image_size[0]**2+image_size[1]**2) for t in triangles: # The color is determined by the location of the centroid tc = tri_centroid(t) # Bound centroid to boundaries of the image c = (min(max(0, tc[0]), image_size[0]), min(max(0, tc[1]), image_size[1])) frac = sqrt(c[0]**2+c[1]**2)/s colors.append(calculate_color(gradient, frac)) return colors
def color_from_image(background_image, triangles): """ Color a graph of triangles using the colors from an image. The color of each triangle is determined by the color of the image pixel at its centroid. Arguments: background_image is a PIL Image object triangles is a list of vertex-defined Triangle objects Returns: A list of Color objects, one per triangle such that colors[i] is the color of triangle[i] """ colors = [] pixels = background_image.load() size = background_image.size for t in triangles: centroid = tri_centroid(t) # Truncate the coordinates to fit within the boundaries of the image int_centroid = (int(min(max(centroid[0], 0), size[0] - 1)), int(min(max(centroid[1], 0), size[1] - 1))) # Get the color of the image at the centroid p = pixels[int_centroid[0], int_centroid[1]] colors.append(Color(p[0], p[1], p[2])) return colors
def color_from_image(background_image, triangles): """ Color a graph of triangles using the colors from an image. The color of each triangle is determined by the color of the image pixel at its centroid. Arguments: background_image is a PIL Image object triangles is a list of vertex-defined Triangle objects Returns: A list of Color objects, one per triangle such that colors[i] is the color of triangle[i] """ colors = [] pixels = background_image.load() size = background_image.size for t in triangles: centroid = tri_centroid(t) # Truncate the coordinates to fit within the boundaries of the image int_centroid = ( int(min(max(centroid[0], 0), size[0]-1)), int(min(max(centroid[1], 0), size[1]-1)) ) # Get the color of the image at the centroid p = pixels[int_centroid[0], int_centroid[1]] colors.append(Color(p[0], p[1], p[2])) return colors