Monorepo for Aesthetic.Computer
aesthetic.computer
1# Invert Function Implementation
2
3This document describes the implementation of the `invert` function for KidLisp.
4
5## Summary
6
7Added 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`.
8
9## Implementation Details
10
11### CPU Implementation (graph.mjs)
12
13- Added `invert()` function that processes pixel buffer
14- Performs RGB inversion: `RGB' = 255 - RGB`
15- Preserves alpha channel unchanged
16- Supports masking for partial screen effects
17- Skips fully transparent pixels (optimization)
18- Uses performance tracking for monitoring
19
20### GPU Implementation (gpu-effects.mjs)
21
22- Added `INVERT_FRAGMENT_SHADER` with simple inversion logic
23- Created `gpuInvert()` function with same signature as other GPU effects
24- Compiled and cached shader program and uniform locations
25- Follows GPU-first, CPU-fallback pattern used by other effects
26- Properly handles cleanup in `cleanupGpuEffects()`
27
28### Integration
29
30- Exported `invert` from graph.mjs
31- Added to disk.mjs API (already present, was just calling non-existent function)
32- KidLisp can now call `(invert)` in code
33- Works with embedded layers and baking system (uses existing post-composite infrastructure)
34
35## Usage Examples
36
37### KidLisp Code
38
39```lisp
40; Simple invert
41(wipe "red")
42(invert)
43; Screen is now cyan
44
45; Partial invert with masking
46(wipe "white")
47(ink "blue")
48(box 50 50 100 100)
49(mask 60 60 80 80)
50(invert)
51(unmask)
52; Only the masked region is inverted
53
54; Animation with invert
55(wipe "black")
56(ink "yellow")
57(circle width/2 height/2 50)
58(if (even frame) (invert))
59; Flashing circle effect
60```
61
62## Testing
63
64- Created `tests/invert.test.mjs` with comprehensive logic tests
65- Verified RGB inversion formula for various colors
66- Tested alpha channel preservation
67- Confirmed double invert restores original values
68- Validated boundary cases
69
70## Performance
71
72- GPU implementation provides hardware acceleration when available
73- CPU fallback ensures compatibility on all platforms
74- Transparent pixel skipping reduces unnecessary computation
75- Performance tracking integrated for monitoring
76
77## Related Issues
78
79This implementation enables KidLisp codes like `$beli` (similar to `$4bb` but with invert) to work correctly.