summaryrefslogtreecommitdiff
path: root/internal/userinput/userinput.go
diff options
context:
space:
mode:
authorBenjamin Chausse <benjamin@chausse.xyz>2024-11-25 21:44:11 -0500
committerGitHub <noreply@github.com>2024-11-25 21:44:11 -0500
commitde7c6e9ab8bb4ab3af4557800cf92dd5ebddd2f8 (patch)
treee889022472615bf6ab06df37469eb3bffba8f4a3 /internal/userinput/userinput.go
parent44456ebb15be6f2d9e981fd4093c48fc736d219a (diff)
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
Diffstat (limited to 'internal/userinput/userinput.go')
-rw-r--r--internal/userinput/userinput.go78
1 files changed, 0 insertions, 78 deletions
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
-}