cleaning up, early http server draft
40
http/http.go
Normal file
@@ -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)
|
||||
}
|
||||
}
|
||||
4
http/index.html
Normal file
@@ -0,0 +1,4 @@
|
||||
<form action="/upload" method="POST" enctype="multipart/form-data">
|
||||
<input type="file" name="image" accept="image/*" required>
|
||||
<button type="submit">Upload Image</button>
|
||||
</form>
|
||||
|
Before Width: | Height: | Size: 1.8 MiB After Width: | Height: | Size: 1.8 MiB |
|
Before Width: | Height: | Size: 1.1 MiB After Width: | Height: | Size: 1.1 MiB |
|
Before Width: | Height: | Size: 977 KiB After Width: | Height: | Size: 977 KiB |
|
Before Width: | Height: | Size: 1.1 MiB After Width: | Height: | Size: 1.1 MiB |
|
Before Width: | Height: | Size: 1.1 MiB After Width: | Height: | Size: 1.1 MiB |
BIN
util/.hex_test.go.swp
Normal file
32
util/hex.go
Normal file
@@ -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))
|
||||
}
|
||||
47
util/hex_test.go
Normal file
@@ -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)))
|
||||
}
|
||||