Documentation
¶
Index ¶
- Constants
- Variables
- func ScaleImageDown(src image.Image, resizeArea uint64, scaler draw.Scaler) image.Image
- type ColorCutQuantizer
- type DefaultScorer
- type Filter
- type HSL
- type Palette
- func (p *Palette) ColorForTarget(target *Target, defaultColor uint32) uint32
- func (p *Palette) DarkMutedColor(defaultColor uint32) uint32
- func (p *Palette) DarkMutedSwatch() *Swatch
- func (p *Palette) DarkVibrantColor(defaultColor uint32) uint32
- func (p *Palette) DarkVibrantSwatch() *Swatch
- func (p *Palette) LightMutedColor(defaultColor uint32) uint32
- func (p *Palette) LightMutedSwatch() *Swatch
- func (p *Palette) LightVibrantColor(defaultColor uint32) uint32
- func (p *Palette) LightVibrantSwatch() *Swatch
- func (p *Palette) MutedColor(defaultColor uint32) uint32
- func (p *Palette) MutedSwatch() *Swatch
- func (p *Palette) SwatchForTarget(target *Target) *Swatch
- func (p *Palette) Swatches() []*Swatch
- func (p *Palette) Targets() []*Target
- func (p *Palette) VibrantColor(defaultColor uint32) uint32
- func (p *Palette) VibrantSwatch() *Swatch
- type PaletteBuilder
- func (b *PaletteBuilder) AddFilter(filter Filter) *PaletteBuilder
- func (b *PaletteBuilder) AddTarget(target *Target) *PaletteBuilder
- func (b *PaletteBuilder) ClearFilters() *PaletteBuilder
- func (b *PaletteBuilder) ClearRegion() *PaletteBuilder
- func (b *PaletteBuilder) ClearTargets() *PaletteBuilder
- func (b *PaletteBuilder) Generate() *Palette
- func (b *PaletteBuilder) MaximumColorCount(colors uint32) *PaletteBuilder
- func (b *PaletteBuilder) Region(region image.Rectangle) *PaletteBuilder
- func (b *PaletteBuilder) ResizeImageArea(area uint64) *PaletteBuilder
- func (b *PaletteBuilder) Scaler(scaler draw.Scaler) *PaletteBuilder
- type PriorityQueue
- type QuantizedColor
- func (q QuantizedColor) ApproximateBlue() uint8
- func (q QuantizedColor) ApproximateGreen() uint8
- func (q QuantizedColor) ApproximateRGBA() uint32
- func (q QuantizedColor) ApproximateRed() uint8
- func (q QuantizedColor) QuantizedBlue() uint8
- func (q QuantizedColor) QuantizedGreen() uint8
- func (q QuantizedColor) QuantizedRed() uint8
- func (q QuantizedColor) RGBA() (uint32, uint32, uint32, uint32)
- func (q QuantizedColor) SwapRedBlue() QuantizedColor
- func (q QuantizedColor) SwapRedGreen() QuantizedColor
- type QuantizedColorSlice
- type RGBAInt
- type Scorer
- type ScorerFactory
- type SubImager
- type Swatch
- type Target
- func (t Target) IsExclusive() bool
- func (t Target) MaximumLightness() float64
- func (t Target) MaximumSaturation() float64
- func (t Target) MinimumLightness() float64
- func (t Target) MinimumSaturation() float64
- func (t Target) Scorer(swatches []*Swatch) Scorer
- func (t Target) String() string
- func (t Target) TargetLightness() float64
- func (t Target) TargetSaturation() float64
- type TargetBuilder
- func (b *TargetBuilder) Build() *Target
- func (b *TargetBuilder) IsExclusive(exclusive bool) *TargetBuilder
- func (b *TargetBuilder) MaximumLightness(value float64) *TargetBuilder
- func (b *TargetBuilder) MaximumSaturation(value float64) *TargetBuilder
- func (b *TargetBuilder) MinimumLightness(value float64) *TargetBuilder
- func (b *TargetBuilder) MinimumSaturation(value float64) *TargetBuilder
- func (b *TargetBuilder) ScorerFactory(factory ScorerFactory) *TargetBuilder
- func (b *TargetBuilder) TargetLightness(value float64) *TargetBuilder
- func (b *TargetBuilder) TargetSaturation(value float64) *TargetBuilder
Examples ¶
Constants ¶
const ( DefaultFilterMinLightness = 0.05 DefaultFilterMaxLightness = 0.95 )
Default values for the default filter.
const ( DefaultResizeArea = uint64(320 * 320) DefaultMaximumColorCount = uint32(64) )
Defaults used by the default PaletteBuilder.
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 ¶
var DefaultFilter = NewLightnessFilter(DefaultFilterMinLightness, DefaultFilterMaxLightness)
DefaultFilter removes colors close to white and black.
var (
DefaultScaler = draw.ApproxBiLinear
)
Defaults used by the default PaletteBuilder.
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.
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.
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.
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 ¶
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.
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 ¶
NewLightnessFilter creates a Filter that removes colors based on a lightness.
type HSL ¶
HSL represents the HSL value for an RGBA color.
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 ¶
Returns the selected color for the given target from the palette as an RGB packed int.
func (*Palette) DarkMutedColor ¶
Returns a muted and dark color from the palette as an RGB packed int.
func (*Palette) DarkMutedSwatch ¶
Returns a muted and dark swatch from the palette. Might be nil.
func (*Palette) DarkVibrantColor ¶
Returns a dark and vibrant color from the palette as an RGB packed int.
func (*Palette) DarkVibrantSwatch ¶
Returns a dark and vibrant swatch from the palette. Might be nil.
func (*Palette) LightMutedColor ¶
Returns a muted and light color from the palette as an RGB packed int.
func (*Palette) LightMutedSwatch ¶
Returns a muted and light swatch from the palette. Might be nil.
func (*Palette) LightVibrantColor ¶
Returns a light and vibrant color from the palette as an RGB packed int.
func (*Palette) LightVibrantSwatch ¶
Returns a light and vibrant swatch from the palette. Might be nil.
func (*Palette) MutedColor ¶
Returns a muted color from the palette as an RGB packed int.
func (*Palette) MutedSwatch ¶
Returns a muted swatch from the palette. Might be nil.
func (*Palette) SwatchForTarget ¶
Returns the selected swatch for the given target from the palette, or nil if one could not be found.
func (*Palette) VibrantColor ¶
Returns the most vibrant color in the palette as an RGB packed int.
func (*Palette) VibrantSwatch ¶
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 ¶
PackedRGB is the packed int representing the RGB value (ignores the alpha channel).
func (RGBAInt) PackedRGBA ¶
PackedRGBA is the packed int representing the RGBA value.
type Scorer ¶
Scorer used to score a Swatch for a Target.
func NewDefaultScorer ¶
NewDefaultScorer creates a new default scorer.
type ScorerFactory ¶
ScorerFactory creates a Scorer.
var ( // A function which generates a Scorer a target. DefaultScorerFactory ScorerFactory )
The default scorer factory.
type Swatch ¶
type Swatch struct {
// contains filtered or unexported fields
}
Swatch represents a color swatch generated from an image's palette.
func (Swatch) Population ¶
Population returns the number of pixels represented by this swatch.
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 ¶
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 ¶
The maximum lightness value for this target.
func (Target) MaximumSaturation ¶
The maximum saturation value for this target.
func (Target) MinimumLightness ¶
The minimum lightness value for this target.
func (Target) MinimumSaturation ¶
The minimum saturation value for this target.
func (Target) TargetLightness ¶
The target lightness value for this target.
func (Target) TargetSaturation ¶
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.