Skip to content

Instantly share code, notes, and snippets.

@chrisneal
Last active October 5, 2017 09:19
Show Gist options
  • Save chrisneal/835ee5a571a8f90b356e9c5cc7e8da85 to your computer and use it in GitHub Desktop.
Save chrisneal/835ee5a571a8f90b356e9c5cc7e8da85 to your computer and use it in GitHub Desktop.
Create RGB values from a full length HEX color code
package main
import (
"fmt"
"strconv"
)
type Hex string
type RGB struct {
Red uint8
Green uint8
Blue uint8
}
func Hex2RGB(hex Hex) (RGB, err) {
rgb, err := strconv.ParseUint(string(hex), 16, 32)
if err != nil {
return nil, err
}
rgb = RGB{
Red: uint8(rgb >> 16),
Green: uint8((rgb >> 8) & 0xFF),
Blue: uint8(rgb & 0xFF),
}
return rgb, nil
}
func main() {
// Usage:
rgb, err := Hex2RGB(Hex("FFAAFF"))
if err != nil {
panic("Couldn't convert hex to rgb")
}
fmt.Println(rgb.Red, rgb.Green, rgb.Blue)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment