diff options
Diffstat (limited to 'internal/exporter')
-rw-r--r-- | internal/exporter/exporter.go | 56 |
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 +} |