Skip to content

Instantly share code, notes, and snippets.

@romanitalian
Created November 8, 2020 07:09
Show Gist options
  • Save romanitalian/18590101f432655e6d0acab54d6df3a1 to your computer and use it in GitHub Desktop.
Save romanitalian/18590101f432655e6d0acab54d6df3a1 to your computer and use it in GitHub Desktop.
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