Debian Codegraph @

shiny/iconvg/internal/gradient/gradient.go

raw symbols SCIP
1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Package gradient provides linear and radial gradient images.
6 package gradient
7
8 import (
9 "image"
10 "image/color"
11 "math"
12
13 "golang.org/x/image/math/f64"
14 )
15
16 // TODO: gamma correction / non-linear color interpolation?
17
18 // TODO: move this out of an internal directory, either under
19 // golang.org/x/image or under the standard library's image, so that
20 // golang.org/x/image/{draw,vector} and possibly image/draw can type switch on
21 // the gradient.Gradient type and provide fast path code.
22 //
23 // Doing so requires coming up with a stable API that we'd be happy to support
24 // in the long term. This would probably include an easier way to create
25 // linear, circular and elliptical gradients, without having to explicitly
26 // calculate the f64.Aff3 matrix.
27
28 // Shape is the gradient shape.
29 type Shape uint8
30
31 const (
32 ShapeLinear Shape = iota
33 ShapeRadial
34 )
35
36 // Spread is the gradient spread, or how to spread a gradient past its nominal
37 // bounds (from offset being 0.0 to offset being 1.0).
38 type Spread uint8
39
40 const (
41 // SpreadNone means that offsets outside of the [0, 1] range map to
42 // transparent black.
43 SpreadNone Spread = iota
44 // SpreadPad means that offsets below 0 and above 1 map to the colors that
45 // 0 and 1 would map to.
46 SpreadPad
47 // SpreadReflect means that the offset mapping is reflected start-to-end,
48 // end-to-start, start-to-end, etc.
49 SpreadReflect
50 // SpreadRepeat means that the offset mapping is repeated start-to-end,
51 // start-to-end, start-to-end, etc.
52 SpreadRepeat
53 )
54
55 // Clamp clamps x to the range [0, 1]. If x is outside that range, it is
56 // converted to a value in that range according to s's semantics. It returns -1
57 // if s is SpreadNone and x is outside the range [0, 1].
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 // Stop is an offset and color.
92 type Stop struct {
93 Offset float64
94 RGBA64 color.RGBA64
95 }
96
97 // Range is the range between two stops.
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 // MakeRange returns the range between two stops.
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 // AppendRanges appends to a the ranges defined by a's implicit final stop (if
130 // any exist) and stops.
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 // Gradient is a very large image.Image (the same size as an image.Uniform)
154 // whose colors form a gradient.
155 type Gradient struct {
156 Shape Shape
157 Spread Spread
158
159 // Pix2Grad transforms coordinates from pixel space (the arguments to the
160 // Image.At method) to gradient space. Gradient space is where a linear
161 // gradient ranges from x == 0 to x == 1, and a radial gradient has center
162 // (0, 0) and radius 1.
163 //
164 // This is an affine transform, so it can represent elliptical gradients in
165 // pixel space, including non-axis-aligned ellipses.
166 //
167 // For a linear gradient, the bottom row is ignored.
168 Pix2Grad f64.Aff3
169
170 Ranges []Range
171
172 // First and Last are the first and last stop's colors.
173 First, Last color.RGBA64
174 }
175
176 // Init initializes g to a gradient whose geometry is defined by shape and
177 // pix2Grad and whose colors are defined by spread and stops.
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 // ColorModel satisfies the image.Image interface.
193 func (g *Gradient) ColorModel() color.Model {
194 return color.RGBA64Model
195 }
196
197 // Bounds satisfies the image.Image interface.
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 // At satisfies the image.Image interface.
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 }