Created
July 21, 2016 01:07
-
-
Save kkirsche/a2243352c46e93459bb643c397f56d1c to your computer and use it in GitHub Desktop.
Integer to RGBA in Golang — Similar to C/C++
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 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