package main import ( "fmt" "strconv" "strings" ) /** * 颜色代码转换为RGB * input int * output int red, green, blue **/ func ColorToRGB(color int) (red, green, blue int) { red = color >> 16 green = (color & 0x00FF00) >> 8 blue = color & 0x0000FF return } func main() { color_str := "0xAD99C0" //颜色代码的值 color_str = strings.TrimPrefix(color_str, "0x") //过滤掉16进制前缀 color64, err := strconv.ParseInt(color_str, 16, 32) //字串到数据整型 if err != nil { panic(err) } color32 := int(color64) //类型强转 r, g, b := ColorToRGB(color32) rgb := fmt.Sprintf("RGB(%d,%d,%d)", r, g, b) println(rgb) } --------------------------------------------------------------------------------- output:: RGB(173,153,192)