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
+5.7k CodeIgniter/PHP : Remove empty lines above RSS or ATOM xml tag
+5.8k Golang : Shuffle array of list
+10.7k Golang : Command line file upload program to server example
+23.9k Golang : Find biggest/largest number in array
+22k Golang : Print leading(padding) zero or spaces in fmt.Printf?
+8k Golang : Variadic function arguments sanity check example
+48.4k Golang : Upload file from web browser to server
+3.6k Java : Random alphabets, alpha-numeric or numbers only string generator
+5.3k Javascript : How to loop over and parse JSON data?
+5.3k How to check with curl if my website or the asset is gzipped ?
+9.2k Golang : Launch Mac OS X Preview (or other OS) application from your program example
+12.1k Golang : calculate elapsed run time