52 lines
1.9 KiB
GDScript
52 lines
1.9 KiB
GDScript
class_name ImageTools
|
|
|
|
static func draw_circle(image: Image, center: Vector2i, length: int, color: Color = Color.WHITE):
|
|
for x in range(image.get_width()):
|
|
for y in range(image.get_height()):
|
|
var center_distance = Vector2i(x, y).distance_to(center)
|
|
|
|
if (center_distance <= length):
|
|
image.set_pixel(x, y, color)
|
|
|
|
|
|
static func draw_gradient(image: Image, center: Vector2i, length: int, color: Color = Color.WHITE, inverse := false):
|
|
for x in range(image.get_width()):
|
|
for y in range(image.get_height()):
|
|
var original_pixel_color = image.get_pixel(x, y)
|
|
var center_distance = Vector2i(x, y).distance_to(center)
|
|
|
|
if (center_distance == 0):
|
|
if not inverse:
|
|
image.set_pixel(x, y, original_pixel_color.blend(color))
|
|
else:
|
|
var color_to_add = Color(color, 1 / (center_distance / length)) if not inverse else Color(color, center_distance / length)
|
|
image.set_pixel(
|
|
x,
|
|
y,
|
|
original_pixel_color.blend(color_to_add)
|
|
)
|
|
|
|
static func flatten(image: Image, threshold := 0.5):
|
|
for x in range(image.get_width()):
|
|
for y in range(image.get_height()):
|
|
var original_pixel_color = image.get_pixel(x, y)
|
|
|
|
if original_pixel_color.r > threshold:
|
|
image.set_pixel(
|
|
x,
|
|
y,
|
|
Color.WHITE
|
|
)
|
|
else:
|
|
image.set_pixel(
|
|
x,
|
|
y,
|
|
Color.BLACK
|
|
)
|
|
|
|
static func copy(from: Image, to : Image):
|
|
for x in range(from.get_width()):
|
|
for y in range(from.get_height()):
|
|
to.set_pixel(x, y, from.get_pixel(x, y))
|
|
|
|
|