package image import ( "fmt" "gopkg.in/gographics/imagick.v3/imagick" "icloudapp.cn/tools/util" "strconv" "strings" ) type Color struct { Red uint32 Green uint32 Blue uint32 Alpha uint32 R uint8 G uint8 B uint8 A uint8 } func NewColor(color string) *Color { return &Color{} } // PaddingColorTo8 将color补充到8位,最后两位位Alpha func PaddingColorTo8(color string) string { hexColor := strings.Replace(color, "#", "", 1) strRune := []rune(hexColor) var strBuild strings.Builder if len(hexColor) == 3 { for i := 0; i < 3; i++ { strBuild.WriteString(string(strRune[i])) strBuild.WriteString(string(strRune[i])) } strBuild.WriteString("00") hexColor = strBuild.String() } if len(hexColor) > 8 { hexColor = util.CutString(hexColor, 0, 8) } strBuild.WriteString(hexColor) if len(hexColor) < 8 { padding := 8 - len(hexColor) for i := 0; i < padding; i++ { strBuild.WriteString("0") } } return strBuild.String() } // Alpha rgb alpha 0 透明 -> 1 不透明 func Alpha(color string) (string, Color, float64) { // 0 透明 -> 1 不透明 opacity := 1.0 hexColor := PaddingColorTo8(color) color = util.CutString(hexColor, 0, 6) alpha := util.CutString(hexColor, 6, 8) hexAlpha, _ := strconv.ParseInt(alpha, 16, 10) opacity = float64(int((float64(hexAlpha)/255)*100)) / 100 color = fmt.Sprintf("#%s", color) var rgb Color rgbRune := []rune(hexColor) r, _ := strconv.ParseInt(string(rgbRune[0:2]), 16, 10) rgb.Red = uint32(r) R, _ := strconv.ParseUint(string(rgbRune[0:2]), 16, 8) rgb.R = uint8(R) g, _ := strconv.ParseInt(string(rgbRune[2:4]), 16, 10) rgb.Green = uint32(g) G, _ := strconv.ParseUint(string(rgbRune[2:4]), 16, 8) rgb.G = uint8(G) b, _ := strconv.ParseInt(string(rgbRune[4:6]), 16, 10) rgb.Blue = uint32(b) B, _ := strconv.ParseUint(string(rgbRune[4:6]), 16, 8) rgb.G = uint8(B) a, _ := strconv.ParseInt(string(rgbRune[6:8]), 16, 10) rgb.Alpha = uint32(a / 255) A, _ := strconv.ParseUint(string(rgbRune[6:8]), 16, 8) rgb.A = uint8(A) return color, rgb, opacity } // Pixel PixelWand func Pixel(color string) *imagick.PixelWand { destColor, _, _ := Alpha(color) g := imagick.NewPixelWand() g.SetColor(destColor) return g }