palgen

package module
v1.5.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 3, 2024 License: BSD-3-Clause Imports: 12 Imported by: 3

README

palgen Build GoDoc Go Report Card

Render, extract and save palettes

Given an image, create a palette of N colors, or convert True Color images to indexed ones.

Palettes can also be rendered as images.

Included utilities
  • png256, for converting a True Color PNG image to an indexed PNG image, with a custom palette of 256 colors.
  • png2png, for extracting a palette from a True Color PNG image and write the palette as an indexed 256 color PNG image.
  • png2gpl, for extracting a palette from a True Color PNG image and write the palette as a GIMP palette file (.gpl).
  • png2act, for extracting a palette from a True Color PNG image and write the palette as a Photoshop palette file (.act).
Algorithm

A palette is generated by first dividing the colors in the image into N groups, sorted by intensity. For each group, the median value is used. When the number of colors in a group is even, the average of the two center colors are used as the median, but the two center colors are saved for later and added to the generated palette if there are duplicate colors. The generated palette may be shorter than N if there are not enough colors in the given image. As far as I know, no other software uses this algorithm. It works fine, and is relatively fast, but there even better algorithms out there if you are looking for the optimal palette and want to adjust for which colors the human eye are most sensitive for.

As far as I am aware, this is a unique algorithm that has not been thought of or implemented before (create an issue if not), so I'll hereby name it "Rodseth's Median Intensity Algorithm".

Palette extraction
source image extracted 256 color palette
png png
png png

The palette can be extracted and saved as a PNG image, using png2png, or as a GIMP palette, using png2gpl.

Palettes can be sorted by hue, luminance and chroma, using the HCL colorspace and the go-colorful package, with the included palgen.Sort function. The above palettes are sorted with this method.

Features and limitations
  • Can generate palettes of N colors relatively quickly.
  • The palette is generated by first grouping colors into N intensity levels and then use the median color of each group.
  • The generated palette is not 100% optimal, but it's usable.
  • Can export any given color.Palette to a GIMP .gpl palette file.
  • Can export any given color.Palette to a Photoshop .act palette file.
  • Can convert True Color image.Image images to indexed image.Paletted images.
Example use
// Read a PNG file
imageData, err := os.Open("input.png")
if err != nil {
    return err
}

// Decode the PNG image
img, err := png.Decode(imageData)
if err != nil {
    return err
}

// Generate a palette with 256 colors
pal, err := palgen.Generate(img, 256)
if err != nil {
    return err
}

// Output a .gpl palette file with the name "Untitled"
err = palgen.Save(pal, "output.gpl", "Untitled")
if err != nil {
    return err
}
Image comparison

The palettes are generated by palgen

8 color palette 16 color palette 32 color palette 64 color palette 128 color palette 256 color palette original
png png png png png png png
png png png png png png png
Render existing palettes to images

palgen can also be used for rendering palettes to images. Here are the two built-in palettes in the Go standard library, with and without the colors sorted:

Plan9
Sorted Original
png png
WebSafe
Sorted Original
png png

And one extra:

Burn
Sorted Original
png png
General info

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BasicPalette16 = [16][3]byte{
	{0x0, 0x0, 0x0},
	{191, 0x0, 0x0},
	{0x0, 191, 0x0},
	{191, 191, 0x0},
	{0x0, 0x0, 191},
	{191, 0x0, 191},
	{0x0, 191, 191},
	{191, 191, 191},
	{0x40, 0x40, 0x40},
	{0xff, 0x40, 0x40},
	{0x40, 0xff, 0x40},
	{0xff, 0xff, 0x40},
	{96, 96, 0xff},
	{0xff, 0x40, 0xff},
	{0x40, 0xff, 0xff},
	{0xff, 0xff, 0xff},
}

BasicPalette16 is a basic 16-color palette

View Source
var GeneralPalette256 = [256][3]byte{}/* 256 elements not displayed */

GeneralPalette256 is a an OK standard palette

Functions

func ACT added in v1.0.1

func ACT(pal color.Palette) []byte

ACT converts a given palette to the Photoshop ACT Palette Format (.act) There is no header, just either 768 or 772 bytes of color data. 256 * 3 = 768. The four extra bytes can be 16-bit color count + 16 bit transparent color index.

func BasicPalette added in v1.1.0

func BasicPalette() (pal color.Palette)

BasicPalette can return a basic 16 color palette

func Convert added in v1.0.1

func Convert(img image.Image) (image.Image, error)

Convert can convert an image from True Color to a 256 color paletted image. The palette is automatically extracted from the image.

func ConvertBasic added in v1.1.0

func ConvertBasic(img image.Image) (image.Image, error)

ConvertBasic can convert an image from True Color to a 16 color paletted image, using the 16 basic terminal colors

func ConvertBurn added in v1.3.0

func ConvertBurn(img image.Image) (image.Image, error)

ConvertBurn can convert an image from True Color to a 256 color paletted image, using the Burn palette from github.com/xyproto/burnpal.

func ConvertCustom added in v1.0.1

func ConvertCustom(img image.Image, pal color.Palette) (image.Image, error)

ConvertCustom can convert an image from True Color to a <=256 color paletted image, given a custom palette.

func ConvertGeneral added in v1.0.1

func ConvertGeneral(img image.Image) (image.Image, error)

ConvertGeneral can convert an image from True Color to a 256 color paletted image, using a general palette.

func ConvertPlan9 added in v1.0.1

func ConvertPlan9(img image.Image) (image.Image, error)

ConvertPlan9 can convert an image from True Color to a 256 color paletted image, using the Plan9 palette from the Go standard library.

func GPL

func GPL(pal color.Palette, paletteName string) (string, error)

GPL converts a given palette to the GIMP Palette Format (.gpl) The given name will be used as the palette name in the header

func GeneralPalette added in v1.0.1

func GeneralPalette() (pal color.Palette)

GeneralPalette can return a pretty general 256 color palette

func Generate

func Generate(img image.Image, N int) (color.Palette, error)

Generate can generate a palette with N colors, given an image

func GenerateUpTo added in v1.4.0

func GenerateUpTo(img image.Image, N int) (color.Palette, error)

GenerateUpTo can generate a palette with up to N colors, given an image

func Median added in v1.0.1

func Median(colors []color.Color) (color.Color, error)

Median finds not the average but the median color

func Median3 added in v1.0.1

func Median3(colors []color.Color) (color.Color, color.Color, color.Color, error)

Median3 finds not the average but the median color. Returns three colors if the number of colors is even (average, first and second color in the center).

func Reduce added in v1.3.0

func Reduce(img image.Image, n int) (image.Image, error)

Reduce can convert an image from True Color to a N color paletted image. The palette is automatically extracted from the image.

func Render added in v1.0.1

func Render(pal color.Palette) image.Image

Render a given palette as an image, with blocks of 16x16 pixels and 32 colors per row of blocks.

func RenderWithConfig added in v1.0.1

func RenderWithConfig(pal color.Palette, blockSize, colorsPerRow int) image.Image

RenderWithConfig can render a given palette as an image that shows each color as a square, lined up in rows. blockSize is the size of the color block per color, in pixels (16 is default) colorsPerRow is the number of color blocks per row (32 is default)

func SaveACT added in v1.0.1

func SaveACT(pal color.Palette, filename string) error

SaveACT can save a palette to file in the Photoship ACT Palette Format (.act)

func SaveGPL added in v1.0.1

func SaveGPL(pal color.Palette, filename, paletteName string) error

SaveGPL can save a palette to file in the GIMP Palette Format (.gpl) The given name will be used as the palette name in the header

func Sort added in v1.0.1

func Sort(pal color.Palette)

Sort the palette by luminance, hue and then chroma

Types

type HCLSortablePalette added in v1.0.1

type HCLSortablePalette []color.Color

HCLSortablePalette is a slice of color.Color that can be sorted with sort.Sort, by h, c and l values

func (HCLSortablePalette) Len added in v1.0.1

func (a HCLSortablePalette) Len() int

func (HCLSortablePalette) Less added in v1.0.1

func (a HCLSortablePalette) Less(i, j int) bool

func (HCLSortablePalette) Swap added in v1.0.1

func (a HCLSortablePalette) Swap(i, j int)

type SortablePalette

type SortablePalette []color.Color

SortablePalette is a slice of color.Color that can be sorted with sort.Sort, by euclidian distance for R, G and B

func (SortablePalette) Len

func (a SortablePalette) Len() int

func (SortablePalette) Less

func (a SortablePalette) Less(i, j int) bool

func (SortablePalette) Swap

func (a SortablePalette) Swap(i, j int)

Directories

Path Synopsis
cmd
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL