package util

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)))
}
