Debian Codegraph @

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

raw
1 package palgen
2
3 import (
4 "errors"
5 "image"
6 "image/color"
7 "image/color/palette"
8
9 "github.com/xyproto/burnpal"
10 )
11
12 // TODO: Create a custom Paletted type, based on image/Paletted, that can take > 256 colors
13
14 // ConvertCustom can convert an image from True Color to a <=256 color paletted image, given a custom palette.
15 func ConvertCustom(img image.Image, pal color.Palette) (image.Image, error) {
16 if len(pal) > 256 {
17 return nil, errors.New("can reduce to a maximum of 256 colors")
18 }
19 palImg := image.NewPaletted(img.Bounds(), pal)
20 // For each pixel, go through each color in the palette and pick out the closest one.
21 for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ {
22 for x := img.Bounds().Min.X; x < img.Bounds().Max.X; x++ {
23 sourceColor := img.At(x, y)
24 colorIndex := uint8(pal.Index(sourceColor))
25 palImg.SetColorIndex(x, y, colorIndex)
26 }
27 }
28 return palImg, nil
29 }
30
31 // Convert can convert an image from True Color to a 256 color paletted image.
32 // The palette is automatically extracted from the image.
33 func Convert(img image.Image) (image.Image, error) {
34 customPalette, err := Generate(img, 256)
35 if err != nil {
36 return nil, err
37 }
38 // This should never happen
39 if len(customPalette) > 256 {
40 return nil, errors.New("the generated palette has too many colors")
41 }
42 // Sort using the HCL colorspace
43 Sort(customPalette)
44 // Return a new Paletted image
45 return ConvertCustom(img, customPalette)
46 }
47
48 // Reduce can convert an image from True Color to a N color paletted image.
49 // The palette is automatically extracted from the image.
50 func Reduce(img image.Image, n int) (image.Image, error) {
51 customPalette, err := GenerateUpTo(img, n)
52 if err != nil {
53 return nil, err
54 }
55 // This should never happen
56 if len(customPalette) > n {
57 return nil, errors.New("the generated palette has too many colors")
58 }
59 // Sort using the HCL colorspace
60 Sort(customPalette)
61 // Return a new Paletted image
62 return ConvertCustom(img, customPalette)
63 }
64
65 // ConvertGeneral can convert an image from True Color to a 256 color paletted image, using a general palette.
66 func ConvertGeneral(img image.Image) (image.Image, error) {
67 return ConvertCustom(img, GeneralPalette())
68 }
69
70 // ConvertPlan9 can convert an image from True Color to a 256 color paletted image, using the Plan9 palette from the Go standard library.
71 func ConvertPlan9(img image.Image) (image.Image, error) {
72 return ConvertCustom(img, palette.Plan9)
73 }
74
75 // ConvertBasic can convert an image from True Color to a 16 color paletted image, using the 16 basic terminal colors
76 func ConvertBasic(img image.Image) (image.Image, error) {
77 return ConvertCustom(img, BasicPalette())
78 }
79
80 // ConvertBurn can convert an image from True Color to a 256 color paletted image, using the Burn palette from github.com/xyproto/burnpal.
81 func ConvertBurn(img image.Image) (image.Image, error) {
82 return ConvertCustom(img, burnpal.ColorPalette())
83 }