-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
83 lines (64 loc) · 1.6 KB
/
main.go
File metadata and controls
83 lines (64 loc) · 1.6 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
package main
import (
"fmt"
"image"
"image/color"
"image/draw"
_ "image/jpeg"
_ "image/png"
"log"
"math"
"os"
)
type Cssify struct {
Pixels []*Pixel
Width int
Height int
}
type Pixel struct {
Color string
}
func main() {
if len(os.Args) <= 1 {
printCommandHelp()
}
prettyPrint("---- CSSify v0.1 ----", true)
path := os.Args[1]
loadedImage := fileOpen(path)
img, _, err := image.Decode(loadedImage)
if err != nil {
log.Fatal(err)
}
dst := image.NewRGBA(img.Bounds())
draw.Draw(dst, img.Bounds(), img, img.Bounds().Min, draw.Src)
img = nil
cssImage := Cssify{
Pixels: getCssColors(dst),
Width: dst.Bounds().Dx(),
Height: dst.Bounds().Dy(),
}
htmlGenerator(Cssify{Pixels: cssImage.Pixels, Width: cssImage.Width, Height: cssImage.Height})
}
func getCssColors(img image.Image) []*Pixel {
bounds := img.Bounds()
size := bounds.Dx() * bounds.Dy()
prettyPrint(fmt.Sprintf("CSS-ifying Image: %vx%v", bounds.Dx(), bounds.Dy()), true)
var hexSlice = make([]*Pixel, 0, size)
bar := progressBar("Extracting color:", size)
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
rgba := img.At(x, y).(color.RGBA)
hexSlice = append(hexSlice, rgbaToCssColor(&rgba, false))
bar.Increment()
}
}
bar.Finish()
return hexSlice
}
func rgbaToCssColor(rgba *color.RGBA, isRgb bool) *Pixel {
if isRgb {
alpha := math.Floor((float64(rgba.A)/255)*10) / 10
return &Pixel{Color: fmt.Sprintf("rgba(%v, %v, %v, %v)", rgba.R, rgba.G, rgba.B, alpha)}
}
return &Pixel{Color: fmt.Sprintf("#%02x%02x%02x%02x", rgba.R, rgba.G, rgba.B, rgba.A)}
}