Last active
October 5, 2017 09:19
-
-
Save chrisneal/835ee5a571a8f90b356e9c5cc7e8da85 to your computer and use it in GitHub Desktop.
Create RGB values from a full length HEX color code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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