MCPcopy Create free account
hub / github.com/B33pBeeps/redthread / ParseHexColor

Function ParseHexColor

internal/app/theme.go:30–46  ·  view source on GitHub ↗

ParseHexColor parses "#rrggbb" or "rrggbb" (case-insensitive) into an RGB. Returns ok=false for any malformed input.

(s string)

Source from the content-addressed store, hash-verified

28// ParseHexColor parses "#rrggbb" or "rrggbb" (case-insensitive) into an RGB.
29// Returns ok=false for any malformed input.
30func ParseHexColor(s string) (RGB, bool) {
31 s = strings.TrimSpace(s)
32 s = strings.TrimPrefix(s, "#")
33 if len(s) != 6 {
34 return RGB{}, false
35 }
36 var vals [3]uint8
37 for i := 0; i < 3; i++ {
38 hi, ok1 := hexNibble(s[i*2])
39 lo, ok2 := hexNibble(s[i*2+1])
40 if !ok1 || !ok2 {
41 return RGB{}, false
42 }
43 vals[i] = hi<<4 | lo
44 }
45 return RGB{R: vals[0], G: vals[1], B: vals[2]}, true
46}
47
48func hexNibble(b byte) (uint8, bool) {
49 switch {

Callers 3

drawBackgroundMenuAtFunction · 0.85
TestParseHexColorFunction · 0.85
BgColorMethod · 0.85

Calls 1

hexNibbleFunction · 0.85

Tested by 1

TestParseHexColorFunction · 0.68