-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpostprocesseffects.go
More file actions
1378 lines (1201 loc) · 40 KB
/
Copy pathpostprocesseffects.go
File metadata and controls
1378 lines (1201 loc) · 40 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package glyph
import (
"math"
"time"
"unsafe"
)
// dynFloat64 bundles a static float64 with optional dynamic source (pointer,
// condition, or tween). replaces the repeated 3-field pattern across effects.
type dynFloat64 struct {
val float64
dyn any
ptr *float64
armed *bool // non-nil for From tweens — resolve() sets true, tween waits for it
isSet bool
}
func (d *dynFloat64) set(v any) {
d.isSet = true
switch val := v.(type) {
case float64:
d.val = val
case float32:
d.val = float64(val)
case int:
d.val = float64(val)
case *float64:
d.dyn = val
case conditionNode:
d.dyn = val
case tweenNode:
d.dyn = val
case OscC:
d.dyn = val
}
}
func (d *dynFloat64) compile(tmpl *Template) {
if d.dyn != nil {
d.ptr = tmpl.compileDynFloat64(d.dyn, nil, 0)
}
}
// compileArmed is for screen effects whose Apply calls resolve(). From/Out
// tweens inside conditional branches are tied to that activation.
func (d *dynFloat64) compileArmed(tmpl *Template, elemBase unsafe.Pointer, elemSize uintptr) {
if d.dyn == nil {
return
}
if tw, ok := d.dyn.(tweenNode); ok && (tw.getTweenFrom() != nil || tw.getTweenOut() != nil) && tmpl.root != nil {
d.armed = new(bool)
d.ptr = tmpl.compileTweenFloat64(tw, d.armed, elemBase, elemSize)
} else {
d.ptr = tmpl.compileDynFloat64(d.dyn, elemBase, elemSize)
}
}
func (d dynFloat64) resolve() float64 {
if d.armed != nil {
*d.armed = true
}
if d.ptr != nil {
return *d.ptr
}
return d.val
}
// dynInt bundles a static int with optional dynamic source.
type dynInt struct {
val int
dyn any
ptr *int16
}
func (d *dynInt) set(v any) {
switch val := v.(type) {
case int:
d.val = val
case int16:
d.val = int(val)
case *int16:
d.dyn = val
case conditionNode:
d.dyn = val
case tweenNode:
d.dyn = val
case OscC:
d.dyn = val
}
}
func (d *dynInt) compile(tmpl *Template) {
if d.dyn != nil {
d.ptr = tmpl.compileDynInt16(d.dyn, nil, 0)
}
}
func (d dynInt) resolve() int {
if d.ptr != nil {
return int(*d.ptr)
}
return d.val
}
// dynColor bundles a static Color with optional dynamic source.
type dynColor struct {
val Color
dyn any
ptr *Color
}
func (d *dynColor) set(v any) {
switch val := v.(type) {
case Color:
d.val = val
case *Color:
d.dyn = val
case conditionNode:
d.dyn = val
case tweenNode:
d.dyn = val
case OscC:
d.dyn = val
}
}
func (d *dynColor) compile(tmpl *Template) {
if d.dyn != nil {
d.ptr = tmpl.compileDynColor(d.dyn, nil, 0)
}
}
func (d dynColor) resolve() Color {
if d.ptr != nil {
return *d.ptr
}
return d.val
}
// ---------------------------------------------------------------------------
// Subtle: one-liner polish for real apps
// ---------------------------------------------------------------------------
// SEDim applies the terminal Dim attribute to every cell.
// The simplest possible effect — one attribute, whole screen.
func SEDim() Effect {
return EachCell(func(_, _ int, c Cell, _ PostContext) Cell {
c.Style.Attr = c.Style.Attr.With(AttrDim)
return c
})
}
// TintEffect shifts all RGB colours toward a target colour.
type TintEffect struct {
target Color
strength dynFloat64
dodge *NodeRef
}
// SETint shifts all RGB colours toward a target colour.
// Think colour grading: warm/cool/moody tones in one line.
// Default strength 0.15 — tasteful tint out of the box.
func SETint(color Color) TintEffect {
return TintEffect{target: color, strength: dynFloat64{val: 0.15}}
}
// Strength sets how strongly the tint blends in (0.0 = none, 1.0 = full).
func (t TintEffect) Strength(s any) TintEffect { t.strength.set(s); return t }
// Dodge exempts the given node from tinting — useful for preserving a focused panel.
func (t TintEffect) Dodge(ref *NodeRef) TintEffect { t.dodge = ref; return t }
func (t TintEffect) compileEffect(tmpl *Template) Effect {
t.strength.compileArmed(tmpl, nil, 0)
return t
}
func (t TintEffect) Apply(buf *Buffer, ctx PostContext) {
s := t.strength.resolve()
EachCell(func(x, y int, c Cell, ectx PostContext) Cell {
if t.dodge != nil && inRect(x, y, t.dodge) {
return c
}
c.Style.FG = lerpIfRGB(resolveFG(c.Style.FG, ectx), t.target, s)
c.Style.BG = lerpIfRGB(resolveBG(c.Style.BG, ectx), t.target, s)
return c
}).Apply(buf, ctx)
}
// VignetteEffect darkens cells toward the screen edges.
type VignetteEffect struct {
strength dynFloat64
focus *NodeRef
dodge *NodeRef
quantize bool
}
// SEVignette darkens cells near the screen edges.
// Quadratic falloff for a natural cinematic feel. Default strength 0.8.
func SEVignette() VignetteEffect {
return VignetteEffect{strength: dynFloat64{val: 0.8}, quantize: true}
}
// Strength sets edge darkening intensity (0.0 = no effect, 1.0 = full black at edges).
func (v VignetteEffect) Strength(s any) VignetteEffect { v.strength.set(s); return v }
func (v VignetteEffect) compileEffect(tmpl *Template) Effect {
v.strength.compileArmed(tmpl, nil, 0)
return v
}
// Focus centres the vignette on the given node.
func (v VignetteEffect) Focus(ref *NodeRef) VignetteEffect { v.focus = ref; return v }
// Dodge exempts the given node from darkening.
func (v VignetteEffect) Dodge(ref *NodeRef) VignetteEffect { v.dodge = ref; return v }
// Smooth disables quantization for a continuous gradient (slightly more escape output).
func (v VignetteEffect) Smooth() VignetteEffect { v.quantize = false; return v }
func (v VignetteEffect) Apply(buf *Buffer, ctx PostContext) {
black := Color{Mode: ColorRGB}
var cx, cy float64
if v.focus != nil {
cx = float64(v.focus.X) + float64(v.focus.W)/2
cy = float64(v.focus.Y) + float64(v.focus.H)/2
} else {
cx = float64(ctx.Width) / 2
cy = float64(ctx.Height) / 2
}
// maxDist = distance from center to the farthest screen corner, aspect-compensated.
// using max extents handles off-center focus nodes correctly.
maxX := math.Max(cx, float64(ctx.Width)-cx)
maxY := math.Max(cy, float64(ctx.Height)-cy) * 2
maxDist := math.Sqrt(maxX*maxX + maxY*maxY)
dodgeOpacity := 0.0
if v.dodge != nil {
opacity := refOpacity(v.dodge)
dodgeOpacity = opacity * opacity
}
for y := range ctx.Height {
base := y * buf.width
dy := (float64(y) - cy) * 2
for x := range ctx.Width {
dx := float64(x) - cx
dist := math.Sqrt(dx*dx+dy*dy) / maxDist
dim := dist * dist * v.strength.resolve()
if v.dodge != nil {
dim *= 1 - dodgeOpacity*vignetteDodgeWeight(x, y, v.dodge)
}
if dim > 1 {
dim = 1
}
// snap to 32 levels — imperceptible banding, collapses escape output
if v.quantize {
dim = math.Round(dim*32) / 32
}
idx := base + x
c := &buf.cells[idx]
c.Style.FG = lerpIfRGB(resolveFG(c.Style.FG, ctx), black, dim)
c.Style.BG = lerpIfRGB(resolveBG(c.Style.BG, ctx), black, dim)
}
}
}
func vignetteDodgeWeight(x, y int, ref *NodeRef) float64 {
if ref == nil {
return 0
}
if inRect(x, y, ref) {
return 1
}
const feather = 4.0
dx := 0
if x < ref.X {
dx = ref.X - x
} else if x >= ref.X+ref.W {
dx = x - (ref.X + ref.W - 1)
}
dy := 0
if y < ref.Y {
dy = ref.Y - y
} else if y >= ref.Y+ref.H {
dy = y - (ref.Y + ref.H - 1)
}
dist := math.Sqrt(float64(dx*dx + dy*dy))
if dist >= feather {
return 0
}
t := dist / feather
return 1 - t*t*(3-2*t)
}
// DesaturateEffect removes colour saturation from all RGB cells.
type DesaturateEffect struct {
strength dynFloat64
dodge *NodeRef
}
// SEDesaturate removes colour saturation from all RGB cells.
// Uses perceptual luminance weights (BT.601). Default strength 0.7.
func SEDesaturate() DesaturateEffect { return DesaturateEffect{strength: dynFloat64{val: 0.7}} }
// Strength sets how much to desaturate (0.0 = full colour, 1.0 = fully grey).
func (d DesaturateEffect) Strength(s any) DesaturateEffect { d.strength.set(s); return d }
func (d DesaturateEffect) compileEffect(tmpl *Template) Effect {
d.strength.compileArmed(tmpl, nil, 0)
return d
}
// Dodge exempts the given node — the classic "colour spotlight" on a grey world.
func (d DesaturateEffect) Dodge(ref *NodeRef) DesaturateEffect { d.dodge = ref; return d }
func (d DesaturateEffect) Apply(buf *Buffer, ctx PostContext) {
s := d.strength.resolve()
EachCell(func(x, y int, c Cell, ectx PostContext) Cell {
if d.dodge != nil && inRect(x, y, d.dodge) {
return c
}
c.Style.FG = desaturateColor(resolveFG(c.Style.FG, ectx), s)
c.Style.BG = desaturateColor(resolveBG(c.Style.BG, ectx), s)
return c
}).Apply(buf, ctx)
}
// ContrastEffect boosts contrast by pushing colour channels toward extremes.
type ContrastEffect struct {
strength dynFloat64
dodge *NodeRef
}
// SEContrast boosts contrast by pushing colour channels toward extremes.
// Default strength 1.5 — noticeable punch without going stark.
func SEContrast() ContrastEffect { return ContrastEffect{strength: dynFloat64{val: 1.5}} }
// Strength sets the contrast boost factor (1.0 = noticeable, 3.0+ = stark black/white).
func (h ContrastEffect) Strength(s any) ContrastEffect { h.strength.set(s); return h }
func (h ContrastEffect) compileEffect(tmpl *Template) Effect {
h.strength.compileArmed(tmpl, nil, 0)
return h
}
// Dodge exempts the given node from contrast adjustment.
func (h ContrastEffect) Dodge(ref *NodeRef) ContrastEffect { h.dodge = ref; return h }
func (h ContrastEffect) Apply(buf *Buffer, ctx PostContext) {
s := h.strength.resolve()
EachCell(func(x, y int, c Cell, ectx PostContext) Cell {
if h.dodge != nil && inRect(x, y, h.dodge) {
return c
}
c.Style.FG = boostContrast(resolveFG(c.Style.FG, ectx), s)
c.Style.BG = boostContrast(resolveBG(c.Style.BG, ectx), s)
return c
}).Apply(buf, ctx)
}
// ---------------------------------------------------------------------------
// Medium: noticeable, purposeful
// ---------------------------------------------------------------------------
// FocusDimEffect dims everything outside the bounds of a NodeRef.
type FocusDimEffect struct{ ref *NodeRef }
// SEFocusDim dims everything outside the bounds of a NodeRef.
// The ref is populated each frame after layout, so it tracks the node automatically.
func SEFocusDim(ref *NodeRef) FocusDimEffect { return FocusDimEffect{ref: ref} }
func (f FocusDimEffect) Apply(buf *Buffer, ctx PostContext) {
rx, ry := f.ref.X, f.ref.Y
rw, rh := f.ref.W, f.ref.H
for y := range ctx.Height {
base := y * buf.width
inY := y >= ry && y < ry+rh
for x := range ctx.Width {
if inY && x >= rx && x < rx+rw {
continue
}
buf.cells[base+x].Style.Attr = buf.cells[base+x].Style.Attr.With(AttrDim)
}
}
}
type PulseEffect struct {
speed dynFloat64
strength dynFloat64
}
func SEPulse() PulseEffect {
return PulseEffect{speed: dynFloat64{val: 1.0}, strength: dynFloat64{val: 0.3}}
}
// Speed sets oscillation frequency in cycles per second.
func (p PulseEffect) Speed(s any) PulseEffect { p.speed.set(s); return p }
// Strength sets how much brightness dims at the trough (0.3 = subtle, 0.8 = dramatic).
func (p PulseEffect) Strength(s any) PulseEffect { p.strength.set(s); return p }
func (p PulseEffect) compileEffect(tmpl *Template) Effect {
p.speed.compileArmed(tmpl, nil, 0)
p.strength.compileArmed(tmpl, nil, 0)
return p
}
func (p PulseEffect) Apply(buf *Buffer, ctx PostContext) {
black := Color{Mode: ColorRGB}
t := (math.Sin(ctx.Time.Seconds()*p.speed.resolve()*math.Pi*2) + 1) * 0.5
dim := t * p.strength.resolve()
for y := range ctx.Height {
base := y * buf.width
for x := range ctx.Width {
idx := base + x
c := &buf.cells[idx]
c.Style.FG = lerpIfRGB(resolveFG(c.Style.FG, ctx), black, dim)
c.Style.BG = lerpIfRGB(resolveBG(c.Style.BG, ctx), black, dim)
}
}
}
// GradientMapEffect remaps all colour luminance through a three-stop gradient.
type GradientMapEffect struct{ dark, mid, bright Color }
// SEGradientMap remaps all colour luminance through a three-stop gradient.
// Dark shades map to the first colour, midtones to the second, highlights to the third.
func SEGradientMap(dark, mid, bright Color) GradientMapEffect {
return GradientMapEffect{dark: dark, mid: mid, bright: bright}
}
func (g GradientMapEffect) Apply(buf *Buffer, ctx PostContext) {
EachCell(func(_, _ int, c Cell, ectx PostContext) Cell {
c.Style.FG = gradientMap(resolveFG(c.Style.FG, ectx), g.dark, g.mid, g.bright)
c.Style.BG = gradientMap(resolveBG(c.Style.BG, ectx), g.dark, g.mid, g.bright)
return c
}).Apply(buf, ctx)
}
// ---------------------------------------------------------------------------
// Visual flair
// ---------------------------------------------------------------------------
// DropShadowEffect is a glow/drop-shadow — the inverse of vignette.
// Where vignette darkens from the screen edges inward, this darkens outward
// from a focus node's perimeter. At offset (0,0) it's a symmetric glow.
// Any offset displaces the shadow source, giving a directional drop shadow.
type DropShadowEffect struct {
strength dynFloat64
opacity dynFloat64
radius dynInt
offsetX int
offsetY int
tint dynColor
opacityMode OpacityMode
focus *NodeRef
}
// SEDropShadow creates a radial glow/shadow emanating outward from a focus node.
// Default: radius 8, strength 0.2, offset (-1,-1) for a subtle directional shadow.
// Chain .Focus(&ref) to set the source node, .Offset(x,y) to adjust direction.
func SEDropShadow() DropShadowEffect {
return DropShadowEffect{
strength: dynFloat64{val: 0.2},
opacity: dynFloat64{val: 1.0},
radius: dynInt{val: 8},
offsetX: -1,
offsetY: -1,
tint: dynColor{val: Color{Mode: ColorRGB}},
opacityMode: OpacitySmooth,
}
}
// Strength sets shadow darkness (0.0 = none, 1.0 = full black at source edge).
func (d DropShadowEffect) Strength(s any) DropShadowEffect { d.strength.set(s); return d }
func (d DropShadowEffect) compileEffect(tmpl *Template) Effect {
d.strength.compileArmed(tmpl, nil, 0)
d.opacity.compileArmed(tmpl, nil, 0)
d.radius.compile(tmpl)
d.tint.compile(tmpl)
return d
}
// Opacity sets the compositor opacity for the shadow surface. The focused
// node's own opacity is also applied automatically, so shadows fade with the
// thing that casts them.
func (d DropShadowEffect) Opacity(o any) DropShadowEffect { d.opacity.set(o); return d }
// OpacityMode sets how shadow cells hand back to backing runes during fades.
func (d DropShadowEffect) OpacityMode(mode OpacityMode) DropShadowEffect {
d.opacityMode = mode
return d
}
// Radius sets how far the shadow spreads in cells.
func (d DropShadowEffect) Radius(r any) DropShadowEffect { d.radius.set(r); return d }
// Offset displaces the shadow source — turns the symmetric glow into a directional drop shadow.
func (d DropShadowEffect) Offset(x, y int) DropShadowEffect { d.offsetX = x; d.offsetY = y; return d }
// Tint sets the shadow colour (default black).
func (d DropShadowEffect) Tint(c any) DropShadowEffect { d.tint.set(c); return d }
// Focus sets the node the shadow emanates from.
func (d DropShadowEffect) Focus(ref *NodeRef) DropShadowEffect { d.focus = ref; return d }
func (d DropShadowEffect) Apply(buf *Buffer, ctx PostContext) {
if d.focus == nil {
return
}
ref := d.focus
radius := float64(d.radius.resolve())
effectOpacity := clampOpacity(refOpacity(ref) * d.opacity.resolve())
strength := d.strength.resolve()
if strength <= 0 || radius <= 0 {
return
}
radiusI := int(math.Ceil(radius))
sx, sy := ref.X+d.offsetX, ref.Y+d.offsetY
minX := max(0, sx-radiusI)
maxX := min(ctx.Width, sx+ref.W+radiusI)
minY := max(0, sy-radiusI)
maxY := min(ctx.Height, sy+ref.H+radiusI)
for y := minY; y < maxY; y++ {
if y > buf.dirtyMaxY {
buf.dirtyMaxY = y
}
buf.dirtyRows[y] = true
}
if effectOpacity <= 0 {
return
}
for y := minY; y < maxY; y++ {
for x := minX; x < maxX; x++ {
if inRect(x, y, ref) {
continue
}
cx := max(sx, min(x, sx+ref.W-1))
cy := max(sy, min(y, sy+ref.H-1))
dx := float64(x - cx)
dy := float64(y-cy) * 2
dist := math.Sqrt(dx*dx + dy*dy)
if dist >= radius {
continue
}
t := 1.0 - dist/radius
dim := t * t * strength * effectOpacity
tintColor := d.tint.resolve()
c := &buf.cells[y*buf.width+x]
c.Style.FG = lerpIfRGB(resolveFG(c.Style.FG, ctx), tintColor, dim)
c.Style.BG = lerpIfRGB(resolveBG(c.Style.BG, ctx), tintColor, dim)
}
}
}
// GlowEffect emanates light outward from a focus node, sampling the node's
// edge colours and boosting them — the glow takes on the colour of the content.
type GlowEffect struct {
strength dynFloat64
radius dynInt
brightness dynFloat64
focus *NodeRef
}
// SEGlow creates a colour-sampling glow that reads the focus node's edge pixels
// and spills a brightened version of those colours into the surrounding area.
// Default: radius 8, strength 0.5, brightness 1.4.
func SEGlow() GlowEffect {
return GlowEffect{
strength: dynFloat64{val: 0.5},
radius: dynInt{val: 8},
brightness: dynFloat64{val: 1.4},
}
}
// Strength sets how strongly the glow blends into surrounding cells.
func (g GlowEffect) Strength(s any) GlowEffect { g.strength.set(s); return g }
func (g GlowEffect) compileEffect(tmpl *Template) Effect {
g.strength.compileArmed(tmpl, nil, 0)
g.radius.compile(tmpl)
g.brightness.compileArmed(tmpl, nil, 0)
return g
}
// Radius sets how far the glow spreads in cells.
func (g GlowEffect) Radius(r any) GlowEffect { g.radius.set(r); return g }
// Brightness sets the boost applied to sampled edge colours (1.0 = no boost).
func (g GlowEffect) Brightness(b any) GlowEffect { g.brightness.set(b); return g }
// Focus sets the node the glow emanates from.
func (g GlowEffect) Focus(ref *NodeRef) GlowEffect { g.focus = ref; return g }
func (g GlowEffect) Apply(buf *Buffer, ctx PostContext) {
if g.focus == nil {
return
}
ref := g.focus
radius := float64(g.radius.resolve())
strength := g.strength.resolve() * refOpacity(ref)
if strength <= 0 {
return
}
for y := range ctx.Height {
base := y * buf.width
for x := range ctx.Width {
if inRect(x, y, ref) {
continue
}
ex := max(ref.X, min(x, ref.X+ref.W-1))
ey := max(ref.Y, min(y, ref.Y+ref.H-1))
dx := float64(x - ex)
dy := float64(y-ey) * 2
dist := math.Sqrt(dx*dx + dy*dy)
if dist >= radius {
continue
}
edge := buf.Get(ex, ey)
sample := resolveBG(edge.Style.BG, ctx)
if sample.Mode != ColorRGB {
continue
}
bright := g.brightness.resolve()
boosted := Color{
Mode: ColorRGB,
R: uint8(min(int(float64(sample.R)*bright), 255)),
G: uint8(min(int(float64(sample.G)*bright), 255)),
B: uint8(min(int(float64(sample.B)*bright), 255)),
}
t := 1.0 - dist/radius
blend := t * t * strength
c := &buf.cells[base+x]
c.Style.FG = lerpIfRGB(resolveFG(c.Style.FG, ctx), boosted, blend)
c.Style.BG = lerpIfRGB(resolveBG(c.Style.BG, ctx), boosted, blend)
}
}
}
// SpinGlowEffect emanates a coloured halo around a focus node and rotates it.
// N palette colours become N evenly-spaced stops around the ring, lerped between
// neighbours. Angular intensity is also modulated so a bright arc traces the
// perimeter — even a single-colour palette visibly spins.
type SpinGlowEffect struct {
strength dynFloat64
opacity dynFloat64
radius dynInt
speed dynFloat64
falloff dynFloat64
opacityMode OpacityMode
palette []Color
phase *spinGlowPhase
// paletteRef, when non-nil, is dereferenced per-frame in Apply so the
// effect's palette can change at runtime by reassigning the slice the
// caller holds. Wins over `palette` when both are set.
paletteRef *[]Color
focus *NodeRef
rim bool
}
type spinGlowPhase struct {
value float64
lastTime time.Duration
initialized bool
}
func (p *spinGlowPhase) advance(ctx PostContext, speed float64) float64 {
if !p.initialized {
p.initialized = true
p.lastTime = ctx.Time
return p.value
}
delta := ctx.Delta
if delta <= 0 && ctx.Time > p.lastTime {
delta = ctx.Time - p.lastTime
}
p.lastTime = ctx.Time
p.value += delta.Seconds() * speed
return p.value
}
// defaultSpinGlowPalette — pink → rose → purple, matching the glyph website vibe.
var defaultSpinGlowPalette = []Color{
{Mode: ColorRGB, R: 255, G: 80, B: 120},
{Mode: ColorRGB, R: 255, G: 140, B: 100},
{Mode: ColorRGB, R: 200, G: 100, B: 255},
}
// SESpinGlow creates a rotating radial halo around a focus node. Pass zero
// colours for the default palette, or one+ colours to define a conic gradient
// of stops that rotates with time.
//
// SESpinGlow(&ref) // default palette
// SESpinGlow(&ref, RGB(255, 80, 120)) // single-tint hotspot
// SESpinGlow(&ref, RGB(255,80,120), RGB(150,100,255)) // two-stop swirl
func SESpinGlow(focus *NodeRef, palette ...Color) SpinGlowEffect {
if len(palette) == 0 {
palette = defaultSpinGlowPalette
}
return SpinGlowEffect{
strength: dynFloat64{val: 0.7},
opacity: dynFloat64{val: 1.0},
radius: dynInt{val: 10},
speed: dynFloat64{val: 2.1}, // ~360° / 3s, matches glyph-website install pill
falloff: dynFloat64{val: 1.0}, // deviation from linear; 1 = quadratic
opacityMode: OpacityPaint,
palette: palette,
focus: focus,
}
}
// Strength sets how strongly the glow blends into surrounding cells (0..1).
func (s SpinGlowEffect) Strength(v any) SpinGlowEffect { s.strength.set(v); return s }
// Opacity sets the compositor opacity for the whole spin glow effect. Unlike
// Strength, this is intended for fades: it scales the halo and the foreground
// rim without changing the fully-on rim solidity.
func (s SpinGlowEffect) Opacity(v any) SpinGlowEffect { s.opacity.set(v); return s }
// OpacityMode sets how rim cells hand back to backing runes as effect opacity
// fades. The default is OpacityPaint so a mostly-opaque rim remains solid.
func (s SpinGlowEffect) OpacityMode(mode OpacityMode) SpinGlowEffect {
s.opacityMode = mode
return s
}
// Radius sets how far the glow spreads in cells.
func (s SpinGlowEffect) Radius(v any) SpinGlowEffect { s.radius.set(v); return s }
// Speed sets rotation speed in radians per second. Zero freezes the glow.
func (s SpinGlowEffect) Speed(v any) SpinGlowEffect { s.speed.set(v); return s }
// Falloff shapes the halo's intensity curve between the rect and the radius.
// 0 = linear 1→0 (baseline). Higher values concentrate the drop-off closer
// to the rect — intensity falls quickly just outside the rect, then trails
// off more gently toward the radius. The curve is blended 50/50 with a
// linear term so the halo stays visible all the way to the radius at every
// Falloff value — Radius is declarative, it's always the visible disappearing
// point regardless of the curve shape. fast-pathed for integer values 0..3.
func (s SpinGlowEffect) Falloff(v any) SpinGlowEffect { s.falloff.set(v); return s }
// Rim enables painting the focus node's border perimeter with the rotating
// conic palette — analogous to a CSS conic-gradient stroke on a border.
// Overrides existing border FG. Use on containers with a visible border for
// a rotating-rim look; pair with a small Radius for "rim with a soft outer
// halo" that mirrors the glyph-website install pill.
func (s SpinGlowEffect) Rim(v bool) SpinGlowEffect { s.rim = v; return s }
// PaletteRef wires a per-frame palette source. The pointer is dereferenced
// each Apply, so callers can swap the palette live just by reassigning the
// slice variable the pointer addresses — no effect rebuild required.
// When set, supersedes the palette passed at construction.
func (s SpinGlowEffect) PaletteRef(p *[]Color) SpinGlowEffect {
s.paletteRef = p
return s
}
func (s SpinGlowEffect) compileEffect(tmpl *Template) Effect {
s.strength.compileArmed(tmpl, nil, 0)
s.opacity.compileArmed(tmpl, nil, 0)
s.radius.compile(tmpl)
s.speed.compileArmed(tmpl, nil, 0)
s.falloff.compileArmed(tmpl, nil, 0)
s.phase = &spinGlowPhase{}
return s
}
func (s SpinGlowEffect) Apply(buf *Buffer, ctx PostContext) {
palette := s.palette
if s.paletteRef != nil && len(*s.paletteRef) > 0 {
palette = *s.paletteRef
}
if s.focus == nil || len(palette) == 0 {
return
}
ref := s.focus
radius := float64(s.radius.resolve())
if radius <= 0 {
return
}
effectOpacity := clampOpacity(refOpacity(ref) * s.opacity.resolve())
if effectOpacity <= 0 {
return
}
strength := s.strength.resolve() * effectOpacity
speed := s.speed.resolve()
phase := ctx.Time.Seconds() * speed
if s.phase != nil {
phase = s.phase.advance(ctx, speed)
}
fall := s.falloff.resolve()
// adaptive blend between a linear baseline and the power curve. the
// linear weight shrinks as falloff grows — so higher falloff lets the
// power curve dominate the shape — but is floored at 0.15 so the tail
// stays visibly reaching the radius at any falloff value.
linW := 1.0 / (1.0 + fall)
if linW < 0.15 {
linW = 0.15
}
powW := 1.0 - linW
n := float64(len(palette))
// clip iteration to the bounding box of the halo's reach. cells far
// outside can never satisfy `dist < radius`, so scanning them is wasted
// work.
radiusI := int(math.Ceil(radius))
minX := max(0, ref.X-radiusI)
maxX := min(ctx.Width, ref.X+ref.W+radiusI)
minY := max(0, ref.Y-radiusI)
maxY := min(ctx.Height, ref.Y+ref.H+radiusI)
if strength > 0 {
for y := minY; y < maxY; y++ {
base := y * buf.width
rowPainted := false
for x := minX; x < maxX; x++ {
if inRect(x, y, ref) {
continue
}
// radial distance from the nearest rect edge (same as drop shadow).
ex := max(ref.X, min(x, ref.X+ref.W-1))
ey := max(ref.Y, min(y, ref.Y+ref.H-1))
dxEdge := float64(x - ex)
dyEdge := float64(y - ey)
dist := math.Sqrt(dxEdge*dxEdge + dyEdge*dyEdge)
if dist >= radius {
continue
}
c := &buf.cells[base+x]
// Project halo cells onto the same rectangular perimeter path
// as the rim. A polar angle around a wide, short rect makes
// nearby x/y cells sample visibly different bands.
tint := sampleSpinGlowRectColor(x, y, ref, phase, n, palette)
// radial falloff: linW*t + powW*t^(1+fall). higher fall pulls
// the drop-off closer to the rect, while the linear term keeps
// the halo visible all the way to the radius — so the declared
// Radius is always the visual disappearing point, regardless
// of curve shape. integer values 0..3 are fast-pathed to avoid
// math.Pow. rotation signal comes purely from palette position
// shift, not intensity modulation.
t := 1.0 - dist/radius
var radial float64
switch fall {
case 0.0:
radial = t
case 1.0:
radial = linW*t + powW*t*t
case 2.0:
radial = linW*t + powW*t*t*t
case 3.0:
t2 := t * t
radial = linW*t + powW*t2*t2
default:
radial = linW*t + powW*math.Pow(t, 1+fall)
}
blend := radial * strength
c.Style.FG = lerpIfRGB(resolveFG(c.Style.FG, ctx), tint, blend)
c.Style.BG = lerpIfRGB(resolveBG(c.Style.BG, ctx), tint, blend)
rowPainted = true
}
// we mutate cells by direct pointer, so the buffer's per-row dirty
// tracking never sees these writes. without marking, ClearDirty()
// would skip the rows we painted (if they're past rendered content)
// and paint would accumulate across frames. mark only the rows we
// actually touched — keeps the per-row-clear optimisation intact.
if rowPainted {
if y > buf.dirtyMaxY {
buf.dirtyMaxY = y
}
buf.dirtyRows[y] = true
}
}
}
if s.rim {
s.paintRim(buf, ctx, phase, n, palette, effectOpacity)
}
}
func sampleSpinGlowRectColor(x, y int, ref *NodeRef, phase, n float64, palette []Color) Color {
top := ref.Y - 1
bottom := ref.Y + ref.H
left := ref.X - 1
right := ref.X + ref.W
width := right - left + 1
side := bottom - top - 1
total := 2*width + 2*side
if total <= 0 {
return palette[0]
}
px := min(max(x, left), right)
py := min(max(y, top), bottom)
if x < left {
px = left
} else if x > right {
px = right
}
if y < top {
py = top
} else if y > bottom {
py = bottom
}
var idx int
switch {
case py == top:
idx = px - left
case px == right:
idx = width + (py - top - 1)
case py == bottom:
idx = width + side + (right - px)
default:
idx = width + side + width + (bottom - py - 1)
}
return sampleSpinGlowPerimeterIndex(idx, total, phase, n, palette)
}
func sampleSpinGlowPerimeterIndex(idx, total int, phase, n float64, palette []Color) Color {
phaseNorm := phase / (2 * math.Pi)
norm := float64(idx)/float64(total) - phaseNorm
norm -= math.Floor(norm)
p := norm * n
i0 := int(p) % len(palette)
i1 := (i0 + 1) % len(palette)
frac := p - math.Floor(p)
return Lerp(palette[i0], palette[i1], frac)
}
// paintRim draws a 1-cell-thick coloured stroke OUTSIDE the focus node.
// Each perimeter cell samples the palette by distance along the painted
// rectangle, not by atan2 from the centre. That keeps colour changes even
// across coarse terminal cells, especially on wide/short cards where polar
// sampling makes adjacent x/y edge cells jump through very different parts
// of the gradient.
func (s SpinGlowEffect) paintRim(buf *Buffer, ctx PostContext, phase, n float64, palette []Color, opacity float64) {
ref := s.focus
top := ref.Y - 1
bottom := ref.Y + ref.H
left := ref.X - 1
right := ref.X + ref.W
total := 2*(right-left+1) + 2*(bottom-top-1)
if total <= 0 {
return
}
sample := func(idx int) Color {
return sampleSpinGlowPerimeterIndex(idx, total, phase, n, palette)
}
paintHalfBlock := func(x, y int, r rune, idx int) {
if x < 0 || x >= ctx.Width || y < 0 || y >= ctx.Height {
return
}
tint := sample(idx)
// paintRim owns the perimeter cells: always write the block
// rune, otherwise text in those cells stays visible underneath
// the rim's tint and the rim looks broken. Leave BG alone: the
// block glyph carries the stroke geometry, while BG is a full-cell
// fill and would make half-height top/bottom strokes look too thick.
buf.SetOpacity(x, y, Cell{Rune: r, Style: Style{FG: tint, Attr: AttrNone}}, opacity, s.opacityMode)
// c.Style.BG = Magenta <- set this and you will screw up the border
}
paintFullCell := func(x, y int, idx int) {
if x < 0 || x >= ctx.Width || y < 0 || y >= ctx.Height {
return
}