summaryrefslogtreecommitdiff
path: root/ui/bmp_texture.gd
blob: 8a909838a7a1bdda8191919e04d1b0b4d154ed93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class_name BMPTexture
extends ImageTexture


static func from_texture(texture: Texture2D, background_colors: Array[Color]) -> ImageTexture:
	var image: Image = convert_image(texture.get_image(), background_colors)
	
	var image_texture := ImageTexture.new()
	image_texture.set_image(image)
	
	return image_texture


static func convert_image(image: Image, background_colors: Array[Color]) -> Image:
	var converted_image := image.duplicate()
	converted_image.convert(Image.FORMAT_RGBA8)
	
	for x in converted_image.get_width():
		for y in converted_image.get_height():
			for color in background_colors:
				if converted_image.get_pixel(x, y).is_equal_approx(color):
					converted_image.set_pixel(x, y, Color.TRANSPARENT)
					break
	
	return converted_image