Debian Codegraph @

vendor/github.com/xyproto/palgen/gpl_palette.go

raw
1 package palgen
2
3 import (
4 "errors"
5 "fmt"
6 "image/color"
7 "os"
8 "strings"
9 )
10
11 const softwareID = "github.com/xyproto/palgen"
12
13 // GPL converts a given palette to the GIMP Palette Format (.gpl)
14 // The given name will be used as the palette name in the header
15 func GPL(pal color.Palette, paletteName string) (string, error) {
16 var sb strings.Builder
17 // Prepare a header
18 sb.WriteString("GIMP Palette\nName: ")
19 sb.WriteString(paletteName)
20 sb.WriteString("\nColumns: 4\n# ")
21 sb.WriteString(softwareID)
22 sb.WriteString("\n")
23 // Output the colors
24 for i, c := range pal {
25 cn, ok := c.(color.RGBA)
26 if !ok {
27 return "", errors.New("colors in the given palette must be color.RGBA compatible")
28 }
29 sb.WriteString(fmt.Sprintf("%3d %3d %3d\t%d\n", cn.R, cn.G, cn.B, i))
30 }
31 // Return the generated string
32 return sb.String(), nil
33 }
34
35 // SaveGPL can save a palette to file in the GIMP Palette Format (.gpl)
36 // The given name will be used as the palette name in the header
37 func SaveGPL(pal color.Palette, filename, paletteName string) error {
38 GPLString, err := GPL(pal, paletteName)
39 if err != nil {
40 return err
41 }
42 return os.WriteFile(filename, []byte(GPLString), 0644)
43 }