vibrant

package module
v0.0.0-...-08d3d20 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2023 License: Apache-2.0 Imports: 10 Imported by: 3

README

vibrant

godoc reference

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

screenshot of app.go

install

go get github.com/dayvonjersen/vibrant

usage

// 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;}

See godoc reference for full API.

bonus round

reference implementation/command line tool

go get github.com/dayvonjersen/vibrant/cmd/vibrant
usage: vibrant [options] file

options:
  -compress
    	Strip whitespace from output.
  -css
    	Output results in CSS.
  -json
    	Output results in JSON.
  -lowercase
    	Use lowercase only for all output. (default true)
  -rgb
    	Output RGB instead of HTML hex, e.g. #ffffff.

webapp

go get github.com/dayvonjersen/vibrant
cd $GOPATH/src/github.com/dayvonjersen/vibrant
go run webapp.go
# open http://localhost:8080/ in a browser

thanks

https://github.com/Infinity/Iris

This Google I/O 2014 presentation

https://github.com/jariz/vibrant.js

https://github.com/akfish/node-vibrant

Documentation

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

View Source
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.

Variables

This section is empty.

Functions

This section is empty.

Types

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;}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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