-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcandy_example.go
More file actions
172 lines (149 loc) · 4.31 KB
/
candy_example.go
File metadata and controls
172 lines (149 loc) · 4.31 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
package pattern
import (
"image"
"image/color"
"image/png"
"math"
"os"
)
var CandyOutputFilename = "candy.png"
const CandyBaseLabel = "Candy"
// Candy Example (M&Ms / Smarties)
// Demonstrates using the Scatter pattern to draw overlapping, colored candy circles.
func ExampleNewCandy() {
// 1. Define colors for our candy.
colors := []color.RGBA{
{255, 0, 0, 255}, // Red
{0, 255, 0, 255}, // Green
{0, 0, 255, 255}, // Blue
{255, 255, 0, 255}, // Yellow
{255, 165, 0, 255}, // Orange
{139, 69, 19, 255}, // Brown
}
// 2. Create the Scatter pattern.
candy := NewScatter(
SetScatterFrequency(0.04), // Controls size/spacing relative to pixels
SetScatterDensity(0.9), // High density
SetScatterGenerator(func(u, v float64, hash uint64) (color.Color, float64) {
// Radius of the candy
radius := 14.0
// Distance from center
distSq := u*u + v*v
if distSq > radius*radius {
return color.Transparent, 0
}
dist := math.Sqrt(distSq)
// Pick a random color based on hash
colIdx := hash % uint64(len(colors))
baseCol := colors[colIdx]
// Simple shading: slightly darker at edges, highlight at top-left
// Spherical shading approx
// Normal vector (nx, ny, nz)
// z = sqrt(1 - x^2 - y^2)
nx := u / radius
ny := v / radius
nz := math.Sqrt(math.Max(0, 1.0 - nx*nx - ny*ny))
// Light source direction (top-left)
lx, ly, lz := -0.5, -0.5, 0.7
lLen := math.Sqrt(lx*lx + ly*ly + lz*lz)
lx, ly, lz = lx/lLen, ly/lLen, lz/lLen
// Diffuse
dot := nx*lx + ny*ly + nz*lz
diffuse := math.Max(0, dot)
// Specular (Glossy plastic look)
// Reflected light vector
// R = 2(N.L)N - L
rx := 2*dot*nx - lx
ry := 2*dot*ny - ly
rz := 2*dot*nz - lz
// View vector (straight up)
vx, vy, vz := 0.0, 0.0, 1.0
specDot := rx*vx + ry*vy + rz*vz
specular := math.Pow(math.Max(0, specDot), 20) // Shininess
// Apply lighting
r := float64(baseCol.R) * (0.2 + 0.8*diffuse) + 255*specular*0.6
g := float64(baseCol.G) * (0.2 + 0.8*diffuse) + 255*specular*0.6
b := float64(baseCol.B) * (0.2 + 0.8*diffuse) + 255*specular*0.6
// Clamp
r = math.Min(255, math.Max(0, r))
g = math.Min(255, math.Max(0, g))
b = math.Min(255, math.Max(0, b))
// Anti-aliasing at edge
alpha := 1.0
if dist > radius - 1.0 {
alpha = radius - dist
}
// Use hash for random Z-ordering
z := float64(hash) / 18446744073709551615.0
return color.RGBA{
R: uint8(r),
G: uint8(g),
B: uint8(b),
A: uint8(alpha * 255),
}, z
}),
)
f, err := os.Create(CandyOutputFilename)
if err != nil {
panic(err)
}
defer func() {
if e := f.Close(); e != nil {
panic(e)
}
}()
if err = png.Encode(f, candy); err != nil {
panic(err)
}
}
func GenerateCandy(b image.Rectangle) image.Image {
colors := []color.RGBA{
{255, 0, 0, 255},
{0, 255, 0, 255},
{0, 0, 255, 255},
{255, 255, 0, 255},
{255, 165, 0, 255},
{139, 69, 19, 255},
}
return NewScatter(
SetBounds(b),
SetScatterFrequency(0.04),
SetScatterDensity(0.9),
SetScatterGenerator(func(u, v float64, hash uint64) (color.Color, float64) {
radius := 14.0
distSq := u*u + v*v
if distSq > radius*radius {
return color.Transparent, 0
}
dist := math.Sqrt(distSq)
colIdx := hash % uint64(len(colors))
baseCol := colors[colIdx]
nx := u / radius
ny := v / radius
nz := math.Sqrt(math.Max(0, 1.0 - nx*nx - ny*ny))
lx, ly, lz := -0.5, -0.5, 0.7
lLen := math.Sqrt(lx*lx + ly*ly + lz*lz)
lx, ly, lz = lx/lLen, ly/lLen, lz/lLen
dot := nx*lx + ny*ly + nz*lz
diffuse := math.Max(0, dot)
rx := 2*dot*nx - lx
ry := 2*dot*ny - ly
rz := 2*dot*nz - lz
specDot := rx*0 + ry*0 + rz*1
specular := math.Pow(math.Max(0, specDot), 20)
r := float64(baseCol.R) * (0.2 + 0.8*diffuse) + 255*specular*0.6
g := float64(baseCol.G) * (0.2 + 0.8*diffuse) + 255*specular*0.6
b := float64(baseCol.B) * (0.2 + 0.8*diffuse) + 255*specular*0.6
alpha := 1.0
if dist > radius - 1.0 {
alpha = radius - dist
}
// Use hash for random Z-ordering
z := float64(hash) / 18446744073709551615.0
return color.RGBA{uint8(math.Min(255, math.Max(0, r))), uint8(math.Min(255, math.Max(0, g))), uint8(math.Min(255, math.Max(0, b))), uint8(alpha * 255)}, z
}),
)
}
func init() {
RegisterGenerator(CandyBaseLabel, GenerateCandy)
}