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.
- Tinted with modulate: panel (255, 64, 64), and the child (255, 64, 64) too. Tinted with self_modulate: panel (255, 64, 64), child still (255, 255, 255).
- Tinted with modulate, then a blink that sets modulate = Color(1, 1, 1, 0.3): panel (82, 82, 85), gray, the red was gone. Setting modulate.a = 0.3 instead: (82, 24, 28), still red.
- A blink that writes the whole modulate color every frame for 2 seconds: gray, (155, 155, 157), at the end. tint() and set_alpha(blink_alpha()) for the same 2 seconds: the alpha went from 0.30 to 1.00 and the color stayed red on every frame.
- A tween of modulate to Color(1, 1, 1, 0), halfway: modulate (1, 0.63, 0.63, 0.5), drawn (131, 83, 86), the red fading toward white as it faded out. Color.TRANSPARENT gave the same, because in Godot 4 it is (1, 1, 1, 0), white with no alpha.
- fade() halfway: (131, 35, 38), still red. At the end modulate.a was 0 and self_modulate was still red, so it comes back red when you fade it in.
- tint() green, set_alpha(0.5), then tint() red: red at half alpha, (131, 35, 38). Changing the color did not undo the fade.
- final_color() predicted every drawn pixel within 1 out of 255, panel over the background and child over the panel.
- Where the child sits on a faded panel, both are drawn at their alpha, so that spot is brighter: at 0.3 the panel was (82, 24, 28) and the white child on it (134, 94, 96), not the (81, 81, 85) it would be alone. A CanvasGroup at alpha 0.3 around them drew the panel at (30, 12, 17) in our Compatibility test, much darker than that.
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
- A panel with text on it: tint the panel with self_modulate, so the Label keeps its own color.
- A hit flash or damage color: keep it in self_modulate or a shader, see hit flash shader for 2D, and the blink on modulate.a will not fight it.
- A faded panel and its contents that must look like one flat picture: check a CanvasGroup in your own renderer first, it came out darker in ours.
- Rings and radars that also spin: center their pivot. A full-screen fade that blocks clicks: set Mouse Filter to Ignore.
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
- A screen fade that finishes while the game is paused or slowed
- A hand of cards that always fits, without spreading out when it is small
- Edge markers that stay on the rim and point the true way
- A wrapped Label that is as wide as its text, up to a limit
- Big numbers with thousands separators, minus signs and all
- Show a 0 to 1 ratio on a progress bar, and fill a ring or gauge from the angle you want
- Spin a HUD ring, radar or icon in place instead of around its corner
- Make buttons clickable again under a full-screen fade, transition or post-process ColorRect
- 9-slice window: corners that do not stretch
- Visual novel background: fill the screen, crossfade scenes
- Coins that fly to the HUD counter
- Floating damage numbers in 2D
- An icon over a 3D character's head, without the ghost behind the camera
- Drag a box to select 3D units, in any direction
- Radar blips that turn with the player
- Pick the radial menu slot the mouse is pointing at
- Show a percent that never says 0% or 100% too early
- Type dialogue at the same letters per second on every line and every frame rate
- Place brush dots at the same spacing however fast the mouse moves
- Move a speedometer or gauge needle smoothly without cutting through the bottom
- A countdown that never shows 0 while time is left
- A coin counter that rolls up and lands on the real total