Menu

Colors in Programming, Part 3: Alpha Blending
πŸ“°

Colors in Programming, Part 3: Alpha Blending

DEV Community: cssΒ·Surhid AmatyaΒ·8 days ago
#MZwYcUj2
#devtocss#devto#dev#cccccc#rrggbb
Long article (833 words) β€” no AI summary yet.
Reading 0:00
15s threshold

Part 1 covered RGB. Part 2 added alpha. Now the question that's been hanging: if you put 50% black on a white background, what color does the user see?

A transparent color has no fixed look

#80000000 is roughly half-opaque black. Over white it looks gray. Over blue it looks dark blue. Over red, dark red. The color on its own doesn't decide the result. What's behind it does.

The color on top is the foreground, the one underneath is the background.

The formula

For each channel:

result = foreground Γ— alpha + background Γ— (1 - alpha)

Enter fullscreen mode

Exit fullscreen mode

Alpha here is 0.0 to 1.0, so 50% is 0.5. At 0.5, foreground and background contribute equally.

50% black over white

Take one channel. White is 255, black is 0, alpha is 0.5:

0 Γ— 0.5 + 255 Γ— 0.5 = 127.5

Enter fullscreen mode

Exit fullscreen mode

That rounds to 128, or 80 in hex.…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.