← Pixel Dreamer Studio

Blink or fade a tinted HUD element without losing its color

modulate holds the color and the alpha together, and it tints every child too. Put the color in self_modulate, and blink or fade with modulate.a only.

Set the color with self_modulate, and change only modulate.a to blink or fade; tween "modulate:a", not "modulate". In our Godot 4.7 test a white panel tinted red with modulate came out gray, (82, 82, 85), after a blink set modulate = Color(1, 1, 1, 0.3), and its white child had turned red with it. With the red in self_modulate and the blink on modulate.a it stayed red, (82, 24, 28), on every frame of a 2 second blink, and the child stayed white.

The code

Save it as tint_blink.gd. tint() puts a color in self_modulate and keeps its alpha. set_alpha() and fade() only touch modulate.a, so children fade along and every color stays. blink_alpha() gives a smooth blink between a low alpha and 1. final_color() multiplies everything that colors a node: print it when an icon comes out darker or a different color than you set. For example: const TintBlink := preload("res://tint_blink.gd"), TintBlink.tint($TargetLock, Color.RED), then each frame TintBlink.set_alpha($TargetLock, TintBlink.blink_alpha(time)).

extends RefCounted
## Keep a HUD element's color while it blinks or fades.
## The color goes in self_modulate. Blinking and fading only touch modulate.a.


static func tint(item: CanvasItem, color: Color) -> void:
	# self_modulate colors this node only, not its children
	item.self_modulate = Color(color.r, color.g, color.b, item.self_modulate.a)


static func set_alpha(item: CanvasItem, alpha: float) -> void:
	item.modulate.a = alpha  # children fade with it, the color stays


static func fade(item: CanvasItem, to_alpha: float, seconds: float) -> Tween:
	var t := item.create_tween()
	t.tween_property(item, "modulate:a", to_alpha, seconds)
	return t


static func blink_alpha(time: float, speed := 1.5, min_alpha := 0.3) -> float:
	return lerpf(min_alpha, 1.0, 0.5 + 0.5 * sin(time * speed * TAU))


static func final_color(item: CanvasItem) -> Color:
	# what Godot multiplies into this node's pixels: its own modulate and self_modulate,
	# times the modulate of every CanvasItem parent up to the first node that is not one
	var c := item.modulate * item.self_modulate
	var p := item.get_parent()
	while p is CanvasItem:
		c *= (p as CanvasItem).modulate
		p = p.get_parent()
	return c

What we measured

A white ColorRect panel, 300 by 200, with a white 100 by 100 ColorRect child, on a CanvasLayer over a dark background of (7, 7, 12). We read the drawn pixel color, 0 to 255, on the panel and on the child. The red tint was (1, 0.25, 0.25). Two runs gave the same numbers.

Why it happens

modulate and self_modulate are both colors with an alpha, multiplied into the pixels. modulate also multiplies every child, self_modulate only the node itself. Setting modulate to a new Color replaces the tint together with the alpha, and a tween of "modulate" moves all four channels toward its target, so the color goes with it. "modulate:a" is just the alpha. Children are drawn on top of their parent, each with its own alpha, which is why a faded panel shows through a faded child. We tested with the Compatibility renderer only.

What to change

HUD pieces set up this way: Godot 4 Ready Kit includes 124 Sci-Fi HUD Elements. Every element is a white TextureRect you color in Self Modulate, and the target locks blink with a script that changes modulate alpha only, so your color stays.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Godot 4 Ready Kit is 9 products in one bundle, including 124 Sci-Fi HUD Elements: white pieces you color in Self Modulate, with target locks that blink on alpha only.

More how-tos: UI and HUD