-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathgradient.go
More file actions
163 lines (138 loc) · 3.72 KB
/
Copy pathgradient.go
File metadata and controls
163 lines (138 loc) · 3.72 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
package gg
import (
"math"
"sort"
"github.com/gogpu/gg/internal/color"
)
// ExtendMode defines how gradients extend beyond their defined bounds.
type ExtendMode int
const (
// ExtendPad extends edge colors beyond bounds (default behavior).
ExtendPad ExtendMode = iota
// ExtendRepeat repeats the gradient pattern.
ExtendRepeat
// ExtendReflect mirrors the gradient pattern.
ExtendReflect
)
// ColorStop represents a color at a specific position in a gradient.
type ColorStop struct {
Offset float64 // Position in gradient, 0.0 to 1.0
Color RGBA // Color at this position
}
// sortStops sorts color stops by offset and removes duplicates.
func sortStops(stops []ColorStop) []ColorStop {
if len(stops) == 0 {
return stops
}
// Create a copy to avoid modifying the original
sorted := make([]ColorStop, len(stops))
copy(sorted, stops)
sort.Slice(sorted, func(i, j int) bool {
return sorted[i].Offset < sorted[j].Offset
})
return sorted
}
// applyExtendMode applies the extend mode to normalize t to [0, 1].
func applyExtendMode(t float64, mode ExtendMode) float64 {
switch mode {
case ExtendRepeat:
t -= math.Floor(t)
if t < 0 {
t++
}
case ExtendReflect:
t = math.Abs(t)
period := math.Floor(t)
t -= period
if int(period)%2 == 1 {
t = 1 - t
}
default: // ExtendPad
t = clamp01(t)
}
return t
}
// clamp01 clamps a value to [0, 1] range.
func clamp01(x float64) float64 {
if x < 0 {
return 0
}
if x > 1 {
return 1
}
return x
}
// interpolateColorLinear performs linear interpolation between two colors in linear sRGB space.
// This produces perceptually correct color blending.
func interpolateColorLinear(c1, c2 RGBA, t float64) RGBA {
// Convert to internal color format (float32)
col1 := color.ColorF32{
R: float32(c1.R),
G: float32(c1.G),
B: float32(c1.B),
A: float32(c1.A),
}
col2 := color.ColorF32{
R: float32(c2.R),
G: float32(c2.G),
B: float32(c2.B),
A: float32(c2.A),
}
// Convert to linear space
linear1 := color.SRGBToLinearColor(col1)
linear2 := color.SRGBToLinearColor(col2)
// Interpolate in linear space
t32 := float32(t)
interpolated := color.ColorF32{
R: linear1.R + t32*(linear2.R-linear1.R),
G: linear1.G + t32*(linear2.G-linear1.G),
B: linear1.B + t32*(linear2.B-linear1.B),
A: linear1.A + t32*(linear2.A-linear1.A),
}
// Convert back to sRGB
result := color.LinearToSRGBColor(interpolated)
return RGBA{
R: float64(result.R),
G: float64(result.G),
B: float64(result.B),
A: float64(result.A),
}
}
// colorAtOffset returns the interpolated color at a given offset.
// Handles edge cases: empty stops, single stop, out-of-bounds t.
// The stops slice MUST be pre-sorted by offset. Callers are responsible
// for sorting stops once (at AddColorStop time or lazily before first use).
func colorAtOffset(stops []ColorStop, t float64, mode ExtendMode) RGBA {
// Edge case: no stops
if len(stops) == 0 {
return Transparent
}
// Edge case: single stop
if len(stops) == 1 {
return stops[0].Color
}
// Apply extend mode to normalize t
t = applyExtendMode(t, mode)
// Find the two stops to interpolate between
// Binary search for efficiency (no allocation)
idx := sort.Search(len(stops), func(i int) bool {
return stops[i].Offset >= t
})
// Handle edge cases after extend mode
if idx == 0 {
return stops[0].Color
}
if idx >= len(stops) {
return stops[len(stops)-1].Color
}
// Interpolate between stops[idx-1] and stops[idx]
stop1 := stops[idx-1]
stop2 := stops[idx]
// Avoid division by zero for coincident stops
if stop2.Offset == stop1.Offset {
return stop1.Color
}
// Calculate interpolation factor
localT := (t - stop1.Offset) / (stop2.Offset - stop1.Offset)
return interpolateColorLinear(stop1.Color, stop2.Color, localT)
}