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
+9.4k Golang : Populate slice with sequential integers example
+9k Golang : Apply Histogram Equalization to color images
+17.8k Golang : Convert IPv4 address to decimal number(base 10) or integer
+12.6k Golang : Convert int(year) to time.Time type
+12k Golang : 2 dimensional array example
+18.4k Golang : Display list of time zones with GMT
+9.2k Golang : How to extract video or image files from html source code
+4.4k JavaScript : Rounding number to decimal formats to display currency
+15.7k Golang : Get file permission
+7.2k Golang : Create zip/ePub file without compression(use Store algorithm)
+9.8k Golang : Find and replace data in all files recursively
+15.8k Golang : How to check if input from os.Args is integer?