Skip to content

An indexed color palette implementation in Go on top of a k-d tree for fast color lookups. Also rank a palette against an image to identify prominent colors.

License

Notifications You must be signed in to change notification settings

philoj/tree-palette

Repository files navigation

treepalette

GoDoc Build Status codecov Go Report Card License

An indexed color palette implementation in Go on top of a k-d tree for fast color lookups. Also rank a palette against an image to identify prominent colors.

  • Transparent(RGBA) and opaque(RGB) palettes
  • Direct image conversion
  • Image pixel counting and color ranking, for prominent color analysis

kd-tree implementation adapted from: kyroy/kdtree

Usage

go get github.com/philoj/tree-palette
import "github.com/philoj/tree-palette"

Create a color model for color lookups.

m := NewPalettedColorModel([]color.Color{
        // list of colors in the palette
        }, false // ignore alpha values
    )
equivalentColor := m.Convert(someColor)

Color ranking and image color analysis

Start by implementing treepalette.PaletteColor and treepalette.Color interfaces:

// Color express A color as A n-dimensional point in the RGBA space for usage in the kd-tree search algorithm.
type Color interface {

	// Dimensions returns the total number of dimensions(3 for RGB, 4 for RGBA).
	Dimensions() int

	// Dimension returns the value of the i-th dimension, say R,G,B and/or A.
	Dimension(i int) uint32
}

// PaletteColor is A Color inside an indexed color palette.
type PaletteColor interface {
	Color

	// Index returns palette index of the color
	Index() int
}

Or use included implementations treepalette.ColorRGBA and treepalette.IndexedColorRGBA respectively:

// Unknown color
c := treepalette.NewOpaqueColor(121,201,10)

// Palette colors
p1 := treepalette.NewOpaquePaletteColor(255, 130, 1, 2, "DARK ORANGE") // R,G,B, unique-id, name
p2 := treepalette.NewOpaquePaletteColor(1, 128, 181, 11, "PACIFIC BLUE")

// Create palette
palette := treepalette.NewPalette([]treepalette.PaletteColor{p1,p2}, false)

// Equivalent color
equivalent := palette.Convert(c)

// Convert an image.Image
palettedImage := palette.ApplyPalette(img)

// Rank the palette against all the pixels in an image.Image
colors, colorCount := palette.Rank(img)
fmt.Printf("Most frequent color is %s. It appears %d times.", colors[0], colorCount[colors[0].Index()])

About

An indexed color palette implementation in Go on top of a k-d tree for fast color lookups. Also rank a palette against an image to identify prominent colors.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages