From de7c6e9ab8bb4ab3af4557800cf92dd5ebddd2f8 Mon Sep 17 00:00:00 2001 From: Benjamin Chausse Date: Mon, 25 Nov 2024 21:44:11 -0500 Subject: feat: Interactive input (#16) * Refactor copy keybinds into single help entry * Manually input color values at runtime * Showcase input instead of clipboard in demo * more vhs updates --- internal/userinput/userinput.go | 78 ----------------------------------------- 1 file changed, 78 deletions(-) delete mode 100644 internal/userinput/userinput.go (limited to 'internal/userinput/userinput.go') diff --git a/internal/userinput/userinput.go b/internal/userinput/userinput.go deleted file mode 100644 index 8f1ecdc..0000000 --- a/internal/userinput/userinput.go +++ /dev/null @@ -1,78 +0,0 @@ -package userinput - -import ( - "errors" - "fmt" - "strings" - - "github.com/ChausseBenjamin/termpicker/internal/colors" -) - -var ( - errUnknownColorFormat = errors.New("Unrecognized color format") - errHexParsing = errors.New("Failed to parse hex color") - errRGBParsing = errors.New("Failed to parse RGB color") - errHSLParsing = errors.New("Failed to parse HSL color") - errCMYKParsing = errors.New("Failed to parse CMYK color") -) - -func sanitize(s string) string { - s = strings.ReplaceAll(s, "\"", "") - s = strings.ReplaceAll(s, "%", "") - s = strings.ReplaceAll(s, "°", "") - s = strings.TrimSpace(s) - s = strings.ToLower(s) - return s -} - -func ParseColor(s string) (colors.ColorSpace, error) { - s = sanitize(s) - switch { - case strings.Contains(s, "#"): - return parseHex(s) - case strings.Contains(s, "rgb"): - return parseRGB(s) - case strings.Contains(s, "hsl"): - return parseHSL(s) - case strings.Contains(s, "cmyk"): - return parseCMYK(s) - default: - return nil, errUnknownColorFormat - } -} - -func parseRGB(s string) (colors.ColorSpace, error) { - var r, g, b int - _, err := fmt.Sscanf(s, "rgb(%d,%d,%d)", &r, &g, &b) - if err != nil { - return nil, errors.Join(errRGBParsing, err) - } - return colors.RGB{R: r, G: g, B: b}, nil -} - -func parseHex(s string) (colors.ColorSpace, error) { - var r, g, b int - _, err := fmt.Sscanf(s, "#%02x%02x%02x", &r, &g, &b) - if err != nil { - return nil, errors.Join(errHexParsing, err) - } - return colors.RGB{R: r, G: g, B: b}, nil -} - -func parseCMYK(s string) (colors.ColorSpace, error) { - var c, m, y, k int - _, err := fmt.Sscanf(s, "cmyk(%d,%d,%d,%d)", &c, &m, &y, &k) - if err != nil { - return nil, errors.Join(errCMYKParsing, err) - } - return colors.CMYK{C: c, M: m, Y: y, K: k}, nil -} - -func parseHSL(str string) (colors.ColorSpace, error) { - var h, s, l int - _, err := fmt.Sscanf(str, "hsl(%d,%d,%d)", &h, &s, &l) - if err != nil { - return nil, errors.Join(errHSLParsing, err) - } - return colors.HSL{H: h, S: s, L: l}, nil -} -- cgit v1.2.3