vibrant: Index | Files

package vibrant

import ""

Extract prominent colors from images. Go port of the Android Palette class aka Vibrant.js

// example: create css stylesheet from image file
checkErr := func(err error) { if err != nil { panic(err) } }

f, err := os.Open("some_image.jpg")
defer f.Close()

img, _, err := image.Decode(f)

palette, err := vibrant.NewPaletteFromImage(img)

for name, swatch := range palette.ExtractAwesome() {
	fmt.Printf("/* %s (population: %d) *\/\n%s\n\n", name, swatch.Population, swatch)


/* LightMuted (population: 253) *\/

/* DarkMuted (population: 11069) *\/

/* Vibrant (population: 108) *\/

/* LightVibrant (population: 87) *\/

/* DarkVibrant (population: 2932) *\/

/* Muted (population: 4098) *\/


Package Files

bitmap.go color.go colorcutquantizer.go colorhistogram.go godoc.go palette.go priorityqueue.go swatch.go vbox.go


const (
    TARGET_DARK_LUMA                = 0.26
    MAX_DARK_LUMA                   = 0.45
    MIN_LIGHT_LUMA                  = 0.55
    TARGET_LIGHT_LUMA               = 0.74
    MIN_NORMAL_LUMA                 = 0.3
    TARGET_NORMAL_LUMA              = 0.5
    MAX_NORMAL_LUMA                 = 0.7
    MAX_MUTED_SATURATION            = 0.4
    MIN_VIBRANT_SATURATION          = 0.35
    WEIGHT_SATURATION               = 3
    WEIGHT_LUMA                     = 6
    WEIGHT_POPULATION               = 1
    MIN_CONTRAST_TITLE_TEXT         = 3.0
    MIN_CONTRAST_BODY_TEXT          = 4.5

These constants are taken directly from the Android Palette source code, with the exception of DEFAULT_CALCULATE_NUMBER_COLORS which was 16.

type Color Uses

type Color int

func (Color) BodyTextColor Uses

func (c Color) BodyTextColor() Color

Returns either black or white based on MIN_CONTRAST_BODY_TEXT

func (Color) RGB Uses

func (c Color) RGB() (r, g, b int)

func (Color) RGBHex Uses

func (c Color) RGBHex() string

e.g. "#bada55"

func (Color) String Uses

func (c Color) String() string

Same as RGBHex()

func (Color) TextColor Uses

func (c Color) TextColor(contrastRatio float64) Color

Returns either black or white based on contrastRatio.

func (Color) TitleTextColor Uses

func (c Color) TitleTextColor() Color

Returns either black or white based on MIN_CONTRAST_TITLE_TEXT

type Palette Uses

type Palette struct {
    // contains filtered or unexported fields

func NewPalette Uses

func NewPalette(img image.Image, numColors int) (Palette, error)

func NewPaletteFromImage Uses

func NewPaletteFromImage(img image.Image) (Palette, error)

Calls NewPalette with DEFAULT_CALCULATE_NUMBER_COLORS as a default value for numColors.

func (*Palette) ExtractAwesome Uses

func (p *Palette) ExtractAwesome() map[string]*Swatch

Possible map keys are:


Some or all of these keys might not be set depending on the source image and numColors parameters used when creating the Palette. YMMV

func (*Palette) FindColor Uses

func (p *Palette) FindColor(targetLuma, minLuma, maxLuma, targetSaturation, minSaturation, maxSaturation float64) *Swatch

Finds a Swatch which best matches the specified parameters.

See also package constants.

type Swatch Uses

type Swatch struct {
    Color      Color
    Population int
    Name       string // might be empty

func (*Swatch) String Uses

func (sw *Swatch) String() string

Convenience method that returns CSS e.g.


Package vibrant imports 10 packages (graph) and is imported by 4 packages. Updated 2018-07-19. Refresh now. Tools for package owners.