summaryrefslogtreecommitdiff
path: root/internal/userinput
diff options
context:
space:
mode:
authorBenjamin Chausse <benjamin@chausse.xyz>2024-11-25 16:45:33 -0500
committerGitHub <noreply@github.com>2024-11-25 16:45:33 -0500
commit821c198fe6700a9e9e9eb07ab6922e7bde7f2bfb (patch)
tree33f696ab2dfa433bc1513fd44b313419b74cc812 /internal/userinput
parentbad902e67b98a2630ff4396f846a223e3eed6a7a (diff)
Accept initial color using -c flag (#14)
Diffstat (limited to 'internal/userinput')
-rw-r--r--internal/userinput/userinput.go78
1 files changed, 78 insertions, 0 deletions
diff --git a/internal/userinput/userinput.go b/internal/userinput/userinput.go
new file mode 100644
index 0000000..8f1ecdc
--- /dev/null
+++ b/internal/userinput/userinput.go
@@ -0,0 +1,78 @@
+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
+}