vibrant

package module
v0.0.0-...-0680b8c Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2016 License: MIT Imports: 10 Imported by: 0

README

Vibrant

GoDoc Build Status Report Card

Vibrant is a customizable Go library for extracting colors from images. It is largely based on Android's Palette class.

Example usages can be found in the documentation and in the cli directory`.

Author

Rob Cherry

Special Thanks

Documentation

Index

Examples

Constants

View Source
const (
	DefaultFilterMinLightness = 0.05
	DefaultFilterMaxLightness = 0.95
)

Default values for the default filter.

View Source
const (
	DefaultResizeArea        = uint64(320 * 320)
	DefaultMaximumColorCount = uint32(64)
)

Defaults used by the default PaletteBuilder.

View Source
const (
	MinNormalLightness    = 0.3
	TargetNormalLightness = 0.5
	MaxNormalLightness    = 0.7

	MinLightLightness    = 0.55
	TargetLightLightness = 0.74

	TargetDarkLightness = 0.26
	MaxDarkLightness    = 0.45

	TargetVibrantSaturation = 1
	MinVibrantSaturation    = 0.35

	TargetMutedSaturation = 0.3
	MaxMutedSaturation    = 0.4

	// Weights used by the default scoring function.
	DefaultSaturationWeight = 0.375
	DefaultLightnessWeight  = 0.575
	DefaultPopulationWeight = 0.05
)

Constants used by the default targets.

Variables

DefaultFilter removes colors close to white and black.

View Source
var (
	DefaultScaler = draw.ApproxBiLinear
)

Defaults used by the default PaletteBuilder.

View Source
var HSLModel = color.ModelFunc(func(c color.Color) color.Color {
	if _, ok := c.(HSL); ok {
		return c
	}
	nrgba := color.NRGBAModel.Convert(c).(color.NRGBA)
	h, s, l := rgbToHSL(nrgba.R, nrgba.G, nrgba.B)
	return HSL{h, s, l, nrgba.A}
})

HSLModel is the color.Model for the HSL type.

View Source
var QuantizedColorGenerator = func(r, g, b uint8) QuantizedColor {
	quantizedRed := quantizeColorValue(r)
	quantizedGreen := quantizeColorValue(g)
	quantizedBlue := quantizeColorValue(b)
	return QuantizedColor((quantizedRed << (quantizeWordWidth + quantizeWordWidth)) | (quantizedGreen << quantizeWordWidth) | quantizedBlue)
}

QuantizedColorGenerator creates a new QuantizedColor from a given red, green, and blue value.

View Source
var QuantizedColorModel = color.ModelFunc(func(c color.Color) color.Color {
	if _, ok := c.(QuantizedColor); ok {
		return c
	}
	nrgba := color.NRGBAModel.Convert(c).(color.NRGBA)
	return QuantizedColorGenerator(nrgba.R, nrgba.G, nrgba.B)
})

QuantizedColorModel is the color.Model for the QuantizedColor type.

View Source
var RGBAIntModel = color.ModelFunc(func(c color.Color) color.Color {
	if _, ok := c.(RGBAInt); ok {
		return c
	}
	nrgba := color.NRGBAModel.Convert(c).(color.NRGBA)
	return RGBAInt((uint32(nrgba.A) << 24) | (uint32(nrgba.R) << 16) | (uint32(nrgba.G) << 8) | uint32(nrgba.B))
})

RGBAIntModel is the color.Model for the RGBAInt type.

Functions

func ScaleImageDown

func ScaleImageDown(src image.Image, resizeArea uint64, scaler draw.Scaler) image.Image

ScaleImageDown will scale the image down as needed. This is relatively slow.

Types

type ColorCutQuantizer

type ColorCutQuantizer struct {
	// contains filtered or unexported fields
}

ColorCutQuantizer is a color quantizer based on the Median-cut algorithm, but optimized for picking out distinct colors rather than representation colors.

The color space is represented as a 3-dimensional cube with each dimension being an RGB component. The cube is then repeatedly divided until we have reduced the color space to the requested number of colors. An average color is then generated from each cube.

What makes this different to median-cut is that median-cut divided cubes so that all of the cubes have roughly the same population, where this quantizer divides boxes based on their color volume. This means that the color space is divided into distinct colors, rather than representative colors.

func NewColorCutQuantizer

func NewColorCutQuantizer() *ColorCutQuantizer

NewColorCutQuantizer creates a default ColorCutQuantizer.

func NewColorCutQuantizerWithFilters

func NewColorCutQuantizerWithFilters(filters []Filter) *ColorCutQuantizer

NewColorCutQuantizerWithFilters creates a ColorCutQuantizer with custom filters.

func (*ColorCutQuantizer) Quantize

Quantize populates a color palette for the image.

func (*ColorCutQuantizer) Swatches

func (q *ColorCutQuantizer) Swatches(colorCount uint32, m image.Image) []*Swatch

Swatches returns a slice of swaches generated from the image.

type DefaultScorer

type DefaultScorer struct {
	// contains filtered or unexported fields
}

DefaultScorer is the default scorer implementation used to score a Target.

func (DefaultScorer) Score

func (s DefaultScorer) Score(swatch *Swatch) float64

Score returns the score for a Swatch.

type Filter

type Filter interface {
	// contains filtered or unexported methods
}

A Filter provides a mechanism for exercising fine-grained control over which colors are valid within a resulting Palette.

func NewLightnessFilter

func NewLightnessFilter(blackMaxLightness float64, whiteMinimumLightness float64) Filter

NewLightnessFilter creates a Filter that removes colors based on a lightness.

type HSL

type HSL struct {
	H, S, L float64
	A       uint8
}

HSL represents the HSL value for an RGBA color.

func (HSL) RGBA

func (c HSL) RGBA() (uint32, uint32, uint32, uint32)

RGBA implements the color.Color interface.

func (HSL) String

func (c HSL) String() string

type Palette

type Palette struct {
	// contains filtered or unexported fields
}

Palette extracts prominent colors from an image.

A number of colors with different profiles are extracted from the image:

Vibrant
Vibrant Dark
Vibrant Light
Muted
Muted Dark
Muted Light

These can be retrieved via the appropriate getter method.

Instances are created with a PaletteBuilder which supports several options to tweak the generated Palette.

func (*Palette) ColorForTarget

func (p *Palette) ColorForTarget(target *Target, defaultColor uint32) uint32

Returns the selected color for the given target from the palette as an RGB packed int.

func (*Palette) DarkMutedColor

func (p *Palette) DarkMutedColor(defaultColor uint32) uint32

Returns a muted and dark color from the palette as an RGB packed int.

func (*Palette) DarkMutedSwatch

func (p *Palette) DarkMutedSwatch() *Swatch

Returns a muted and dark swatch from the palette. Might be nil.

func (*Palette) DarkVibrantColor

func (p *Palette) DarkVibrantColor(defaultColor uint32) uint32

Returns a dark and vibrant color from the palette as an RGB packed int.

func (*Palette) DarkVibrantSwatch

func (p *Palette) DarkVibrantSwatch() *Swatch

Returns a dark and vibrant swatch from the palette. Might be nil.

func (*Palette) LightMutedColor

func (p *Palette) LightMutedColor(defaultColor uint32) uint32

Returns a muted and light color from the palette as an RGB packed int.

func (*Palette) LightMutedSwatch

func (p *Palette) LightMutedSwatch() *Swatch

Returns a muted and light swatch from the palette. Might be nil.

func (*Palette) LightVibrantColor

func (p *Palette) LightVibrantColor(defaultColor uint32) uint32

Returns a light and vibrant color from the palette as an RGB packed int.

func (*Palette) LightVibrantSwatch

func (p *Palette) LightVibrantSwatch() *Swatch

Returns a light and vibrant swatch from the palette. Might be nil.

func (*Palette) MutedColor

func (p *Palette) MutedColor(defaultColor uint32) uint32

Returns a muted color from the palette as an RGB packed int.

func (*Palette) MutedSwatch

func (p *Palette) MutedSwatch() *Swatch

Returns a muted swatch from the palette. Might be nil.

func (*Palette) SwatchForTarget

func (p *Palette) SwatchForTarget(target *Target) *Swatch

Returns the selected swatch for the given target from the palette, or nil if one could not be found.

func (*Palette) Swatches

func (p *Palette) Swatches() []*Swatch

Returns all of the swatches which make up the palette.

func (*Palette) Targets

func (p *Palette) Targets() []*Target

Returns the targets used to generate this palette.

func (*Palette) VibrantColor

func (p *Palette) VibrantColor(defaultColor uint32) uint32

Returns the most vibrant color in the palette as an RGB packed int.

func (*Palette) VibrantSwatch

func (p *Palette) VibrantSwatch() *Swatch

Returns the most vibrant swatch in the palette. Might be nil.

type PaletteBuilder

type PaletteBuilder struct {
	// contains filtered or unexported fields
}

PaletteBuilder is used for generating Palette instances.

Example
file, _ := os.Open("test_files/1.jpg")
decodedImage, _, _ := image.Decode(file)
palette := NewPaletteBuilder(decodedImage).Generate()
// Iterate over the swatches in the palette...
for _, swatch := range palette.Swatches() {
	fmt.Printf("Swatch has color %v and population %d\n", swatch.RGBAInt(), swatch.Population())
}
for _, target := range palette.Targets() {
	_ = palette.SwatchForTarget(target)
	// Do something with the swatch for a given target...
}
Output:

Example (MaximumColorCount)
file, _ := os.Open("test_files/1.jpg")
decodedImage, _, _ := image.Decode(file)
// Use a custom color count.
palette := NewPaletteBuilder(decodedImage).MaximumColorCount(32).Generate()
// Iterate over the swatches in the palette...
for _, swatch := range palette.Swatches() {
	fmt.Printf("Swatch has color %v and population %d\n", swatch.RGBAInt(), swatch.Population())
}
for _, target := range palette.Targets() {
	_ = palette.SwatchForTarget(target)
	// Do something with the swatch for a given target...
}
Output:

Example (ResizeImageArea)
file, _ := os.Open("test_files/1.jpg")
decodedImage, _, _ := image.Decode(file)
// Use a custom resize image area and scaler.
palette := NewPaletteBuilder(decodedImage).ResizeImageArea(160 * 160).Scaler(draw.CatmullRom).Generate()
// Iterate over the swatches in the palette...
for _, swatch := range palette.Swatches() {
	fmt.Printf("Swatch has color %v and population %d\n", swatch.RGBAInt(), swatch.Population())
}
for _, target := range palette.Targets() {
	_ = palette.SwatchForTarget(target)
	// Do something with the swatch for a given target...
}
Output:

func NewPaletteBuilder

func NewPaletteBuilder(image image.Image) *PaletteBuilder

NewPaletteBuilder creates a new PaletteBuilder for an image.

func (*PaletteBuilder) AddFilter

func (b *PaletteBuilder) AddFilter(filter Filter) *PaletteBuilder

Add a filter to be able to have fine grained control over which colors are allowed in the resulting palette.

func (*PaletteBuilder) AddTarget

func (b *PaletteBuilder) AddTarget(target *Target) *PaletteBuilder

Add a target profile to be generated in the palette.

You can retrieve the result via Palette#getSwatchForTarget(Target).

func (*PaletteBuilder) ClearFilters

func (b *PaletteBuilder) ClearFilters() *PaletteBuilder

Clear all added filters. This includes any default filters added automatically.

func (*PaletteBuilder) ClearRegion

func (b *PaletteBuilder) ClearRegion() *PaletteBuilder

Clear a previously specified region.

func (*PaletteBuilder) ClearTargets

func (b *PaletteBuilder) ClearTargets() *PaletteBuilder

Clear all added targets. This includes any default targets added automatically.

func (*PaletteBuilder) Generate

func (b *PaletteBuilder) Generate() *Palette

Generate and return the Palette.

func (*PaletteBuilder) MaximumColorCount

func (b *PaletteBuilder) MaximumColorCount(colors uint32) *PaletteBuilder

Set the maximum number of colors to use in the quantization step.

Good values for depend on the source image type. For landscapes, good values are in the range 10-16. For images which are largely made up of people's faces then this value should be increased to ~24.

func (*PaletteBuilder) Region

func (b *PaletteBuilder) Region(region image.Rectangle) *PaletteBuilder

Set a region of the image to be used exclusively when calculating the palette.

func (*PaletteBuilder) ResizeImageArea

func (b *PaletteBuilder) ResizeImageArea(area uint64) *PaletteBuilder

Set the resize value. If the image's area is greater than the value specified, then the image will be resized so that it's area matches the provided area. If the image is smaller or equal, the original is used as-is.

This value has a large effect on the processing time. The larger the resized image is, the greater time it will take to generate the palette. The smaller the image is, the more detail is lost in the resulting image and thus less precision for color selection.

A value of 0 can be used to disable resizing.

func (*PaletteBuilder) Scaler

func (b *PaletteBuilder) Scaler(scaler draw.Scaler) *PaletteBuilder

Specify the scaling function used to resize an image. Set to nil to disable resizing.

type PriorityQueue

type PriorityQueue interface {
	Offer(items ...interface{})
	Poll() interface{}
	Len() int
}

A PriorityQueue implements heap.Interface and holds items.

func NewPriorityQueue

func NewPriorityQueue(initialCapacity uint32, priorityFunction func(interface{}) uint32) PriorityQueue

NewPriorityQueue creates a new PriorityQueue with a given capacity and priority function.

type QuantizedColor

type QuantizedColor uint16

QuantizedColor represents a reduced RGB color space.

func (QuantizedColor) ApproximateBlue

func (q QuantizedColor) ApproximateBlue() uint8

ApproximateBlue is the approximate blue component of a quantized color.

func (QuantizedColor) ApproximateGreen

func (q QuantizedColor) ApproximateGreen() uint8

ApproximateGreen is the approximate green component of a quantized color.

func (QuantizedColor) ApproximateRGBA

func (q QuantizedColor) ApproximateRGBA() uint32

ApproximateRGBA is the approximate RGBA value of the quantized color.

func (QuantizedColor) ApproximateRed

func (q QuantizedColor) ApproximateRed() uint8

ApproximateRed is the approximate red component of the quantized color.

func (QuantizedColor) QuantizedBlue

func (q QuantizedColor) QuantizedBlue() uint8

QuantizedBlue is the blue component of a quantized color.

func (QuantizedColor) QuantizedGreen

func (q QuantizedColor) QuantizedGreen() uint8

QuantizedGreen is the green component of a quantized color.

func (QuantizedColor) QuantizedRed

func (q QuantizedColor) QuantizedRed() uint8

QuantizedRed is the red component of the quantized color.

func (QuantizedColor) RGBA

func (q QuantizedColor) RGBA() (uint32, uint32, uint32, uint32)

RGBA implements the color.Color interface.

func (QuantizedColor) SwapRedBlue

func (q QuantizedColor) SwapRedBlue() QuantizedColor

SwapRedBlue returns a new QuantizedColor whose red and blue color components have been swapped.

func (QuantizedColor) SwapRedGreen

func (q QuantizedColor) SwapRedGreen() QuantizedColor

SwapRedGreen returns a new QuantizedColor whose red and green color components have been swapped.

type QuantizedColorSlice

type QuantizedColorSlice []QuantizedColor

QuantizedColorSlice attaches the methods of sort.Interface to []QuantizedColor, sorting in increasing order.

func (QuantizedColorSlice) Len

func (s QuantizedColorSlice) Len() int

func (QuantizedColorSlice) Less

func (s QuantizedColorSlice) Less(i, j int) bool

func (QuantizedColorSlice) Swap

func (s QuantizedColorSlice) Swap(i, j int)

type RGBAInt

type RGBAInt uint32

RGBAInt represents a packed RGBA color.

func (RGBAInt) PackedRGB

func (c RGBAInt) PackedRGB() uint32

PackedRGB is the packed int representing the RGB value (ignores the alpha channel).

func (RGBAInt) PackedRGBA

func (c RGBAInt) PackedRGBA() uint32

PackedRGBA is the packed int representing the RGBA value.

func (RGBAInt) RGBA

func (c RGBAInt) RGBA() (uint32, uint32, uint32, uint32)

RGBA implements the color.Color interface.

func (RGBAInt) String

func (c RGBAInt) String() string

type Scorer

type Scorer interface {
	// Score returns the score for a Swatch.
	Score(*Swatch) float64
}

Scorer used to score a Swatch for a Target.

func NewDefaultScorer

func NewDefaultScorer(target Target, maxPopulation uint32) Scorer

NewDefaultScorer creates a new default scorer.

type ScorerFactory

type ScorerFactory func(Target, []*Swatch) Scorer

ScorerFactory creates a Scorer.

var (
	// A function which generates a Scorer a target.
	DefaultScorerFactory ScorerFactory
)

The default scorer factory.

type SubImager

type SubImager interface {
	SubImage(r image.Rectangle) image.Image
}

SubImager is a utility interface for an image.Image that can extract a sub-image.

type Swatch

type Swatch struct {
	// contains filtered or unexported fields
}

Swatch represents a color swatch generated from an image's palette.

func NewSwatch

func NewSwatch(color color.Color, population uint32) *Swatch

NewSwatch creates a new Swatch from a color and population.

func (Swatch) Color

func (s Swatch) Color() color.Color

Color returns this swatch's color value.

func (*Swatch) HSL

func (s *Swatch) HSL() HSL

HSL returns this swatch's color value as an HSL.

func (Swatch) Population

func (s Swatch) Population() uint32

Population returns the number of pixels represented by this swatch.

func (*Swatch) RGBAInt

func (s *Swatch) RGBAInt() RGBAInt

RGBAInt returns this swatch's color value as an RGBAInt.

type Target

type Target struct {
	// contains filtered or unexported fields
}

Target allows custom selection of colors in a Palette's generation. Instances can be created via TargetBuilder.

To use the target, use the PaletteBuilder#addTarget(Target) API when building a Palette.

var (
	// A target which has the characteristics of a vibrant color which is light in luminance.
	LightVibrant *Target
	// A target which has the characteristics of a vibrant color which is neither light or dark.
	Vibrant *Target
	// A target which has the characteristics of a vibrant color which is dark in luminance.
	DarkVibrant *Target
	// A target which has the characteristics of a muted color which is light in luminance.
	LightMuted *Target
	// A target which has the characteristics of a muted color which is neither light or dark.
	Muted *Target
	// A target which has the characteristics of a muted color which is dark in luminance.
	DarkMuted *Target
)

The default targets.

func (Target) IsExclusive

func (t Target) IsExclusive() bool

Returns whether any color selected for this target is exclusive for this target only.

If false, then the color can be selected for other targets.

func (Target) MaximumLightness

func (t Target) MaximumLightness() float64

The maximum lightness value for this target.

func (Target) MaximumSaturation

func (t Target) MaximumSaturation() float64

The maximum saturation value for this target.

func (Target) MinimumLightness

func (t Target) MinimumLightness() float64

The minimum lightness value for this target.

func (Target) MinimumSaturation

func (t Target) MinimumSaturation() float64

The minimum saturation value for this target.

func (Target) Scorer

func (t Target) Scorer(swatches []*Swatch) Scorer

Returns a Scorer for the target.

func (Target) String

func (t Target) String() string

func (Target) TargetLightness

func (t Target) TargetLightness() float64

The target lightness value for this target.

func (Target) TargetSaturation

func (t Target) TargetSaturation() float64

The target saturation value for this target.

type TargetBuilder

type TargetBuilder struct {
	// contains filtered or unexported fields
}

TargetBuilder is used for generating Target instances.

func NewTargetBuilder

func NewTargetBuilder() *TargetBuilder

NewTargetBuilder creates a new TargetBuilder from scratch.

func NewTargetBuilderFromTarget

func NewTargetBuilderFromTarget(target *Target) *TargetBuilder

NewTargetBuilderFromTarget creates a new TargetBuilder based on an existing Target.

func (*TargetBuilder) Build

func (b *TargetBuilder) Build() *Target

Build returns a copy of the Target.

func (*TargetBuilder) IsExclusive

func (b *TargetBuilder) IsExclusive(exclusive bool) *TargetBuilder

Set whether any color selected for this target is exclusive to this target only. Defaults to true.

func (*TargetBuilder) MaximumLightness

func (b *TargetBuilder) MaximumLightness(value float64) *TargetBuilder

Set the maximum lightness value for this target.

func (*TargetBuilder) MaximumSaturation

func (b *TargetBuilder) MaximumSaturation(value float64) *TargetBuilder

Set the maximum saturation value for this target.

func (*TargetBuilder) MinimumLightness

func (b *TargetBuilder) MinimumLightness(value float64) *TargetBuilder

Set the minimum lightness value for this target.

func (*TargetBuilder) MinimumSaturation

func (b *TargetBuilder) MinimumSaturation(value float64) *TargetBuilder

Set the minimum saturation value for this target.

func (*TargetBuilder) ScorerFactory

func (b *TargetBuilder) ScorerFactory(factory ScorerFactory) *TargetBuilder

Set the ScorerFactory for the target. The ScorerFactory is used to generate a Scorer for a target from a slice of swatches.

func (*TargetBuilder) TargetLightness

func (b *TargetBuilder) TargetLightness(value float64) *TargetBuilder

Set the target/ideal lightness value for this target.

func (*TargetBuilder) TargetSaturation

func (b *TargetBuilder) TargetSaturation(value float64) *TargetBuilder

Set the target/ideal saturation value for this target.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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