Making a go-hexcolor package

2024/08/08 | 1 minute read | Updated at 2024/08/08

Making a go-hexcolor package

Just wrote a Go package called “go-hexcolor”, to convert HEX color codes into color.NRGBA. It supports three-digit, six-digit, and eight-digit HEX color codes. Whether it starts with # or not doesn’t matter.

// s is []byte
// only RGB parts here
// rToB() for converting from rune to byte
// by subtracting 'a' 'A' or '0'

if len(s) == 6 || len(s) == 8 {
  clr.R = (rToB(s[0]) << 4) + rToB(s[1])
  clr.G = (rToB(s[2]) << 4) + rToB(s[3])
  clr.B = (rToB(s[4]) << 4) + rToB(s[5])

  return
}

if len(s) == 3 || len(s) == 4 {
  clr.R = rToB(s[0]) * 17
  clr.G = rToB(s[1]) * 17
  clr.B = rToB(s[2]) * 17

  return
}

bin16/go-hexcolor

© 2026 Banana Engine Failure

🌱 Powered by Hugo with theme Dream.