add new flags, add the ability to print to stdout
This commit is contained in:
4
go.mod
4
go.mod
@@ -1,7 +1,7 @@
|
|||||||
module github.com/amberisvibin/rgb2rgb8
|
module github.com/amberisvibin/rgb2rgb8
|
||||||
|
|
||||||
go 1.23.5
|
go 1.15 // calculated with mingo
|
||||||
|
|
||||||
require github.com/wayneashleyberry/truecolor v1.0.1
|
require github.com/wayneashleyberry/truecolor v1.0.1
|
||||||
|
|
||||||
require github.com/ogier/pflag v0.0.1
|
require github.com/spf13/pflag v1.0.7
|
||||||
|
4
go.sum
4
go.sum
@@ -1,4 +1,4 @@
|
|||||||
github.com/ogier/pflag v0.0.1 h1:RW6JSWSu/RkSatfcLtogGfFgpim5p7ARQ10ECk5O750=
|
github.com/spf13/pflag v1.0.7 h1:vN6T9TfwStFPFM5XzjsvmzZkLuaLX+HS+0SeFLRgU6M=
|
||||||
github.com/ogier/pflag v0.0.1/go.mod h1:zkFki7tvTa0tafRvTBIZTvzYyAu6kQhPZFnshFFPE+g=
|
github.com/spf13/pflag v1.0.7/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||||
github.com/wayneashleyberry/truecolor v1.0.1 h1:REnJBycjnvg0AFErbLx2GCmLLar8brlqm62kOKnRsGs=
|
github.com/wayneashleyberry/truecolor v1.0.1 h1:REnJBycjnvg0AFErbLx2GCmLLar8brlqm62kOKnRsGs=
|
||||||
github.com/wayneashleyberry/truecolor v1.0.1/go.mod h1:fyL3jRES70g94n+Eu+XLhXYvcseza55ph8zlkmUKW7Q=
|
github.com/wayneashleyberry/truecolor v1.0.1/go.mod h1:fyL3jRES70g94n+Eu+XLhXYvcseza55ph8zlkmUKW7Q=
|
||||||
|
79
rgb2rgb8.go
79
rgb2rgb8.go
@@ -4,17 +4,21 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/ogier/pflag"
|
"github.com/spf13/pflag"
|
||||||
"github.com/wayneashleyberry/truecolor/pkg/color"
|
"github.com/wayneashleyberry/truecolor/pkg/color"
|
||||||
"math"
|
"math"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var output *os.File
|
||||||
|
|
||||||
var (
|
var (
|
||||||
outputFile string
|
outputFile string
|
||||||
verbose bool
|
verbose bool
|
||||||
version bool
|
version bool
|
||||||
colors bool
|
colors bool
|
||||||
|
print bool
|
||||||
|
help bool
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -26,13 +30,12 @@ func init() {
|
|||||||
"output file",
|
"output file",
|
||||||
)
|
)
|
||||||
pflag.BoolVarP(
|
pflag.BoolVarP(
|
||||||
&verbose,
|
&print,
|
||||||
"verbose",
|
"print",
|
||||||
"v",
|
"p",
|
||||||
false,
|
false,
|
||||||
"verbose output",
|
"print to stdout instead of output file. -o will have no effect",
|
||||||
)
|
)
|
||||||
|
|
||||||
pflag.BoolVarP(
|
pflag.BoolVarP(
|
||||||
&colors,
|
&colors,
|
||||||
"colors",
|
"colors",
|
||||||
@@ -40,16 +43,30 @@ func init() {
|
|||||||
false,
|
false,
|
||||||
"print colors to terminal (needs 24 bit color support)",
|
"print colors to terminal (needs 24 bit color support)",
|
||||||
)
|
)
|
||||||
|
pflag.BoolVarP(
|
||||||
|
&verbose,
|
||||||
|
"verbose",
|
||||||
|
"v",
|
||||||
|
false,
|
||||||
|
"verbose output",
|
||||||
|
)
|
||||||
pflag.BoolVar(
|
pflag.BoolVar(
|
||||||
&version,
|
&version,
|
||||||
"version",
|
"version",
|
||||||
false,
|
false,
|
||||||
"version info",
|
"version info",
|
||||||
)
|
)
|
||||||
|
pflag.BoolVarP(
|
||||||
|
&help,
|
||||||
|
"help",
|
||||||
|
"h",
|
||||||
|
false,
|
||||||
|
"displays help",
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
//converts different size bytes to float64 to compare
|
// converts different size bytes to float64 to compare
|
||||||
//high and low and return the one thats closer
|
// high and low and return the one thats closer
|
||||||
func compareChannels(base, high, low byte) byte {
|
func compareChannels(base, high, low byte) byte {
|
||||||
absHigh := math.Abs(float64(high - base))
|
absHigh := math.Abs(float64(high - base))
|
||||||
absLow := math.Abs(float64(low - base))
|
absLow := math.Abs(float64(low - base))
|
||||||
@@ -59,24 +76,33 @@ func compareChannels(base, high, low byte) byte {
|
|||||||
return low
|
return low
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func printDefaults() {
|
||||||
|
fmt.Fprintf(os.Stderr, "Usage: %s [options] [-o outfile] infile\n", os.Args[0])
|
||||||
|
fmt.Fprintf(os.Stderr, "Options:\n")
|
||||||
|
pflag.PrintDefaults()
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
pflag.Usage = func() {
|
pflag.Usage = printDefaults
|
||||||
fmt.Fprintf(os.Stderr, "%s is a utility that converts RGB24 hex palette files to 8bit 3-3-2 RGB.\n", os.Args[0])
|
|
||||||
fmt.Fprintf(os.Stderr, "Usage:\n")
|
|
||||||
pflag.PrintDefaults()
|
|
||||||
}
|
|
||||||
|
|
||||||
pflag.Parse()
|
pflag.Parse()
|
||||||
|
|
||||||
if version {
|
if version {
|
||||||
fmt.Println("rgb2rgb8 v0.1")
|
fmt.Fprintf(os.Stderr, "%s is a utility that converts RGB24 hex palette files to 8bit 3-3-2 RGB.\n", os.Args[0])
|
||||||
|
fmt.Println("rgb2rgb8 v0.1.0")
|
||||||
fmt.Println("(c) 2025 Amber Zeller")
|
fmt.Println("(c) 2025 Amber Zeller")
|
||||||
fmt.Println("Distributed under the MIT license")
|
fmt.Println("Distributed under the MIT license")
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if help {
|
||||||
|
printDefaults()
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
if len(pflag.Args()) == 0 {
|
if len(pflag.Args()) == 0 {
|
||||||
fmt.Println("Error: No file specified.")
|
fmt.Println("Error: No file specified.")
|
||||||
|
printDefaults()
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,14 +123,14 @@ func main() {
|
|||||||
defer input.Close()
|
defer input.Close()
|
||||||
|
|
||||||
//create output file
|
//create output file
|
||||||
output, err := os.OpenFile(outputFile, os.O_WRONLY|os.O_CREATE, 0644)
|
if !print {
|
||||||
if err != nil {
|
output, err = os.Create(outputFile)
|
||||||
fmt.Println("Error: Cannot create output file.", err)
|
if err != nil {
|
||||||
os.Exit(3)
|
fmt.Println("Error: Cannot create output file.", err)
|
||||||
|
os.Exit(3)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
defer output.Close()
|
|
||||||
|
|
||||||
//scan through input
|
//scan through input
|
||||||
scanner := bufio.NewScanner(input)
|
scanner := bufio.NewScanner(input)
|
||||||
var data string
|
var data string
|
||||||
@@ -177,9 +203,14 @@ func main() {
|
|||||||
fmt.Println(scanner.Err())
|
fmt.Println(scanner.Err())
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = output.WriteString(data)
|
if print {
|
||||||
if err != nil {
|
fmt.Fprintf(os.Stderr, "%s", data)
|
||||||
fmt.Println("Error: Failed to write output file.", err)
|
} else {
|
||||||
os.Exit(4)
|
_, err = output.WriteString(data)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error: Failed to write output file.", err)
|
||||||
|
os.Exit(4)
|
||||||
|
}
|
||||||
|
output.Close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user