summaryrefslogtreecommitdiff
path: root/internal/exporter/exporter.go
diff options
context:
space:
mode:
authorBenjamin Chausse <benjamin@chausse.xyz>2024-10-24 12:02:39 -0400
committerBenjamin Chausse <benjamin@chausse.xyz>2024-10-24 12:02:39 -0400
commitd6c5530b35743d1a2f153f942f508ca0768870af (patch)
tree1374ab8394ed9ad7a2720f5c158426a54b89b7b5 /internal/exporter/exporter.go
parent7277fa994838474ced8dc61a5f5002cb269685e8 (diff)
more than one slider WIPHEADmaster
Diffstat (limited to 'internal/exporter/exporter.go')
-rw-r--r--internal/exporter/exporter.go56
1 files changed, 56 insertions, 0 deletions
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
+}