Repository navigation
Expand file tree
/
Copy pathgradient_extended_test.go
More file actions
537 lines (464 loc) · 14.2 KB
/
Copy pathgradient_extended_test.go
File metadata and controls
537 lines (464 loc) · 14.2 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
package gg
import (
"math"
"testing"
)
// --- Extended ExtendMode Tests ---
func TestApplyExtendMode_LargeValues(t *testing.T) {
tests := []struct {
name string
t float64
mode ExtendMode
want float64
}{
{"repeat large positive", 5.75, ExtendRepeat, 0.75},
{"repeat large negative", -3.25, ExtendRepeat, 0.75},
{"reflect 3.25", 3.25, ExtendReflect, 0.75},
{"reflect 4.5", 4.5, ExtendReflect, 0.5},
{"pad large positive", 100, ExtendPad, 1},
{"pad large negative", -100, ExtendPad, 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := applyExtendMode(tt.t, tt.mode)
if math.Abs(got-tt.want) > 0.001 {
t.Errorf("applyExtendMode(%v, %v) = %v, want %v", tt.t, tt.mode, got, tt.want)
}
})
}
}
// --- clamp01 Tests ---
func TestClamp01(t *testing.T) {
tests := []struct {
name string
x float64
want float64
}{
{"negative", -0.5, 0},
{"zero", 0, 0},
{"middle", 0.5, 0.5},
{"one", 1.0, 1.0},
{"over", 1.5, 1.0},
{"large negative", -100, 0},
{"large positive", 100, 1},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := clamp01(tt.x)
if got != tt.want {
t.Errorf("clamp01(%v) = %v, want %v", tt.x, got, tt.want)
}
})
}
}
// --- colorAtOffset Tests ---
func TestColorAtOffset_EdgeCases(t *testing.T) {
t.Run("empty stops returns transparent", func(t *testing.T) {
got := colorAtOffset(nil, 0.5, ExtendPad)
if !colorsEqual(got, Transparent, 0.001) {
t.Errorf("colorAtOffset(nil) = %+v, want Transparent", got)
}
})
t.Run("single stop returns that color", func(t *testing.T) {
stops := []ColorStop{{Offset: 0.5, Color: Red}}
got := colorAtOffset(stops, 0.5, ExtendPad)
if !colorsEqual(got, Red, 0.001) {
t.Errorf("colorAtOffset(single) = %+v, want Red", got)
}
})
t.Run("t before first stop", func(t *testing.T) {
stops := []ColorStop{
{Offset: 0.25, Color: Red},
{Offset: 0.75, Color: Blue},
}
got := colorAtOffset(stops, 0.1, ExtendPad)
if !colorsEqual(got, Red, 0.001) {
t.Errorf("colorAtOffset(before first) = %+v, want Red", got)
}
})
t.Run("t after last stop", func(t *testing.T) {
stops := []ColorStop{
{Offset: 0.25, Color: Red},
{Offset: 0.75, Color: Blue},
}
got := colorAtOffset(stops, 0.9, ExtendPad)
if !colorsEqual(got, Blue, 0.001) {
t.Errorf("colorAtOffset(after last) = %+v, want Blue", got)
}
})
t.Run("coincident stops", func(t *testing.T) {
stops := []ColorStop{
{Offset: 0.5, Color: Red},
{Offset: 0.5, Color: Blue},
}
got := colorAtOffset(stops, 0.5, ExtendPad)
// Should return the first stop's color (no division by zero)
if !colorsEqual(got, Red, 0.01) && !colorsEqual(got, Blue, 0.01) {
t.Errorf("colorAtOffset(coincident) should return one of the stop colors, got %+v", got)
}
})
t.Run("pre-sorted stops work correctly", func(t *testing.T) {
stops := sortStops([]ColorStop{
{Offset: 1.0, Color: Blue},
{Offset: 0.0, Color: Red},
})
got := colorAtOffset(stops, 0, ExtendPad)
if !colorsEqual(got, Red, 0.001) {
t.Errorf("colorAtOffset(sorted, t=0) = %+v, want Red", got)
}
})
}
func TestColorAtOffset_Interpolation(t *testing.T) {
stops := []ColorStop{
{Offset: 0, Color: Red},
{Offset: 1, Color: Blue},
}
// At exact start offset
got := colorAtOffset(stops, 0, ExtendPad)
if !colorsEqual(got, Red, 0.01) {
t.Errorf("t=0 should be Red, got %+v", got)
}
// At exact end offset
got = colorAtOffset(stops, 1, ExtendPad)
if !colorsEqual(got, Blue, 0.01) {
t.Errorf("t=1 should be Blue, got %+v", got)
}
// Midpoint should be neither Red nor Blue
got = colorAtOffset(stops, 0.5, ExtendPad)
if colorsEqual(got, Red, 0.1) || colorsEqual(got, Blue, 0.1) {
t.Errorf("t=0.5 should be midpoint, not endpoint, got %+v", got)
}
}
func TestColorAtOffset_ThreeStops(t *testing.T) {
stops := []ColorStop{
{Offset: 0, Color: Red},
{Offset: 0.5, Color: Green},
{Offset: 1, Color: Blue},
}
// At stop positions should return exact colors
tests := []struct {
t float64
want RGBA
}{
{0, Red},
{0.5, Green},
{1, Blue},
}
for _, tt := range tests {
got := colorAtOffset(stops, tt.t, ExtendPad)
if !colorsEqual(got, tt.want, 0.01) {
t.Errorf("colorAtOffset(3 stops, t=%v) = %+v, want %+v", tt.t, got, tt.want)
}
}
}
// --- sortStops Tests ---
func TestSortStops_SingleStop(t *testing.T) {
stops := []ColorStop{{Offset: 0.5, Color: Red}}
got := sortStops(stops)
if len(got) != 1 {
t.Errorf("sortStops(single) len = %d, want 1", len(got))
}
if got[0].Offset != 0.5 {
t.Errorf("sortStops(single) offset = %v, want 0.5", got[0].Offset)
}
}
func TestSortStops_DoesNotModifyOriginal(t *testing.T) {
stops := []ColorStop{
{Offset: 1, Color: Blue},
{Offset: 0, Color: Red},
}
original := make([]ColorStop, len(stops))
copy(original, stops)
_ = sortStops(stops)
// Original should be unchanged
for i, s := range stops {
if s.Offset != original[i].Offset {
t.Errorf("sortStops modified original: stops[%d].Offset = %v, want %v",
i, s.Offset, original[i].Offset)
}
}
}
// --- Linear Gradient Extended Tests ---
func TestLinearGradientBrush_Diagonal(t *testing.T) {
g := NewLinearGradientBrush(0, 0, 100, 100).
AddColorStop(0, Red).
AddColorStop(1, Blue)
// Point on the gradient line at start
startColor := g.ColorAt(0, 0)
if !colorsEqual(startColor, Red, gradientEpsilon) {
t.Errorf("Diagonal start = %+v, want Red", startColor)
}
// Point on the gradient line at end
endColor := g.ColorAt(100, 100)
if !colorsEqual(endColor, Blue, gradientEpsilon) {
t.Errorf("Diagonal end = %+v, want Blue", endColor)
}
}
func TestLinearGradientBrush_SetExtend(t *testing.T) {
g := NewLinearGradientBrush(0, 0, 100, 0)
if g.Extend != ExtendPad {
t.Errorf("Default extend = %v, want ExtendPad", g.Extend)
}
g.SetExtend(ExtendRepeat)
if g.Extend != ExtendRepeat {
t.Errorf("After SetExtend(Repeat) = %v, want ExtendRepeat", g.Extend)
}
g.SetExtend(ExtendReflect)
if g.Extend != ExtendReflect {
t.Errorf("After SetExtend(Reflect) = %v, want ExtendReflect", g.Extend)
}
}
func TestLinearGradientBrush_ExtendReflect(t *testing.T) {
g := NewLinearGradientBrush(0, 0, 100, 0).
AddColorStop(0, Red).
AddColorStop(1, Blue).
SetExtend(ExtendReflect)
// At 200 (2x range), should reflect back to start
got := g.ColorAt(200, 0)
if !colorsEqual(got, Red, 0.05) {
t.Errorf("ExtendReflect at 200 should be near Red, got %+v", got)
}
}
func TestLinearGradientBrush_PerpendicularPoint(t *testing.T) {
// Horizontal gradient (0,0) -> (100,0)
g := NewLinearGradientBrush(0, 0, 100, 0).
AddColorStop(0, Red).
AddColorStop(1, Blue)
// Points at same X but different Y should get same color
c1 := g.ColorAt(50, 0)
c2 := g.ColorAt(50, 100)
c3 := g.ColorAt(50, -50)
if !colorsEqual(c1, c2, 0.001) {
t.Errorf("Perpendicular should give same color: %+v vs %+v", c1, c2)
}
if !colorsEqual(c1, c3, 0.001) {
t.Errorf("Perpendicular should give same color: %+v vs %+v", c1, c3)
}
}
// --- Radial Gradient Extended Tests ---
func TestRadialGradientBrush_SimpleSymmetry(t *testing.T) {
g := NewRadialGradientBrush(50, 50, 0, 50).
AddColorStop(0, Red).
AddColorStop(1, Blue)
// Points equidistant from center should get same color
c1 := g.ColorAt(75, 50) // 25 units right
c2 := g.ColorAt(25, 50) // 25 units left
c3 := g.ColorAt(50, 75) // 25 units down
c4 := g.ColorAt(50, 25) // 25 units up
if !colorsEqual(c1, c2, 0.01) {
t.Errorf("Radial symmetry: right %+v vs left %+v", c1, c2)
}
if !colorsEqual(c1, c3, 0.01) {
t.Errorf("Radial symmetry: right %+v vs down %+v", c1, c3)
}
if !colorsEqual(c1, c4, 0.01) {
t.Errorf("Radial symmetry: right %+v vs up %+v", c1, c4)
}
}
func TestRadialGradientBrush_FocalAtCenter(t *testing.T) {
// When focus equals center, SetFocus should not change behavior
g := NewRadialGradientBrush(50, 50, 0, 50).
SetFocus(50, 50). // Same as center
AddColorStop(0, Red).
AddColorStop(1, Blue)
centerColor := g.ColorAt(50, 50)
if !colorsEqual(centerColor, Red, gradientEpsilon) {
t.Errorf("Focus at center, center color = %+v, want Red", centerColor)
}
}
func TestRadialGradientBrush_FocalCompute(t *testing.T) {
g := NewRadialGradientBrush(50, 50, 0, 50).
SetFocus(30, 30).
AddColorStop(0, Red).
AddColorStop(1, Blue)
// computeTFocal should be called when focus != center
t1 := g.computeT(30, 30) // At focus
if math.Abs(t1) > 0.001 {
t.Errorf("computeT at focus should be ~0, got %v", t1)
}
}
func TestRadialGradientBrush_SingleStop(t *testing.T) {
g := NewRadialGradientBrush(50, 50, 0, 50).
AddColorStop(0.5, Green)
got := g.ColorAt(50, 50)
if !colorsEqual(got, Green, gradientEpsilon) {
t.Errorf("Single stop radial = %+v, want Green", got)
}
}
func TestRadialGradientBrush_ExtendReflect(t *testing.T) {
g := NewRadialGradientBrush(50, 50, 0, 25).
AddColorStop(0, Red).
AddColorStop(1, Blue).
SetExtend(ExtendReflect)
if g.Extend != ExtendReflect {
t.Errorf("Extend = %v, want ExtendReflect", g.Extend)
}
}
// --- Sweep Gradient Extended Tests ---
func TestSweepGradientBrush_SingleStop(t *testing.T) {
g := NewSweepGradientBrush(50, 50, 0).
AddColorStop(0.5, Green)
got := g.ColorAt(100, 50)
if !colorsEqual(got, Green, gradientEpsilon) {
t.Errorf("Single stop sweep = %+v, want Green", got)
}
}
func TestSweepGradientBrush_ZeroSweep(t *testing.T) {
g := NewSweepGradientBrush(50, 50, 0).
SetEndAngle(0). // Zero sweep range
AddColorStop(0, Red).
AddColorStop(1, Blue)
// With zero sweep, angleToT should return 0
got := g.ColorAt(100, 50)
if !colorsEqual(got, Red, gradientEpsilon) {
t.Errorf("Zero sweep should return first stop, got %+v", got)
}
}
func TestSweepGradientBrush_PartialSweep(t *testing.T) {
// Half rotation sweep
g := NewSweepGradientBrush(50, 50, 0).
SetEndAngle(math.Pi).
AddColorStop(0, Red).
AddColorStop(1, Blue)
// At start angle (0 radians = right)
rightColor := g.ColorAt(100, 50)
if !colorsEqual(rightColor, Red, gradientEpsilon) {
t.Errorf("Partial sweep at start = %+v, want Red", rightColor)
}
// At end angle (Pi = left)
leftColor := g.ColorAt(0, 50)
if !colorsEqual(leftColor, Blue, gradientEpsilon) {
t.Errorf("Partial sweep at end = %+v, want Blue", leftColor)
}
}
func TestNormalizeAngle(t *testing.T) {
tests := []struct {
name string
angle float64
sweepRange float64
wantSign int // 1 = positive, -1 = negative, 0 = zero
}{
{"positive sweep, positive angle", math.Pi, 2 * math.Pi, 1},
{"positive sweep, negative angle", -math.Pi, 2 * math.Pi, 1},
{"negative sweep, positive angle", math.Pi, -2 * math.Pi, -1},
{"negative sweep, negative angle", -math.Pi, -2 * math.Pi, -1},
{"zero angle, positive sweep", 0, 2 * math.Pi, 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := normalizeAngle(tt.angle, tt.sweepRange)
switch tt.wantSign {
case 1:
if got < 0 {
t.Errorf("normalizeAngle(%v, %v) = %v, expected >= 0", tt.angle, tt.sweepRange, got)
}
case -1:
if got > 0 {
t.Errorf("normalizeAngle(%v, %v) = %v, expected <= 0", tt.angle, tt.sweepRange, got)
}
case 0:
if math.Abs(got) > 0.001 {
t.Errorf("normalizeAngle(%v, %v) = %v, expected ~0", tt.angle, tt.sweepRange, got)
}
}
})
}
}
// --- Interpolation Extended Tests ---
func TestInterpolateColorLinear_SameColor(t *testing.T) {
got := interpolateColorLinear(Red, Red, 0.5)
if !colorsEqual(got, Red, 0.001) {
t.Errorf("interpolateColorLinear(Red, Red, 0.5) = %+v, want Red", got)
}
}
func TestInterpolateColorLinear_WithAlpha(t *testing.T) {
transparent := RGBA2(1, 0, 0, 0) // Red but fully transparent
opaque := RGBA2(1, 0, 0, 1) // Red fully opaque
// At t=0 should be transparent
got := interpolateColorLinear(transparent, opaque, 0)
if math.Abs(got.A-0) > 0.01 {
t.Errorf("t=0 alpha = %v, want ~0", got.A)
}
// At t=1 should be opaque
got = interpolateColorLinear(transparent, opaque, 1)
if math.Abs(got.A-1) > 0.01 {
t.Errorf("t=1 alpha = %v, want ~1", got.A)
}
// At t=0.5 should be semi-transparent
got = interpolateColorLinear(transparent, opaque, 0.5)
if got.A < 0.1 || got.A > 0.9 {
t.Errorf("t=0.5 alpha = %v, expected between 0.1 and 0.9", got.A)
}
}
// --- firstStopColor Tests ---
func TestFirstStopColor(t *testing.T) {
t.Run("empty returns transparent", func(t *testing.T) {
got := firstStopColor(nil)
if !colorsEqual(got, Transparent, 0.001) {
t.Errorf("firstStopColor(nil) = %+v, want Transparent", got)
}
})
t.Run("single stop", func(t *testing.T) {
stops := []ColorStop{{Offset: 0.5, Color: Green}}
got := firstStopColor(stops)
if !colorsEqual(got, Green, 0.001) {
t.Errorf("firstStopColor(single) = %+v, want Green", got)
}
})
t.Run("sorted returns minimum offset", func(t *testing.T) {
stops := sortStops([]ColorStop{
{Offset: 0.7, Color: Blue},
{Offset: 0.3, Color: Red},
})
got := firstStopColor(stops)
if !colorsEqual(got, Red, 0.001) {
t.Errorf("firstStopColor(sorted) = %+v, want Red (min offset)", got)
}
})
}
// --- Method Chaining Tests ---
func TestLinearGradientBrush_Chaining(t *testing.T) {
g := NewLinearGradientBrush(0, 0, 100, 0).
AddColorStop(0, Red).
AddColorStop(0.5, Green).
AddColorStop(1, Blue).
SetExtend(ExtendRepeat)
if len(g.Stops) != 3 {
t.Errorf("Chaining: stops = %d, want 3", len(g.Stops))
}
if g.Extend != ExtendRepeat {
t.Errorf("Chaining: extend = %v, want ExtendRepeat", g.Extend)
}
}
func TestRadialGradientBrush_Chaining(t *testing.T) {
g := NewRadialGradientBrush(50, 50, 0, 50).
SetFocus(30, 30).
AddColorStop(0, Red).
AddColorStop(1, Blue).
SetExtend(ExtendReflect)
if len(g.Stops) != 2 {
t.Errorf("Chaining: stops = %d, want 2", len(g.Stops))
}
if g.Focus.X != 30 || g.Focus.Y != 30 {
t.Errorf("Chaining: focus = %v, want (30,30)", g.Focus)
}
if g.Extend != ExtendReflect {
t.Errorf("Chaining: extend = %v, want ExtendReflect", g.Extend)
}
}
func TestSweepGradientBrush_Chaining(t *testing.T) {
g := NewSweepGradientBrush(50, 50, 0).
SetEndAngle(math.Pi).
AddColorStop(0, Red).
AddColorStop(1, Blue).
SetExtend(ExtendRepeat)
if len(g.Stops) != 2 {
t.Errorf("Chaining: stops = %d, want 2", len(g.Stops))
}
if g.EndAngle != math.Pi {
t.Errorf("Chaining: endAngle = %v, want Pi", g.EndAngle)
}
if g.Extend != ExtendRepeat {
t.Errorf("Chaining: extend = %v, want ExtendRepeat", g.Extend)
}
}