Golang : Create new color from command line parameters
Problem:
You want to create a new color base on given RGBA parameters/arguments from command line. How to do that?
Solution:
Create a new color variable with image/color
package. Set the RGBA values with {}
, such as:
color1 := color.RGBA{uint8(red1), uint8(green1), uint8(blue1), uint8(alpha1)}
Here you go!
createnewcolor.go
package main
import (
"fmt"
"image/color"
"os"
"strconv"
)
func main() {
if len(os.Args) != 9 {
fmt.Printf("Usage : %s <red1> <green1> <blue1> <alpha1> <red2> <green2> <blue2> <alpha1> \n", os.Args[0])
os.Exit(0)
}
//sanity checks
red1int, _ := strconv.Atoi(os.Args[1])
red1 := uint8(red1int)
green1int, _ := strconv.Atoi(os.Args[2])
green1 := uint8(green1int)
blue1int, _ := strconv.Atoi(os.Args[3])
blue1 := uint8(blue1int)
alpha1int, _ := strconv.Atoi(os.Args[4])
alpha1 := uint8(alpha1int)
red2int, _ := strconv.Atoi(os.Args[5])
red2 := uint8(red2int)
green2int, _ := strconv.Atoi(os.Args[6])
green2 := uint8(green2int)
blue2int, _ := strconv.Atoi(os.Args[7])
blue2 := uint8(blue2int)
alpha2int, _ := strconv.Atoi(os.Args[8])
alpha2 := uint8(alpha2int)
fmt.Println("Red 1 : ", red1)
fmt.Println("Green 1 : ", green1)
fmt.Println("Blue 1 : ", blue1)
fmt.Println("Alpha 1 : ", alpha1)
fmt.Println("Red 2 : ", red2)
fmt.Println("Green 2 : ", green2)
fmt.Println("Blue 2 : ", blue2)
fmt.Println("Alpha 2 : ", alpha2)
// create new colors from given arguments/command line parameters
color1 := color.RGBA{uint8(red1), uint8(green1), uint8(blue1), uint8(alpha1)}
color2 := color.RGBA{uint8(red2), uint8(green2), uint8(blue2), uint8(alpha2)}
fmt.Println("Color 1 = ", color1) // white
fmt.Println("Color 2 = ", color2) // black
}
Output:
./createnewcolor 0 0 0 0 0 0 255 0
Red 1 : 0
Green 1 : 0
Blue 1 : 0
Alpha 1 : 0
Red 2 : 0
Green 2 : 0
Blue 2 : 255
Alpha 2 : 0
Color 1 = {0 0 0 0}
Color 2 = {0 0 255 0}
Happy coloring!
Reference:
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+22.4k Golang : Convert Unix timestamp to UTC timestamp
+9.1k Golang : How to find out similarity between two strings with Jaro-Winkler Distance?
+12.8k Python : Convert IPv6 address to decimal and back to IPv6
+9.8k Golang : Sort and reverse sort a slice of integers
+12.5k Golang : Pass database connection to function called from another package and HTTP Handler
+25.9k Mac/Linux and Golang : Fix bind: address already in use error
+16.7k Golang : Get the IPv4 and IPv6 addresses for a specific network interface
+13.3k Facebook PHP getUser() returns 0
+8.6k Golang : How to join strings?
+18.3k Golang : How to get hour, minute, second from time?
+23.2k Golang : Get ASCII code from a key press(cross-platform) example
+9.1k Golang : How to control fmt or log print format?