···11+# Invert Function Implementation
22+33+This document describes the implementation of the `invert` function for KidLisp.
44+55+## Summary
66+77+Added the missing `invert` function to the graphics system, with both CPU and GPU implementations. The function inverts RGB color values (255 - value) while preserving the alpha channel, following the same pattern as other image effects like `contrast` and `brightness`.
88+99+## Implementation Details
1010+1111+### CPU Implementation (graph.mjs)
1212+1313+- Added `invert()` function that processes pixel buffer
1414+- Performs RGB inversion: `RGB' = 255 - RGB`
1515+- Preserves alpha channel unchanged
1616+- Supports masking for partial screen effects
1717+- Skips fully transparent pixels (optimization)
1818+- Uses performance tracking for monitoring
1919+2020+### GPU Implementation (gpu-effects.mjs)
2121+2222+- Added `INVERT_FRAGMENT_SHADER` with simple inversion logic
2323+- Created `gpuInvert()` function with same signature as other GPU effects
2424+- Compiled and cached shader program and uniform locations
2525+- Follows GPU-first, CPU-fallback pattern used by other effects
2626+- Properly handles cleanup in `cleanupGpuEffects()`
2727+2828+### Integration
2929+3030+- Exported `invert` from graph.mjs
3131+- Added to disk.mjs API (already present, was just calling non-existent function)
3232+- KidLisp can now call `(invert)` in code
3333+- Works with embedded layers and baking system (uses existing post-composite infrastructure)
3434+3535+## Usage Examples
3636+3737+### KidLisp Code
3838+3939+```lisp
4040+; Simple invert
4141+(wipe "red")
4242+(invert)
4343+; Screen is now cyan
4444+4545+; Partial invert with masking
4646+(wipe "white")
4747+(ink "blue")
4848+(box 50 50 100 100)
4949+(mask 60 60 80 80)
5050+(invert)
5151+(unmask)
5252+; Only the masked region is inverted
5353+5454+; Animation with invert
5555+(wipe "black")
5656+(ink "yellow")
5757+(circle width/2 height/2 50)
5858+(if (even frame) (invert))
5959+; Flashing circle effect
6060+```
6161+6262+## Testing
6363+6464+- Created `tests/invert.test.mjs` with comprehensive logic tests
6565+- Verified RGB inversion formula for various colors
6666+- Tested alpha channel preservation
6767+- Confirmed double invert restores original values
6868+- Validated boundary cases
6969+7070+## Performance
7171+7272+- GPU implementation provides hardware acceleration when available
7373+- CPU fallback ensures compatibility on all platforms
7474+- Transparent pixel skipping reduces unnecessary computation
7575+- Performance tracking integrated for monitoring
7676+7777+## Related Issues
7878+7979+This implementation enables KidLisp codes like `$beli` (similar to `$4bb` but with invert) to work correctly.