Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func set_pixel_on_in_mask(pixel: Vector2, mask: Array): # mask is a 2D bool array of each pixel
- Util.loud_log("Setting mask to true. pixel pos", pixel, LOG_LEVEL)
- mask[pixel.x][pixel.y] = true
- func color_match(color1: Color, color2: Color) -> bool:
- Util.low_log("matching colors. [color1, color2]", [color1, color2], LOG_LEVEL)
- if color1.is_equal_approx(color2):
- return true
- else:
- return false
- func wand_select(pos: Vector2, color_to_match: Color, mask: Array, locked_image: Image):
- if mask[pos.x][pos.y]:
- Util.loud_log("Checking a pixel that has already been set to true. pos", pos, LOG_LEVEL)
- return
- var color_at_point: Color = locked_image.get_pixelv(pos)
- if color_match(color_at_point, color_to_match): # Out of memory *here*
- set_pixel_on_in_mask(pos, mask)
- wand_select(Vector2(pos.x+1, pos.y), color_to_match, mask, locked_image)
- wand_select(Vector2(pos.x-1, pos.y), color_to_match, mask, locked_image)
- wand_select(Vector2(pos.x, pos.y+1), color_to_match, mask, locked_image)
- wand_select(Vector2(pos.x, pos.y-1), color_to_match, mask, locked_image)
- func initialize_mask(mask_size: Vector2): # In reality, x here is a *row* instead of a *column*
- var mask = []
- for x in range(mask_size.x):
- mask.push_back([])
- for y in range(mask_size.y):
- mask[x].push_back(false)
- Util.loud_log("initialized mask. x (mask.size())", mask.size(), LOG_LEVEL)
- Util.loud_log("initialized mask. y (mask[0].size())", mask[0].size(), LOG_LEVEL)
- return mask
- func magic_wand(click_pos: Vector2, color_to_match: Color):
- var data: Image = get_viewport().get_texture().get_data()
- data.lock()
- color_to_match = data.get_pixelv(click_pos) # DANGER! Re-check parameters in signatures for superfluousness
- var view_size: Vector2 = get_viewport().size
- var wand_mask = initialize_mask(view_size)
- Util.loud_log("Waving magic wand. wand_mask", wand_mask, LOG_LEVEL)
- wand_select(click_pos, color_to_match, wand_mask, data)
- data.unlock()
- return wand_mask
- func save_mask(mask: Array) -> void: # Takes a given 2D Array mask and saves it as a .png
- var x_dim = mask.size()
- var y_dim = mask[0].size()
- var image: Image = Image.new()
- image.create(x_dim, y_dim, false, Image.FORMAT_L8)
- image.lock()
- Util.loud_log("Created image", str(image), LOG_LEVEL)
- for x in range(x_dim):
- for y in range(y_dim):
- if mask[x][y]:
- Util.low_log("setting mask pixel. pos", Vector2(x, y), LOG_LEVEL)
- image.set_pixel(x, y, ColorN("white"))
- image.unlock()
- image.save_png("res://Map/image.png")
Add Comment
Please, Sign In to add comment