From d6c5530b35743d1a2f153f942f508ca0768870af Mon Sep 17 00:00:00 2001 From: Benjamin Chausse Date: Thu, 24 Oct 2024 12:02:39 -0400 Subject: more than one slider WIP --- internal/exporter/exporter.go | 56 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 internal/exporter/exporter.go (limited to 'internal/exporter') diff --git a/internal/exporter/exporter.go b/internal/exporter/exporter.go new file mode 100644 index 0000000..3ecf6ef --- /dev/null +++ b/internal/exporter/exporter.go @@ -0,0 +1,56 @@ +package exporter + +import ( + "fmt" + "strings" +) + +const ( + rgbFormat = iota + hexFormat + hslFormat + hwbFormat +) + +func Export(hex string, format int) string { + switch format { + case hexFormat: + return hex + case rgbFormat: + return toRgb(hex) + case hslFormat: + return "Not Implemented yet..." + case hwbFormat: + return "Not Implemented yet..." + default: + return "Unknown export format requested" + } +} + +func toRgb(hex string) string { + hex = strings.TrimPrefix(hex, "#") + r, g, b := HexToI(hex[:2]), HexToI(hex[2:4]), HexToI(hex[4:6]) + return fmt.Sprintf("rgb(%d, %d, %d)", r, g, b) +} + +func HexToI(s string) int { + lenS := len(s) - 1 + chars := "0123456789ABCDEF" + s = strings.ToUpper(s) + var ttl int = 0 + + for n := lenS; n >= 0; n-- { + done := false + for i := 0; i < len(chars); i++ { + if s[n] == chars[i] { + ttl += i * (n << 4) + done = true + break + } + } + if !done { // Still not done after checking all chars + return -1 + } + } + return ttl +} -- cgit v1.2.3