material

package module
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2025 License: Apache-2.0 Imports: 13 Imported by: 0

README

Material

GitHub go.mod Go version GitHub Repo stars GitHub License GitHub Tag

[!IMPORTANT] 🔥 Found this useful? A quick star goes a long way.

A pure go implementation of Material Color Utilities without any external dependencies.

Feature

Example

Find more Examples.

package main

import (
	"encoding/json"
	"image/jpeg"
	"log"
	"os"

	"github.com/Nadim147c/material/v2"
)

func main() {
	f, err := os.Open("quantizer/gophar.jpg")
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	img, err := jpeg.Decode(f)
	if err != nil {
		log.Fatal(err)
	}

	colors, err := material.Generate(
		material.FromImage(img),
		material.WithDark(true),
		material.WithVariant(material.VariantTonalSpot),
	)
	if err != nil {
		log.Fatal(err)
	}

	json.NewEncoder(os.Stdout).Encode(colors) // prints the colors as json
}

License

This project is licensed under the Apache License, Version 2.0.

It incorporates code derived from Material Color Utilities by Google LLC, used under the terms of the Apache License, Version 2.0.

The included gophar image, used as test data, is licensed under the Creative Commons Attribution 4.0 International (CC BY 4.0) license, attributed to Renee French.

For additional details and third-party attributions, see the NOTICE file.

Documentation

Index

Examples

Constants

View Source
const (
	// PlatformPhone is a Platform of type Phone.
	PlatformPhone = dynamic.PlatformPhone
	// PlatformWatch is a Platform of type Watch.
	PlatformWatch = dynamic.PlatformWatch
	// VariantMonochrome is a Variant of type Monochrome.
	VariantMonochrome = dynamic.VariantMonochrome
	// VariantNeutral is a Variant of type Neutral.
	VariantNeutral = dynamic.VariantNeutral
	// VariantTonalSpot is a Variant of type Tonal_spot.
	VariantTonalSpot = dynamic.VariantTonalSpot
	// VariantVibrant is a Variant of type Vibrant.
	VariantVibrant = dynamic.VariantVibrant
	// VariantExpressive is a Variant of type Expressive.
	VariantExpressive = dynamic.VariantExpressive
	// VariantFidelity is a Variant of type Fidelity.
	VariantFidelity = dynamic.VariantFidelity
	// VariantContent is a Variant of type Content.
	VariantContent = dynamic.VariantContent
	// VariantRainbow is a Variant of type Rainbow.
	VariantRainbow = dynamic.VariantRainbow
	// VariantFruitSalad is a Variant of type Fruit_salad.
	VariantFruitSalad = dynamic.VariantFruitSalad
	// Version2021 is a Version of type 2021.
	Version2021 = dynamic.Version2021
	// Version2025 is a Version of type 2025.
	Version2025 = dynamic.Version2025
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Colors

type Colors struct {
	Scheme *dynamic.Scheme `json:"scheme,omitzero"`

	CustomColors map[string]CustomColor `json:"custom"`

	Background              color.ARGB `json:"background"`
	Error                   color.ARGB `json:"error"`
	ErrorContainer          color.ARGB `json:"error_container"`
	ErrorDim                color.ARGB `json:"error_dim"`
	InverseOnSurface        color.ARGB `json:"inverse_on_surface"`
	InversePrimary          color.ARGB `json:"inverse_primary"`
	InverseSurface          color.ARGB `json:"inverse_surface"`
	OnBackground            color.ARGB `json:"on_background"`
	OnError                 color.ARGB `json:"on_error"`
	OnErrorContainer        color.ARGB `json:"on_error_container"`
	OnPrimary               color.ARGB `json:"on_primary"`
	OnPrimaryContainer      color.ARGB `json:"on_primary_container"`
	OnPrimaryFixed          color.ARGB `json:"on_primary_fixed"`
	OnPrimaryFixedVariant   color.ARGB `json:"on_primary_fixed_variant"`
	OnSecondary             color.ARGB `json:"on_secondary"`
	OnSecondaryContainer    color.ARGB `json:"on_secondary_container"`
	OnSecondaryFixed        color.ARGB `json:"on_secondary_fixed"`
	OnSecondaryFixedVariant color.ARGB `json:"on_secondary_fixed_variant"`
	OnSurface               color.ARGB `json:"on_surface"`
	OnSurfaceVariant        color.ARGB `json:"on_surface_variant"`
	OnTertiary              color.ARGB `json:"on_tertiary"`
	OnTertiaryContainer     color.ARGB `json:"on_tertiary_container"`
	OnTertiaryFixed         color.ARGB `json:"on_tertiary_fixed"`
	OnTertiaryFixedVariant  color.ARGB `json:"on_tertiary_fixed_variant"`
	Outline                 color.ARGB `json:"outline"`
	OutlineVariant          color.ARGB `json:"outline_variant"`
	Primary                 color.ARGB `json:"primary"`
	PrimaryContainer        color.ARGB `json:"primary_container"`
	PrimaryDim              color.ARGB `json:"primary_dim"`
	PrimaryFixed            color.ARGB `json:"primary_fixed"`
	PrimaryFixedDim         color.ARGB `json:"primary_fixed_dim"`
	Scrim                   color.ARGB `json:"scrim"`
	Secondary               color.ARGB `json:"secondary"`
	SecondaryContainer      color.ARGB `json:"secondary_container"`
	SecondaryDim            color.ARGB `json:"secondary_dim"`
	SecondaryFixed          color.ARGB `json:"secondary_fixed"`
	SecondaryFixedDim       color.ARGB `json:"secondary_fixed_dim"`
	Shadow                  color.ARGB `json:"shadow"`
	Surface                 color.ARGB `json:"surface"`
	SurfaceBright           color.ARGB `json:"surface_bright"`
	SurfaceContainer        color.ARGB `json:"surface_container"`
	SurfaceContainerHigh    color.ARGB `json:"surface_container_high"`
	SurfaceContainerHighest color.ARGB `json:"surface_container_highest"`
	SurfaceContainerLow     color.ARGB `json:"surface_container_low"`
	SurfaceContainerLowest  color.ARGB `json:"surface_container_lowest"`
	SurfaceDim              color.ARGB `json:"surface_dim"`
	SurfaceTint             color.ARGB `json:"surface_tint"`
	SurfaceVariant          color.ARGB `json:"surface_variant"`
	Tertiary                color.ARGB `json:"tertiary"`
	TertiaryContainer       color.ARGB `json:"tertiary_container"`
	TertiaryDim             color.ARGB `json:"tertiary_dim"`
	TertiaryFixed           color.ARGB `json:"tertiary_fixed"`
	TertiaryFixedDim        color.ARGB `json:"tertiary_fixed_dim"`
	// contains filtered or unexported fields
}

Colors is generated material you colors

func Generate

func Generate(src Source, options ...Option) (*Colors, error)

Generate generates material you colors

Example
c := color.ARGBFromHexMust("#FF0000")
colors, _ := Generate(
	FromColor(c),
	WithDark(true),
	WithContrast(0.5),
	WithVariant(VariantExpressive),
)
fmt.Println(colors.Primary)

func (*Colors) Map

func (c *Colors) Map() map[string]color.ARGB

Map returns map with color name in snake case as name and color.ARGB as value

type CustomColor

type CustomColor struct {
	Color            color.ARGB `json:"color"`
	OnColor          color.ARGB `json:"on_color"`
	ColorContainer   color.ARGB `json:"color_container"`
	OnColorContainer color.ARGB `json:"on_color_container"`
}

CustomColor is the custom colors generated from user defined colors

type CustomColorOption

type CustomColorOption struct {
	Blend bool
	Ratio float64
	Color color.ARGB
}

CustomColorOption is used define custom color

type Option

type Option func(s *Settings)

Option is a func modifes the dynamic scheme settings

func WithContext

func WithContext(ctx context.Context) Option

WithContext returns an Option that set the context

func WithContrast

func WithContrast(c float64) Option

WithContrast returns an Option that sets the contrast level

func WithCustomColor

func WithCustomColor(name string, c gocolor.Color) Option

WithCustomColor returns an Option that adds a custom color.

func WithCustomColorBlend

func WithCustomColorBlend(name string, c gocolor.Color, ratio float64) Option

WithCustomColorBlend returns an Option that adds a custom color which will be blended with primary color by geven ratio. Ratio range [0, 1].

func WithDark

func WithDark(d bool) Option

WithDark returns an Option that sets the dark mode flag

func WithPlatform

func WithPlatform(p dynamic.Platform) Option

WithPlatform returns an Option that sets the platform

func WithSettings

func WithSettings(s Settings) Option

WithSettings settings all values of settings

func WithVariant

func WithVariant(v dynamic.Variant) Option

WithVariant returns an Option that sets the variant

func WithVersion

func WithVersion(v dynamic.Version) Option

WithVersion returns an Option that sets the version

type Settings

type Settings struct {
	Context  context.Context  `json:"-"` // context shouldn't be encoded
	Contrast float64          `json:"contrast"`
	Dark     bool             `json:"dark"`
	Platform dynamic.Platform `json:"platform"`
	Variant  dynamic.Variant  `json:"variant"`
	Version  dynamic.Version  `json:"version"`

	Custom map[string]CustomColorOption `json:"-"`
}

Settings is the dynamic schema configuration

type Source

type Source func() ([]color.ARGB, error)

Source is a function that returns source colors for material you

func Combine

func Combine(sources ...Source) Source

Combine returns a Source that merges multiple Sources into one

func Filter

func Filter(source Source, predicate func(color.ARGB) bool) Source

Filter returns a Source that filters colors using a predicate function

func FromARGB

func FromARGB(argbs []color.ARGB) Source

FromARGB returns Source colors from a slice of ARGB values

func FromBytes

func FromBytes(b []byte) Source

FromBytes returns Source colors from a byte slice.

WARNING: DO NOT pass image encoded file buffer. byte slice should be a sequence of r, g, b bytes (3 bytes per color).

func FromColor

func FromColor(c gocolor.Color) Source

FromColor returns a Source from a single color.Color interface

func FromColors

func FromColors(s []gocolor.Color) Source

FromColors returns a Source from a slice of color.Color interfaces

func FromHex

func FromHex(hex string) Source

FromHex returns a Source from a hex color string (e.g., "#FF5733" or "FF5733")

func FromHexes

func FromHexes(hexes []string) Source

FromHexes returns a Source from multiple hex color strings

func FromImage

func FromImage(img image.Image) Source

FromImage returns Source colors from image.Image interface

func FromReader

func FromReader(r io.Reader) Source

FromReader returns Source colors from an io.Reader containing RGB bytes. Reads until EOF and extracts RGB triplets from the stream.

WARNING: Do NOT pass image encoded file readers (e.g., PNG, JPEG). Use FromImage with image.Decode for encoded image files.

Directories

Path Synopsis
examples
color command
ffmpeg command
gradient command
image command

Jump to

Keyboard shortcuts

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