diff --git a/http/http.go b/http/http.go new file mode 100644 index 0000000..8361f67 --- /dev/null +++ b/http/http.go @@ -0,0 +1,40 @@ +package main + +import ( + "net/http" + "log" +) + +type imageTransform struct { + image.Image + TransformFunc func(image.Image) image.Image +} + +func (t imageTransform) ServeHTTP(w http.ResponseWriter, r *http.Request) { + +} + +func indexHTML(w http.ResponseWriter, r *http.Request) { + f, err := os.Open("index.html") + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + log.Fatalln("error opening file:", err) + return + } + _, err := io.Copy(w, f) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + log.Println("copy error:", err) + return + } +} + +func main() { + http.HandleFunc("/", indexHTML) + http.Handle("POST /upload", imageTransform) + + err := http.ListenAndServe(":8080", nil) + if err != nil { + log.Panicln(err) + } +} diff --git a/http/index.html b/http/index.html new file mode 100644 index 0000000..1dd1ca9 --- /dev/null +++ b/http/index.html @@ -0,0 +1,4 @@ +
+ + +
diff --git a/cmd/downscale/downscale.go b/pkg/downscale/downscale.go similarity index 100% rename from cmd/downscale/downscale.go rename to pkg/downscale/downscale.go diff --git a/cmd/downscale/main.go b/pkg/downscale/main.go similarity index 100% rename from cmd/downscale/main.go rename to pkg/downscale/main.go diff --git a/cmd/mesh/8.clock.png b/pkg/mesh/8.clock.png similarity index 100% rename from cmd/mesh/8.clock.png rename to pkg/mesh/8.clock.png diff --git a/cmd/mesh/9p.go b/pkg/mesh/9p.go similarity index 100% rename from cmd/mesh/9p.go rename to pkg/mesh/9p.go diff --git a/cmd/mesh/mesh.go b/pkg/mesh/mesh.go similarity index 100% rename from cmd/mesh/mesh.go rename to pkg/mesh/mesh.go diff --git a/cmd/mkpalette/palette.go b/pkg/mkpalette/palette.go similarity index 100% rename from cmd/mkpalette/palette.go rename to pkg/mkpalette/palette.go diff --git a/cmd/negate/8.absolutecinema.png b/pkg/negate/8.absolutecinema.png similarity index 100% rename from cmd/negate/8.absolutecinema.png rename to pkg/negate/8.absolutecinema.png diff --git a/cmd/negate/minus19blue b/pkg/negate/minus19blue similarity index 100% rename from cmd/negate/minus19blue rename to pkg/negate/minus19blue diff --git a/cmd/negate/minus19blue.go b/pkg/negate/minus19blue.go similarity index 100% rename from cmd/negate/minus19blue.go rename to pkg/negate/minus19blue.go diff --git a/cmd/negate/mixed.go b/pkg/negate/mixed.go similarity index 100% rename from cmd/negate/mixed.go rename to pkg/negate/mixed.go diff --git a/cmd/negate/negate b/pkg/negate/negate similarity index 100% rename from cmd/negate/negate rename to pkg/negate/negate diff --git a/cmd/negate/negate.go b/pkg/negate/negate.go similarity index 100% rename from cmd/negate/negate.go rename to pkg/negate/negate.go diff --git a/cmd/negate/negated5.png b/pkg/negate/negated5.png similarity index 100% rename from cmd/negate/negated5.png rename to pkg/negate/negated5.png diff --git a/cmd/negate/negated8.png b/pkg/negate/negated8.png similarity index 100% rename from cmd/negate/negated8.png rename to pkg/negate/negated8.png diff --git a/cmd/negate/reshaded,negated8.png b/pkg/negate/reshaded,negated8.png similarity index 100% rename from cmd/negate/reshaded,negated8.png rename to pkg/negate/reshaded,negated8.png diff --git a/cmd/showpalette/.show.go.swp b/pkg/showpalette/.show.go.swp similarity index 100% rename from cmd/showpalette/.show.go.swp rename to pkg/showpalette/.show.go.swp diff --git a/cmd/showpalette/show.go b/pkg/showpalette/show.go similarity index 100% rename from cmd/showpalette/show.go rename to pkg/showpalette/show.go diff --git a/util/.hex_test.go.swp b/util/.hex_test.go.swp new file mode 100644 index 0000000..9f578a3 Binary files /dev/null and b/util/.hex_test.go.swp differ diff --git a/util/hex.go b/util/hex.go new file mode 100644 index 0000000..65aa065 --- /dev/null +++ b/util/hex.go @@ -0,0 +1,32 @@ +package imageutils + +import ( + "fmt" + "image/color" + "errors" +) + +var ParseHexColorErr = errors.New("invalid length, must be 7 or 4") + +func ParseHexColor(s string) (c color.RGBA, err error) { + c.A = 0xff + switch len(s) { + case 7: + _, err = fmt.Sscanf(s, "#%02x%02x%02x", &c.R, &c.G, &c.B) + case 4: + _, err = fmt.Sscanf(s, "#%1x%1x%1x", &c.R, &c.G, &c.B) + // Double the hex digits: + c.R *= 17 + c.G *= 17 + c.B *= 17 + default: + err = ParseHexColorErr + + } + return +} + +func ColorToHex(c color.Color) string { + r, g, b, _ := color.NRGBAModel.Convert(c).RGBA() + return fmt.Sprintf("#%02x%02x%02x", byte(r), byte(g), byte(b)) +} diff --git a/util/hex_test.go b/util/hex_test.go new file mode 100644 index 0000000..6a0123c --- /dev/null +++ b/util/hex_test.go @@ -0,0 +1,47 @@ +package imageutils + +import ( + "fmt" + "image/png" + "os" + "path/filepath" + "testing" +) + +// Returns path of first occured file +// with png extension in user's home directory. +func firstPNG(root string) (filename string, + err error) { + var fn filepath.WalkFunc = func(path string, + info os.FileInfo, + fileErr error) error { + if fileErr != nil { + return fileErr + } + if !info.IsDir() && + filepath.Ext(path) == ".png" { + filename = path + return nil + } + return nil + } + err = filepath.Walk(root, fn) + return +} + +func TestColorToHex(t *testing.T) { + root := os.Getenv("HOME") + path, err := firstPNG(root) + if err != nil { + t.Fatal(err) + } + f, err := os.Open(path) + if err != nil { + t.Fatal(err) + } + img, err := png.Decode(f) + if err != nil { + t.Fatal(err) + } + fmt.Println(ColorToHex(img.At(0, 0))) +} diff --git a/util.go b/util/util.go similarity index 100% rename from util.go rename to util/util.go