Last active
July 1, 2020 03:15
-
-
Save raylee/8a505d57ca577af2b0eddec9aa48015b to your computer and use it in GitHub Desktop.
Convert a text file with R G B values [0-255] separated by whitespace or commas, one triplet per line, to Photoshop's ACT format.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"os" | |
"strconv" | |
"strings" | |
) | |
func usage() { | |
fmt.Println("Convert a text file with up to 256 RGB values to Photoshop's ACT format") | |
fmt.Println() | |
fmt.Println("Usage: <infile> <outfile>") | |
fmt.Println(" <infile> is a text file with R G B values [0-255] separated by") | |
fmt.Println(" whitespace or commas, one per line") | |
fmt.Println("<outfile> will be written in Photoshop's .ACT color format") | |
} | |
func exists(filename string) bool { | |
_, err := os.Stat(filename) | |
return err == nil | |
} | |
func toByte(s string) uint8 { | |
if v, err := strconv.ParseUint(s, 10, 8); err == nil { | |
return uint8(v) | |
} else { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
return 0 | |
} | |
func main() { | |
if len(os.Args) != 3 { | |
usage() | |
os.Exit(1) | |
} | |
infile, outfile := os.Args[1], os.Args[2] | |
if exists(outfile) { | |
fmt.Println("Cowardly refusing to overwrite " + outfile) | |
os.Exit(1) | |
} | |
transparencyColorIndex := 0 | |
act := make([]byte, 772) | |
colormap, err := ioutil.ReadFile(infile) | |
if err != nil { | |
fmt.Println("Could not open " + infile + " for reading") | |
os.Exit(1) | |
} | |
text := strings.ReplaceAll(string(colormap), "\r\n", "\n") | |
text := strings.ReplaceAll(string(colormap), ",", " ") | |
lines := strings.Split(text, "\n") | |
if len(lines) > 256 { | |
lines = lines[0:256] | |
} | |
colorCount := len(lines) | |
for i := range lines { | |
components := strings.Fields(lines[i]) | |
if len(components) != 3 { | |
fmt.Printf("File malformed. Expected 3, found %d color components on line: %s\n", | |
len(components), lines[i]) | |
os.Exit(1) | |
} | |
act[i*3+0] = toByte(components[0]) // R | |
act[i*3+1] = toByte(components[1]) // G | |
act[i*3+2] = toByte(components[2]) // B | |
} | |
act[768] = uint8(colorCount >> 8) | |
act[769] = uint8(colorCount & 0xff) | |
act[770] = uint8(transparencyColorIndex >> 8) | |
act[771] = uint8(transparencyColorIndex & 0xff) | |
err = ioutil.WriteFile(outfile, act, 0644) | |
if err != nil { | |
fmt.Println("Could not write output file: ", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment