ColorKeyAlphaEffectLib — Chroma Key Transparency

Makes pixels of a specific color transparent, enabling chroma key (green screen) removal and selective color knockout effects on image controls. Wraps FireMonkey’s TColorKeyAlphaEffect. Ideal for green screen removal, background knockout, sprite transparency, and color-based masking. 14 functions.

CategoryCountDescription
Error Handling4colorkey_error, errormsg$, strerror$, clearerror
Creation & Destruction2colorkey# (create), colorkey_free (destroy)
Key Color & Tolerance4color, tolerance (get/set)
Effect Control4enabled, trigger (get/set)
⚠ Critical: The default tolerance is 0.0, which means no colors become transparent. You must set tolerance > 0 for the effect to work. A typical starting value is 0.1–0.2.

How Chroma Key Works

The effect compares every pixel in the image to the key color. If a pixel’s color is within the tolerance distance of the key color, it becomes transparent. The wider the tolerance, the more shades around the key color are knocked out.

StepWhat Happens
1. Set key colorChoose the color to remove (e.g., "Lime" for green screen)
2. Set toleranceHow far from the key color to match (0.0 = exact only, 1.0 = everything)
3. Pixels within tolerance → transparentMatching pixels become fully transparent, revealing whatever is behind
⚠ Textures Only: This effect works on bitmap/texture data (images loaded via image_load#). It does not work on vector shapes like rectangles or circles.

Cross-Platform Support

PlatformStatusNotes
Windows✅ Full SupportGPU-accelerated via Direct2D
Linux✅ Full SupportSoftware rendering fallback
Android✅ Full SupportGPU-accelerated

Error Handling

All functions set an error code on failure. Check with colorkey_error() after operations.

FunctionSignatureDescription
colorkey_error()colorkey_error@Returns last error code (0 = no error)
colorkey_errormsg$()colorkey_errormsg$@Returns last error message as string
colorkey_strerror$(code)colorkey_strerror$@nConverts an error code to descriptive text
colorkey_clearerror()colorkey_clearerror@Clears the error state

Error Codes

CodeConstantMeaning
0ERR_NONENo error
1ERR_NIL_EFFECTEffect pointer is nil
2ERR_INVALID_EFFECTPointer is not a valid TColorKeyAlphaEffect
3ERR_INVALID_VALUEProperty value out of range
4ERR_NIL_PARENTParent control pointer is nil
5ERR_INVALID_PARENTPointer is not a valid TFmxObject

Creation & Destruction

FunctionSignatureDescription
colorkey#(parent#)colorkey#@#Creates a color key alpha effect attached to the given control. Returns the effect pointer.
colorkey_free(effect#)colorkey_free@#Destroys the effect and removes it from the parent control.

Key Color

Sets the target color to make transparent. Colors can be specified as named colors, hex RGB, or hex ARGB.

FunctionSignatureDescription
colorkey_color#(effect#, color$)colorkey_color#@#$Sets the color to knock out
colorkey_color$(effect#)colorkey_color$@#Gets the current key color

Color Format Reference

FormatExampleDescription
Named color"Lime", "Blue", "Red"Standard color names
Hex RGB"#00FF00"6-digit hex (red, green, blue)
Hex ARGB"#FF00FF00"8-digit hex (alpha, red, green, blue)

Common Key Colors for Chroma Key

ColorValueUse Case
"Lime"#00FF00Standard green screen (most common)
"Green"#008000Darker green backgrounds
"Blue"#0000FFBlue screen (film industry)
"White"#FFFFFFWhite background knockout
"Black"#000000Black background knockout
"Magenta"#FF00FFMagenta (game sprites, rarely in photos)
╯ key-color.bas
' Green screen removal
colorkey_color#(ck#, "Lime")
colorkey_tolerance#(ck#, 0.15)

' Blue screen removal
colorkey_color#(ck#, "#0000FF")
colorkey_tolerance#(ck#, 0.2)

' White background knockout
colorkey_color#(ck#, "White")
colorkey_tolerance#(ck#, 0.1)

Tolerance

Controls how closely a pixel must match the key color to become transparent. At 0.0 only an exact match is affected (and the default 0.0 effectively disables the effect). At 1.0 nearly all colors become transparent.

FunctionSignatureDescription
colorkey_tolerance#(effect#, value)colorkey_tolerance#@#nSets tolerance (0.0–1.0)
colorkey_tolerance(effect#)colorkey_tolerance@#Gets current tolerance
ToleranceMatch RangeUse Case
0.0Nothing (default — effect disabled)Must be > 0 to work
0.01–0.05Exact color onlyUniform solid backgrounds, sprites
0.05–0.15Close shades includedClean green screens, product photos
0.15–0.30Wider range, catches shadowsUneven lighting, amateur green screens
0.30–0.50Aggressive knockoutNoisy backgrounds, artistic effect
0.50–1.0Most/all colors removedExtreme effect, near-full transparency
╯ tolerance.bas
' Tight match for clean green screen
colorkey_tolerance#(ck#, 0.1)

' Wider match for uneven lighting
colorkey_tolerance#(ck#, 0.25)

let t = colorkey_tolerance(ck#)
print "Tolerance: " + stri$(t, 2)

Effect Control

Enabled

FunctionSignatureDescription
colorkey_enabled#(effect#, value)colorkey_enabled#@#nEnables (1) or disables (0)
colorkey_enabled(effect#)colorkey_enabled@#Gets enabled state

Trigger

FunctionSignatureDescription
colorkey_trigger#(effect#, trigger$)colorkey_trigger#@#$Sets trigger string
colorkey_trigger$(effect#)colorkey_trigger$@#Gets current trigger string

Complete Examples

Adjustable Tolerance with Slider

╯ colorkey-slider.bas
let frm# = Pointer#(0)
let img# = Pointer#(0)
let ck# = Pointer#(0)
let trkTol# = Pointer#(0)
let lblTol# = Pointer#(0)

frm# = form#("ColorKey Control", 450, 380)

img# = image#(frm#)
image_bounds#(img#, 125, 20, 200, 150)
image_load#(img#, "https://picsum.photos/200/150")

ck# = colorkey#(img#)
colorkey_color#(ck#, "Green")
colorkey_tolerance#(ck#, 0.2)

' Tolerance control
lblTol# = label#(frm#, "Tolerance: 0.20", 50, 190)
trkTol# = trackbar#(frm#)
trackbar_bounds#(trkTol#, 50, 220, 350, 30)
trackbar_max#(trkTol#, 100)
trackbar_value#(trkTol#, 20)
trackbar_onchange#(trkTol#, "OnTolChange")

form_show(frm#)

function OnTolChange(sender#) local v
  let v = trackbar_value(trkTol#) / 100
  colorkey_tolerance#(ck#, v)
  label_text#(lblTol#, "Tolerance: " + stri$(v, 2))
endfunction

Toggle Chroma Key Effect

╯ colorkey-toggle.bas
let frm# = Pointer#(0)
let img# = Pointer#(0)
let ck# = Pointer#(0)
let btn# = Pointer#(0)
let isOn = 0

frm# = form#("ChromaKey Demo", 400, 300)

img# = image#(frm#)
image_bounds#(img#, 100, 30, 200, 150)
image_load#(img#, "https://picsum.photos/200/150")

ck# = colorkey#(img#)
colorkey_enabled#(ck#, 0)
colorkey_color#(ck#, "Lime")
colorkey_tolerance#(ck#, 0.1)

btn# = button#(frm#, "Enable Effect")
button_bounds#(btn#, 140, 210, 120, 30)
button_onclick#(btn#, "Toggle")

form_show(frm#)

function Toggle(sender#)
  if isOn = 0 then
    colorkey_enabled#(ck#, 1)
    isOn = 1
    button_text#(btn#, "Disable Effect")
  else
    colorkey_enabled#(ck#, 0)
    isOn = 0
    button_text#(btn#, "Enable Effect")
  endif
endfunction

Best Practices

PracticeWhy
Always set tolerance > 0Default 0.0 makes nothing transparent — the most common mistake
Use "Lime" (#00FF00) for green screenStandard chroma key green — widest compatibility
Start tolerance at 0.1, increase graduallyToo high removes foreground colors; fine-tune visually
Only use on image controls, not vector shapesEffect operates on bitmap textures, not vector rendering
Use "Magenta" for game spritesMagenta (#FF00FF) rarely appears in natural images
Combine with other effects for compositingLayer transparent images over backgrounds for photo composites
Animate tolerance with FloatAnimationLibGradual reveal/disappear effects by animating tolerance 0 → 0.3
Use colorkey_enabled# to toggleFaster than destroy/recreate for before/after comparison

Quick Reference

FunctionSignatureDescription
ERROR HANDLING
colorkey_error()colorkey_error@Last error code
colorkey_errormsg$()colorkey_errormsg$@Last error message
colorkey_strerror$(code)colorkey_strerror$@nError code to text
colorkey_clearerror()colorkey_clearerror@Clear error state
CREATION & DESTRUCTION
colorkey#(parent#)colorkey#@#Create effect on control
colorkey_free(effect#)colorkey_free@#Destroy effect
KEY COLOR & TOLERANCE
colorkey_color#(effect#, color$)colorkey_color#@#$Set key color
colorkey_color$(effect#)colorkey_color$@#Get key color
colorkey_tolerance#(effect#, val)colorkey_tolerance#@#nSet tolerance (0.0–1.0)
colorkey_tolerance(effect#)colorkey_tolerance@#Get tolerance
EFFECT CONTROL
colorkey_enabled#(effect#, val)colorkey_enabled#@#nEnable (1) / disable (0)
colorkey_enabled(effect#)colorkey_enabled@#Get enabled state
colorkey_trigger#(effect#, str$)colorkey_trigger#@#$Set trigger string
colorkey_trigger$(effect#)colorkey_trigger$@#Get trigger string

14 functions. Part of the Plan9Basic visual effects library system.

See Also

  • MonochromeEffectLib — Convert to grayscale
  • InvertEffectLib — Invert all colors
  • HueAdjustEffectLib — Shift hue values
  • FillEffectLib — Fill with solid color
  • MaskToAlphaEffectLib — Convert luminance to transparency