This repository has been archived on 2023-07-25. You can view files and clone it, but cannot push or open issues or pull requests.
humanshader/hsv.go

76 lines
1.8 KiB
Go

package main
import (
"bufio"
"fmt"
hsv "git.snrd.eu/sunred/humanshader/validator"
"os"
"strconv"
)
func main() {
args := os.Args[1:]
// TODO: Better argument parsing
if len(args) == 1 || len(args) == 3 || len(args) == 5 {
fmt.Printf("Writing PPM image to %s.\n", args[0])
if len(args) == 3 || len(args) == 5 {
width, _ := strconv.Atoi(args[1])
height, _ := strconv.Atoi(args[2])
if len(args) == 5 {
startx, _ := strconv.Atoi(args[3])
starty, _ := strconv.Atoi(args[4])
writePPM(args[0], width, height, startx, starty)
} else {
writePPM(args[0], width, height, 0, 0)
}
} else {
writePPM(args[0], 71, 40, 0, 0)
}
fmt.Println("Done")
} else if len(args) == 2 {
pxX, _ := strconv.Atoi(args[0])
pxY, _ := strconv.Atoi(args[1])
printPixel(pxX, pxY)
} else {
input := bufio.NewScanner(os.Stdin)
fmt.Print("Pixel x coordinate: ")
input.Scan()
pxX, _ := strconv.Atoi(input.Text())
fmt.Print("Pixel y coordinate: ")
input.Scan()
pxY, _ := strconv.Atoi(input.Text())
fmt.Println()
printPixel(pxX, pxY)
}
}
func printPixel(x int, y int) {
val := hsv.NewValidator(true)
val.Calculate(x, y)
r, g, b := val.GetColor()
fmt.Printf("\nR: %d\nG: %d\nB: %d\nHEX: #%02X%02X%02X\n", r, g, b, r, g, b)
}
func writePPM(path string, width int, height int, startx int, starty int) {
f, err := os.Create(path)
if err != nil {
panic(err)
}
defer f.Close()
bw := bufio.NewWriter(f)
bw.WriteString("P3\n")
bw.WriteString(fmt.Sprintf("%d %d\n", width, height))
bw.WriteString("255\n")
for y := startx; y < startx+height; y++ {
for x := starty; x < starty+width; x++ {
v := hsv.NewValidator(false)
v.Calculate(x, y)
r, g, b := v.GetColor()
bw.WriteString(fmt.Sprintf("%d %d %d ", r, g, b))
}
bw.WriteString("\n")
}
bw.Flush()
}