Created
November 8, 2020 07:09
-
-
Save romanitalian/18590101f432655e6d0acab54d6df3a1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 colors | |
import ( | |
"image/color" | |
"strconv" | |
) | |
type rgb struct { | |
red uint8 | |
green uint8 | |
blue uint8 | |
} | |
func ToRGBA(h string) (color.RGBA, error) { | |
rgb, err := hex2RGB(h) | |
if err != nil { | |
return color.RGBA{}, err | |
} | |
return color.RGBA{R: rgb.red, G: rgb.green, B: rgb.blue, A: 255}, nil | |
} | |
func hex2RGB(hex string) (rgb, error) { | |
values, err := strconv.ParseUint(hex, 16, 32) | |
if err != nil { | |
return rgb{}, err | |
} | |
return rgb{ | |
red: uint8(values >> 16), | |
green: uint8((values >> 8) & 0xFF), | |
blue: uint8(values & 0xFF), | |
}, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment