/
utility.go
221 lines (185 loc) · 4.78 KB
/
utility.go
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
package util
import (
"image"
"image/color"
_ "image/gif"
_ "image/jpeg"
"image/png"
"math"
"os"
)
// Color constants
var COLOR = struct {
RED color.RGBA
BLUE color.RGBA
BLACK color.RGBA
WHITE color.RGBA
YELLOW color.RGBA
ORANGE color.RGBA
GREEN color.RGBA
PURPLE color.RGBA
PINK color.RGBA
TRANSPARENT color.RGBA
}{
RED: color.RGBA{255, 0, 0, 255},
BLUE: color.RGBA{0, 0, 255, 255},
BLACK: color.RGBA{0, 0, 0, 255},
WHITE: color.RGBA{255, 255, 255, 255},
YELLOW: color.RGBA{255, 255, 0, 255},
ORANGE: color.RGBA{255, 165, 0, 255},
GREEN: color.RGBA{0, 255, 0, 255},
PURPLE: color.RGBA{128, 0, 128, 255},
PINK: color.RGBA{255, 192, 203, 255},
TRANSPARENT: color.RGBA{0, 0, 0, 0},
}
// Converts RGB values to HSL.
func RGBToHSL(r, g, b uint8) (float64, float64, float64) {
rF, gF, bF := float64(r)/255.0, float64(g)/255.0, float64(b)/255.0
max := math.Max(math.Max(rF, gF), bF)
min := math.Min(math.Min(rF, gF), bF)
delta := max - min
var h, s, l float64
// Calculate hue
if delta == 0 {
h = 0
} else if max == rF {
h = 60 * math.Mod(((gF-bF)/delta)+6, 6)
if h < 0 {
h += 360
}
} else if max == gF {
h = 60 * (((bF - rF) / delta) + 2)
} else {
h = 60 * (((rF - gF) / delta) + 4)
}
// Calculate lightness
l = (max + min) / 2
// Calculate saturation
if delta == 0 {
s = 0
} else {
s = delta / (1 - math.Abs(2*l-1))
}
return h, s, l
}
// Finds the average of the RGB values of a color
func AverageRGBA(color color.Color) uint8 {
r, g, b, _ := color.RGBA()
return uint8((uint32(r) + uint32(g) + uint32(b)) / 3)
}
// Converts an image to grayscale
func ConvertToGray(input image.Image) *image.Gray {
bounds := input.Bounds()
gray := image.NewGray(bounds)
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
oldColor := input.At(x, y)
grayColor := color.GrayModel.Convert(oldColor)
gray.Set(x, y, grayColor)
}
}
return gray
}
// Loads an image from file
func LoadImage(filename string) (image.Image, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
return nil, err
}
return img, nil
}
func createImage(x, y int, color color.RGBA) image.Image {
img := image.NewRGBA(image.Rect(0, 0, x, y))
for i := 0; i < x; i++ {
for j := 0; j < y; j++ {
img.Set(i, j, color)
}
}
return img
}
// Saves an image to file
func SaveImage(filename string, img image.Image) error {
file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()
err = png.Encode(file, img)
if err != nil {
return err
}
return nil
}
// Represents a group of subimages that can be combined into a single image
// The subimages are arranged in a grid of size x by y.
type Subimages struct {
Images []image.Image
x int
y int
}
// X returns the x value.
func (s *Subimages) X() int {
return s.x
}
// Y returns the y value.
func (s *Subimages) Y() int {
return s.y
}
// Splits the image into a grid of smaller images.
// Returns a Subimages struct containing the smaller images and the arrangement of the grid. (x by y)
func SplitImage(img image.Image, x, y int) Subimages {
bounds := img.Bounds()
width, height := bounds.Max.X, bounds.Max.Y
images := make([]image.Image, 0, x*y)
// Calculate the width and height of each sub-image
subWidth := width / x
subHeight := height / y
// Create a new image for each sub-image
for i := 0; i < x; i++ {
for j := 0; j < y; j++ {
subImage := img.(interface {
SubImage(r image.Rectangle) image.Image
}).SubImage(image.Rect(i*subWidth, j*subHeight, (i+1)*subWidth, (j+1)*subHeight))
images = append(images, subImage)
}
}
return Subimages{images, x, y}
}
// Takes in a subimages struct and combines them into a single image
func CombineImages(imgs *Subimages) image.Image {
bounds := imgs.Images[0].Bounds()
width, height := bounds.Max.X, bounds.Max.Y
// Create a new image for the result
combinedImage := image.NewRGBA(image.Rect(0, 0, width*imgs.X(), height*imgs.Y()))
// Combine the images
for i, img := range imgs.Images {
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
combinedImage.Set(i*width+x, i/width*height+y, img.At(x, y))
}
}
}
return combinedImage
}
func AverageColor(img *image.Image) color.RGBA {
bounds := (*img).Bounds()
width, height := bounds.Max.X, bounds.Max.Y
var r, g, b uint32
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
rTemp, gTemp, bTemp, _ := (*img).At(x, y).RGBA()
r += rTemp
g += gTemp
b += bTemp
}
}
r /= uint32(width * height)
g /= uint32(width * height)
b /= uint32(width * height)
return color.RGBA{uint8(r), uint8(g), uint8(b), 255}
}