| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
package gradient |
| 7 |
|
| 8 |
import ( |
| 9 |
"image" |
| 10 |
"image/color" |
| 11 |
"math" |
| 12 |
|
| 13 |
"golang.org/x/image/math/f64" |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
type Shape uint8 |
| 30 |
|
| 31 |
const ( |
| 32 |
ShapeLinear Shape = iota |
| 33 |
ShapeRadial |
| 34 |
) |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
type Spread uint8 |
| 39 |
|
| 40 |
const ( |
| 41 |
|
| 42 |
|
| 43 |
SpreadNone Spread = iota |
| 44 |
|
| 45 |
|
| 46 |
SpreadPad |
| 47 |
|
| 48 |
|
| 49 |
SpreadReflect |
| 50 |
|
| 51 |
|
| 52 |
SpreadRepeat |
| 53 |
) |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
func (s Spread) Clamp(x float64) float64 { |
| 59 |
if x >= 0 { |
| 60 |
if x <= 1 { |
| 61 |
return x |
| 62 |
} |
| 63 |
switch s { |
| 64 |
case SpreadPad: |
| 65 |
return 1 |
| 66 |
case SpreadReflect: |
| 67 |
if int(x)&1 == 0 { |
| 68 |
return x - math.Floor(x) |
| 69 |
} |
| 70 |
return math.Ceil(x) - x |
| 71 |
case SpreadRepeat: |
| 72 |
return x - math.Floor(x) |
| 73 |
} |
| 74 |
return -1 |
| 75 |
} |
| 76 |
switch s { |
| 77 |
case SpreadPad: |
| 78 |
return 0 |
| 79 |
case SpreadReflect: |
| 80 |
x = -x |
| 81 |
if int(x)&1 == 0 { |
| 82 |
return x - math.Floor(x) |
| 83 |
} |
| 84 |
return math.Ceil(x) - x |
| 85 |
case SpreadRepeat: |
| 86 |
return x - math.Floor(x) |
| 87 |
} |
| 88 |
return -1 |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
type Stop struct { |
| 93 |
Offset float64 |
| 94 |
RGBA64 color.RGBA64 |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
type Range struct { |
| 99 |
Offset0 float64 |
| 100 |
Offset1 float64 |
| 101 |
Width float64 |
| 102 |
R0 float64 |
| 103 |
R1 float64 |
| 104 |
G0 float64 |
| 105 |
G1 float64 |
| 106 |
B0 float64 |
| 107 |
B1 float64 |
| 108 |
A0 float64 |
| 109 |
A1 float64 |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
func MakeRange(s0, s1 Stop) Range { |
| 114 |
return Range{ |
| 115 |
Offset0: s0.Offset, |
| 116 |
Offset1: s1.Offset, |
| 117 |
Width: s1.Offset - s0.Offset, |
| 118 |
R0: float64(s0.RGBA64.R), |
| 119 |
R1: float64(s1.RGBA64.R), |
| 120 |
G0: float64(s0.RGBA64.G), |
| 121 |
G1: float64(s1.RGBA64.G), |
| 122 |
B0: float64(s0.RGBA64.B), |
| 123 |
B1: float64(s1.RGBA64.B), |
| 124 |
A0: float64(s0.RGBA64.A), |
| 125 |
A1: float64(s1.RGBA64.A), |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
func AppendRanges(a []Range, stops []Stop) []Range { |
| 132 |
if len(stops) == 0 { |
| 133 |
return nil |
| 134 |
} |
| 135 |
if len(a) != 0 { |
| 136 |
z := a[len(a)-1] |
| 137 |
a = append(a, MakeRange(Stop{ |
| 138 |
Offset: z.Offset1, |
| 139 |
RGBA64: color.RGBA64{ |
| 140 |
R: uint16(z.R1), |
| 141 |
G: uint16(z.G1), |
| 142 |
B: uint16(z.B1), |
| 143 |
A: uint16(z.A1), |
| 144 |
}, |
| 145 |
}, stops[0])) |
| 146 |
} |
| 147 |
for i := 0; i < len(stops)-1; i++ { |
| 148 |
a = append(a, MakeRange(stops[i], stops[i+1])) |
| 149 |
} |
| 150 |
return a |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
type Gradient struct { |
| 156 |
Shape Shape |
| 157 |
Spread Spread |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
Pix2Grad f64.Aff3 |
| 169 |
|
| 170 |
Ranges []Range |
| 171 |
|
| 172 |
|
| 173 |
First, Last color.RGBA64 |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
func (g *Gradient) Init(shape Shape, spread Spread, pix2Grad f64.Aff3, stops []Stop) { |
| 179 |
g.Shape = shape |
| 180 |
g.Spread = spread |
| 181 |
g.Pix2Grad = pix2Grad |
| 182 |
g.Ranges = AppendRanges(g.Ranges[:0], stops) |
| 183 |
if len(stops) == 0 { |
| 184 |
g.First = color.RGBA64{} |
| 185 |
g.Last = color.RGBA64{} |
| 186 |
} else { |
| 187 |
g.First = stops[0].RGBA64 |
| 188 |
g.Last = stops[len(stops)-1].RGBA64 |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
func (g *Gradient) ColorModel() color.Model { |
| 194 |
return color.RGBA64Model |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
func (g *Gradient) Bounds() image.Rectangle { |
| 199 |
return image.Rectangle{ |
| 200 |
Min: image.Point{-1e9, -1e9}, |
| 201 |
Max: image.Point{+1e9, +1e9}, |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
func (g *Gradient) At(x, y int) color.Color { |
| 207 |
if len(g.Ranges) == 0 { |
| 208 |
return color.RGBA64{} |
| 209 |
} |
| 210 |
|
| 211 |
px := float64(x) + 0.5 |
| 212 |
py := float64(y) + 0.5 |
| 213 |
|
| 214 |
offset := 0.0 |
| 215 |
if g.Shape == ShapeLinear { |
| 216 |
offset = g.Spread.Clamp(g.Pix2Grad[0]*px + g.Pix2Grad[1]*py + g.Pix2Grad[2]) |
| 217 |
} else { |
| 218 |
gx := g.Pix2Grad[0]*px + g.Pix2Grad[1]*py + g.Pix2Grad[2] |
| 219 |
gy := g.Pix2Grad[3]*px + g.Pix2Grad[4]*py + g.Pix2Grad[5] |
| 220 |
offset = g.Spread.Clamp(math.Sqrt(gx*gx + gy*gy)) |
| 221 |
} |
| 222 |
if !(offset >= 0) { |
| 223 |
return color.RGBA64{} |
| 224 |
} |
| 225 |
|
| 226 |
if offset < g.Ranges[0].Offset0 { |
| 227 |
return g.First |
| 228 |
} |
| 229 |
for _, r := range g.Ranges { |
| 230 |
if r.Offset0 <= offset && offset <= r.Offset1 { |
| 231 |
t := (offset - r.Offset0) / r.Width |
| 232 |
s := 1 - t |
| 233 |
return color.RGBA64{ |
| 234 |
uint16(s*r.R0 + t*r.R1), |
| 235 |
uint16(s*r.G0 + t*r.G1), |
| 236 |
uint16(s*r.B0 + t*r.B1), |
| 237 |
uint16(s*r.A0 + t*r.A1), |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
return g.Last |
| 242 |
} |