Guest User

Untitled

a guest
Jun 30th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. func set_pixel_on_in_mask(pixel: Vector2, mask: Array): # mask is a 2D bool array of each pixel
  2. Util.loud_log("Setting mask to true. pixel pos", pixel, LOG_LEVEL)
  3. mask[pixel.x][pixel.y] = true
  4.  
  5. func color_match(color1: Color, color2: Color) -> bool:
  6. Util.low_log("matching colors. [color1, color2]", [color1, color2], LOG_LEVEL)
  7. if color1.is_equal_approx(color2):
  8. return true
  9. else:
  10. return false
  11.  
  12. func wand_select(pos: Vector2, color_to_match: Color, mask: Array, locked_image: Image):
  13. if mask[pos.x][pos.y]:
  14. Util.loud_log("Checking a pixel that has already been set to true. pos", pos, LOG_LEVEL)
  15. return
  16.  
  17. var color_at_point: Color = locked_image.get_pixelv(pos)
  18. if color_match(color_at_point, color_to_match): # Out of memory *here*
  19. set_pixel_on_in_mask(pos, mask)
  20.  
  21. wand_select(Vector2(pos.x+1, pos.y), color_to_match, mask, locked_image)
  22. wand_select(Vector2(pos.x-1, pos.y), color_to_match, mask, locked_image)
  23. wand_select(Vector2(pos.x, pos.y+1), color_to_match, mask, locked_image)
  24. wand_select(Vector2(pos.x, pos.y-1), color_to_match, mask, locked_image)
  25.  
  26. func initialize_mask(mask_size: Vector2): # In reality, x here is a *row* instead of a *column*
  27. var mask = []
  28. for x in range(mask_size.x):
  29. mask.push_back([])
  30. for y in range(mask_size.y):
  31. mask[x].push_back(false)
  32. Util.loud_log("initialized mask. x (mask.size())", mask.size(), LOG_LEVEL)
  33. Util.loud_log("initialized mask. y (mask[0].size())", mask[0].size(), LOG_LEVEL)
  34. return mask
  35.  
  36. func magic_wand(click_pos: Vector2, color_to_match: Color):
  37. var data: Image = get_viewport().get_texture().get_data()
  38. data.lock()
  39. color_to_match = data.get_pixelv(click_pos) # DANGER! Re-check parameters in signatures for superfluousness
  40. var view_size: Vector2 = get_viewport().size
  41. var wand_mask = initialize_mask(view_size)
  42. Util.loud_log("Waving magic wand. wand_mask", wand_mask, LOG_LEVEL)
  43. wand_select(click_pos, color_to_match, wand_mask, data)
  44. data.unlock()
  45. return wand_mask
  46.  
  47. func save_mask(mask: Array) -> void: # Takes a given 2D Array mask and saves it as a .png
  48. var x_dim = mask.size()
  49. var y_dim = mask[0].size()
  50. var image: Image = Image.new()
  51. image.create(x_dim, y_dim, false, Image.FORMAT_L8)
  52. image.lock()
  53. Util.loud_log("Created image", str(image), LOG_LEVEL)
  54. for x in range(x_dim):
  55. for y in range(y_dim):
  56. if mask[x][y]:
  57. Util.low_log("setting mask pixel. pos", Vector2(x, y), LOG_LEVEL)
  58. image.set_pixel(x, y, ColorN("white"))
  59. image.unlock()
  60. image.save_png("res://Map/image.png")
Add Comment
Please, Sign In to add comment