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 }