blob: 3ecf6ef1e3e6e97a96115c8fc1916a290f6e0c34 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
}
|