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
+12.8k Golang : List objects in AWS S3 bucket
+7.5k Golang : Example of how to detect which type of script a word belongs to
+5.2k Gogland : Datasource explorer
+36.1k Golang : Validate IP address
+6.8k Golang : How to call function inside template with template.FuncMap
+5.8k Golang : Grab news article text and use NLP to get each paragraph's sentences
+13.8k Golang : Google Drive API upload and rename example
+21.9k Golang : Print leading(padding) zero or spaces in fmt.Printf?
+6.3k Golang : Calculate diameter, circumference, area, sphere surface and volume
+23.7k Golang : Upload to S3 with official aws-sdk-go package
+18.3k Golang : Send email with attachment
+9.8k Golang : Identifying Golang HTTP client request