vibrant: github.com/generaltso/vibrant Index | Files

package vibrant

import "github.com/generaltso/vibrant"

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

https://android.googlesource.com/platform/frameworks/support/+/b14fc7c/v7/palette/src/android/support/v7/graphics/

https://github.com/jariz/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")
checkErr(err)
defer f.Close()

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

palette, err := vibrant.NewPaletteFromImage(img)
checkErr(err)

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

output:

/* LightMuted (population: 253) *\/
.lightmuted{background-color:#cbc0a2;color:#000000;}

/* DarkMuted (population: 11069) *\/
.darkmuted{background-color:#5b553f;color:#ffffff;}

/* Vibrant (population: 108) *\/
.vibrant{background-color:#dfd013;color:#000000;}

/* LightVibrant (population: 87) *\/
.lightvibrant{background-color:#f4ed7d;color:#000000;}

/* DarkVibrant (population: 2932) *\/
.darkvibrant{background-color:#917606;color:#ffffff;}

/* Muted (population: 4098) *\/
.muted{background-color:#a58850;color:#000000;}

Index

Package Files

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

Constants

const (
    DEFAULT_CALCULATE_NUMBER_COLORS = 256
    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
    TARGET_MUTED_SATURATION         = 0.3
    MAX_MUTED_SATURATION            = 0.4
    TARGET_VIBRANT_SATURATION       = 1
    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:

Vibrant
LightVibrant
DarkVibrant
Muted
LightMuted
DarkMuted

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.

.vibrant{background-color:#bada55;color:#ffffff;}

Package vibrant imports 10 packages (graph) and is imported by 1 packages. Updated 2017-05-09. Refresh now. Tools for package owners.