material

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: MIT, MIT Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package material computes Material You color schemes from an image, a port of Google's material-color-utilities (the TypeScript release 0.4.0, 2021 color spec): CAM16 and HCT, the Celebi quantizer (Wu, then WSMeans), the seed scorer, tonal palettes and the dynamic scheme roles of every variant, at any contrast level.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContrastRatio

func ContrastRatio(a, b ARGB) float64

ContrastRatio is the WCAG contrast ratio between two colors, 1 to 21.

Types

type ARGB

type ARGB uint32

ARGB is an opaque sRGB color packed as 0xAARRGGBB.

const FallbackSeed ARGB = 0xff4285f4

FallbackSeed is the seed Score ranks when no color is suitable: a Google blue.

func RGB

func RGB(r, g, b uint8) ARGB

RGB packs 8-bit channels into an opaque color.

func Sample

func Sample(img *image.RGBA, side int) []ARGB

Sample reads img at a grid of at most side samples along its longer edge, keeping its aspect, one pixel at the center of each cell, and returns the opaque ones. Its work and its one allocation are bounded by the grid, whatever the image's resolution.

func Score

func Score(colors []Population) []ARGB

Score ranks the quantized colors as theme seeds, best first: up to four colors of distinct hue, weighing chroma against how much of the image lies near each hue. It is never empty: FallbackSeed stands in when no color qualifies.

func (ARGB) Hex

func (c ARGB) Hex() string

Hex is the color as #rrggbb.

type HCT

type HCT struct {
	Hue, Chroma, Tone float64
	// contains filtered or unexported fields
}

HCT is a color in Google's hue, chroma and tone space: CAM16 hue and chroma with L* tone.

func HCTOf

func HCTOf(c ARGB) HCT

HCTOf is the HCT of an sRGB color.

func SolveHCT

func SolveHCT(hue, chroma, tone float64) HCT

SolveHCT is the sRGB color nearest to the requested hue, chroma and tone: the tone and hue are kept, the chroma is the most sRGB allows up to the request.

func (HCT) ARGB

func (h HCT) ARGB() ARGB

ARGB is the sRGB color.

type Population

type Population struct {
	Color ARGB
	Count int
}

Population is one quantized color and the number of pixels it stands for.

func Quantize

func Quantize(pixels []ARGB, maxColors int) []Population

Quantize reduces the opaque pixels to at most maxColors colors with the Celebi quantizer (Wu boxes refined by WSMeans), in the order the reference implementation reports them.

type Role

type Role uint8

Role is a named color of a dynamic scheme.

const (
	RolePrimaryPaletteKeyColor Role = iota
	RoleSecondaryPaletteKeyColor
	RoleTertiaryPaletteKeyColor
	RoleNeutralPaletteKeyColor
	RoleNeutralVariantPaletteKeyColor
	RoleErrorPaletteKeyColor
	RoleBackground
	RoleOnBackground
	RoleSurface
	RoleSurfaceDim
	RoleSurfaceBright
	RoleSurfaceContainerLowest
	RoleSurfaceContainerLow
	RoleSurfaceContainer
	RoleSurfaceContainerHigh
	RoleSurfaceContainerHighest
	RoleOnSurface
	RoleSurfaceVariant
	RoleOnSurfaceVariant
	RoleInverseSurface
	RoleInverseOnSurface
	RoleOutline
	RoleOutlineVariant
	RoleShadow
	RoleScrim
	RoleSurfaceTint
	RolePrimary
	RoleOnPrimary
	RolePrimaryContainer
	RoleOnPrimaryContainer
	RoleInversePrimary
	RoleSecondary
	RoleOnSecondary
	RoleSecondaryContainer
	RoleOnSecondaryContainer
	RoleTertiary
	RoleOnTertiary
	RoleTertiaryContainer
	RoleOnTertiaryContainer
	RoleError
	RoleOnError
	RoleErrorContainer
	RoleOnErrorContainer
	RolePrimaryFixed
	RolePrimaryFixedDim
	RoleOnPrimaryFixed
	RoleOnPrimaryFixedVariant
	RoleSecondaryFixed
	RoleSecondaryFixedDim
	RoleOnSecondaryFixed
	RoleOnSecondaryFixedVariant
	RoleTertiaryFixed
	RoleTertiaryFixedDim
	RoleOnTertiaryFixed
	RoleOnTertiaryFixedVariant
	// RoleCount is the number of roles.
	RoleCount
)

The scheme roles, named as matugen's JSON names them.

func (Role) String

func (r Role) String() string

String is the role's snake_case name.

type Scheme

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

Scheme is every color role of one dynamic scheme.

func NewScheme

func NewScheme(seed ARGB, variant Variant, dark bool, contrast float64) Scheme

NewScheme builds the scheme of a seed color in one variant, light or dark, at a contrast level from -1 (reduced) through 0 (standard) to 1 (high).

func (*Scheme) Color

func (s *Scheme) Color(r Role) ARGB

Color is the scheme's color for a role.

type Variant

type Variant uint8

Variant is a dynamic scheme style.

const (
	Monochrome Variant = iota
	Neutral
	TonalSpot
	Vibrant
	Expressive
	Fidelity
	Content
	Rainbow
	FruitSalad
)

The scheme variants of the 2021 spec.

func (Variant) String

func (v Variant) String() string

Jump to

Keyboard shortcuts

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