prominentcolor

package
v0.0.0-...-026264f Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2022 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package prominentcolor finds the K most dominant/prominent colors in an image

Index

Constants

View Source
const (
	// ArgumentDefault default settings
	ArgumentDefault int = 0
	// ArgumentSeedRandom randomly pick initial values (instead of K-means++)
	ArgumentSeedRandom = 1 << iota
	// ArgumentAverageMean take the mean value when determining the centroid color (instead of median)
	ArgumentAverageMean
	// ArgumentNoCropping do not crop background that is considered "white"
	ArgumentNoCropping
	// ArgumentLAB (experimental, it seems to be buggy in some cases): uses LAB instead of RGB when measuring distance
	ArgumentLAB
	// ArgumentDebugImage saves a tmp file in /tmp/ where the area that has been cut away by the mask is marked pink
	// useful when figuring out what values to pick for the masks
	ArgumentDebugImage
)
View Source
const (
	// DefaultK is the k used as default
	DefaultK = 3
	// DefaultSize is the default size images are re-sized to
	DefaultSize = 80
)

Variables

View Source
var (
	// MaskWhite "constant" for white mask (for ease of re-use for other mask arrays)
	MaskWhite = ColorBackgroundMask{R: true, G: true, B: true, Treshold: uint32(0xc000)}
	// MaskBlack "constant" for black mask (for ease of re-use for other mask arrays)
	MaskBlack = ColorBackgroundMask{R: false, G: false, B: false, Treshold: uint32(0x5000)}
	// MaskGreen "constant" for green mask (for ease of re-use for other mask arrays)
	MaskGreen = ColorBackgroundMask{R: false, G: true, B: false, PercDiff: 0.9}
)
View Source
var ErrNoPixelsFound = fmt.Errorf("Failed, no non-alpha pixels found (either fully transparent image, or the ColorBackgroundMask removed all pixels)")

ErrNoPixelsFound is returned when no non-alpha pixels are found in the provided image

Functions

func IsBitSet

func IsBitSet(bitset int, lookingfor int) bool

IsBitSet check if "lookingfor" is set in "bitset"

func ProcessImg

func ProcessImg(arguments int, bgmasks []ColorBackgroundMask, img image.Image) draw.Image

ProcessImg process the image and mark unwanted pixels transparent. It checks the corners, if not all of them match the mask, we conclude it's not a clipart/solid background and do nothing

func ProcessImgOutline

func ProcessImgOutline(bgmask ColorBackgroundMask, imgDraw *draw.Image)

ProcessImgOutline follow the outline of the image and mark all "white" pixels as transparent

Types

type ColorBackgroundMask

type ColorBackgroundMask struct {
	// Setting them all to true or all to false; Treshold is used, otherwise PercDiff
	R, G, B bool

	// Treshold is the lower limit to check against for each r,g,b value, when all R,G,B that has true set should be above to be ignored (upper if all set to false)
	Treshold uint32

	// PercDiff if any of R,G,B is true (but not all), any of the other colors divided by the color value that is true, must be below PercDiff
	PercDiff float32
}

ColorBackgroundMask defines which color channels to look for color to ignore

func GetDefaultMasks

func GetDefaultMasks() []ColorBackgroundMask

GetDefaultMasks returns the masks that are used for the default settings

type ColorItem

type ColorItem struct {
	Color ColorRGB
	Cnt   int
}

ColorItem contains color and have many occurrences of this color found

func Kmeans

func Kmeans(orgimg image.Image) (centroids []ColorItem, err error)

Kmeans uses the default: k=3, Kmeans++, Median, crop center, resize to 80 pixels, mask out white/black/green backgrounds It returns an array of ColorItem which are three centroids, sorted according to dominance (most frequent first).

func KmeansWithAll

func KmeansWithAll(k int, orgimg image.Image, arguments int, imageReSize uint, bgmasks []ColorBackgroundMask) ([]ColorItem, error)

KmeansWithAll takes additional arguments to define k, arguments (see constants Argument*), size to resize and masks to use

func KmeansWithArgs

func KmeansWithArgs(arguments int, orgimg image.Image) (centroids []ColorItem, err error)

KmeansWithArgs takes arguments which consists of the bits, see constants Argument*

func (*ColorItem) AsString

func (c *ColorItem) AsString() string

AsString gives back the color in hex as 6 character string

type ColorRGB

type ColorRGB struct {
	R, G, B uint32
}

ColorRGB contains the color values

Jump to

Keyboard shortcuts

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