Skip to content

Instantly share code, notes, and snippets.

@kkirsche
Created July 21, 2016 01:07
Show Gist options
  • Select an option

  • Save kkirsche/a2243352c46e93459bb643c397f56d1c to your computer and use it in GitHub Desktop.

Select an option

Save kkirsche/a2243352c46e93459bb643c397f56d1c to your computer and use it in GitHub Desktop.
Integer to RGBA in Golang — Similar to C/C++
package main
import "fmt"
func main() {
red := int(0xFF0000FF)
green := int(0x00FF00FF)
blue := int(0x0000FFFF)
redHex, greenHex, blueHex, alphaHex := calcColor(red)
fmt.Println("Red:")
fmt.Printf("R: %d, G: %d, B: %d, A: %d\n", redHex, greenHex, blueHex, alphaHex)
fmt.Println()
redHex, greenHex, blueHex, alphaHex = calcColor(green)
fmt.Println("Green:")
fmt.Printf("R: %d, G: %d, B: %d, A: %d\n", redHex, greenHex, blueHex, alphaHex)
fmt.Println()
redHex, greenHex, blueHex, alphaHex = calcColor(blue)
fmt.Println("Blue:")
fmt.Printf("R: %d, G: %d, B: %d, A: %d\n", redHex, greenHex, blueHex, alphaHex)
}
func calcColor(color int) (red, green, blue, alpha int) {
alpha = color & 0xFF
blue = (color >> 8) & 0xFF
green = (color >> 16) & 0xFF
red = (color >> 24) & 0xFF
return red, green, blue, alpha
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment