Back to godoc.org
github.com/generaltso/vibrant

package vibrant

v0.0.0 (89f84c2)
Latest Go to latest
Published: May 13, 2020 | License: Apache-2.0 | Module: github.com/generaltso/vibrant

Overview

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

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

type Color int

func (Color) BodyTextColor

func (c Color) BodyTextColor() Color

Returns either black or white based on MIN_CONTRAST_BODY_TEXT

func (Color) RGB

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

func (Color) RGBHex

func (c Color) RGBHex() string

e.g. "#bada55"

func (Color) String

func (c Color) String() string

Same as RGBHex()

func (Color) TextColor

func (c Color) TextColor(contrastRatio float64) Color

Returns either black or white based on contrastRatio.

func (Color) TitleTextColor

func (c Color) TitleTextColor() Color

Returns either black or white based on MIN_CONTRAST_TITLE_TEXT

type Palette

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

func NewPalette

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

func NewPaletteFromImage

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

Calls NewPalette with DEFAULT_CALCULATE_NUMBER_COLORS as a default value for numColors.

func (*Palette) ExtractAwesome

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

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

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

func (*Swatch) String

func (sw *Swatch) String() string

Convenience method that returns CSS e.g.

.vibrant{background-color:#bada55;color:#ffffff;}
Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
f or F : Jump to identifier