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
+8.2k Golang: Prevent over writing file with md5 hash
+8.1k Swift : Convert (cast) Character to Integer?
+25.3k Golang : convert rune to integer value
+14.6k Golang : Normalize unicode strings for comparison purpose
+21.7k Golang : How to reverse slice or array elements order
+33.9k Golang : Create x509 certificate, private and public keys
+3.9k Detect if Google Analytics and Developer Media are loaded properly or not
+25.1k Golang : Convert uint value to string type
+20.3k Golang : Pipe output from one os.Exec(shell command) to another command
+21.6k Golang : Setting up/configure AWS credentials with official aws-sdk-go
+10.4k Golang : Get local time and equivalent time in different time zone