colormix

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2025 License: MIT Imports: 5 Imported by: 0

README

ColorMix - A Go Library for Color Mixing Optimization

Go Reference Go goreleaser GitHub go.mod Go version of a Go module GoReportCard GitHub license GitHub release

ColorMix is a Go library that optimizes color mixing by adjusting the proportions of a given set of colors to match a target color. The library uses optimization algorithms to find the best weights (proportions) for a set of colors in order to minimize the difference between the mixed color and the target color. The optimization is based on color differences, and it supports advanced optimization techniques.

Features

  • Mix colors to match a target color.
  • Uses CIEDE2000 color difference metric for better perceptual accuracy.
  • Supports different optimization algorithms like Nelder-Mead for general use cases.
  • Prevents zero color contributions through penalty functions.

Installation

To install the ColorMix library, use the go get command:

go get -u github.com/bububa/colormix

Example Usage

Import the package
import "github.com/yourusername/colormix"
Basic Example: Mix Colors to Match a Target

Here is a simple example of how to use the library to mix a set of colors and match a target color.

package main

import (
	"fmt"
	"image/color"

	"github.com/bububa/colormix"
)

func main() {
	targetColor := color.RGBA{200, 100, 50, 255}
	palette := colormix.NewPalette(
		// RED
		color.RGBA{255, 0, 0, 255},
		// GREEN
		color.RGBA{0, 255, 0, 255},
		// BLUE
		color.RGBA{0, 0, 255, 255},
		// WHITE
		color.RGBA{255, 255, 255, 255},
		// BLACK
		color.RGBA{0, 0, 0, 255},
	)
	mixedColor, err := colormix.Mix(targetColor, palette, RGB)
	if err != nil {
		return
	}
	fmt.Printf("Target: %s\n", MakeColor(targetColor).Hex())
	fmt.Println("")
	for _, v := range palette.Colors() {
		fmt.Printf("%s: %.0f%%\n", v.Hex(), v.Ratio()*100)
	}
	fmt.Println("")
	fmt.Printf("Mixed: %s\n", MakeColor(mixedColor).Hex())
	// Output:
	// Target: #c86432
	//
	// #ff0000: 56%
	// #00ff00: 11%
	// #0000ff: 1%
	// #ffffff: 2%
	// #000000: 30%
	//
	// Mixed: #c86432
}
Explanation
  1. palette: This is a matrix containing the available colors in RGB format.
  2. targetColor: The target color that we want to match through mixing.
  3. Mix: This function finds the optimal proportions (weights) of the available colors that best approximate the target color.
  4. mixedColor: Once the optimal weights are calculated, this function computes the resulting mixed color based on the color table and the calculated weights.

Color Metrics

The library uses the CIEDE2000 color difference metric to calculate the color differences, which provides a more perceptually accurate measure of color difference than Euclidean distance in RGB space.

Available Functions

MixColors(targetColor color.Color, palette *colormix.Palette, colorspace colormix.ColorMix) (mixedColor color.Color, err error): Computes the optimal weights for the given color table to match the target color and Calculates the final mixed color based on the optimal weights and the color table.

Optimizers Supported

  • Nelder-Mead: A simplex-based optimization method that doesn't require derivatives. Suitable for non-differentiable functions.
  • LBFGS (Limited-memory Broyden–Fletcher–Goldfarb–Shanno): A gradient-based method that is more efficient for smooth, differentiable functions.

Contributing

We welcome contributions! If you find a bug or want to suggest a feature, feel free to open an issue or submit a pull request.

Steps to Contribute:
  1. Fork the repository.
  2. Create a new branch (git checkout -b feature-name).
  3. Make your changes and commit them (git commit -am 'Add new feature').
  4. Push to the branch (git push origin feature-name).
  5. Open a pull request.

License

This project is licensed under the MIT License—see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ColorInSpace

func ColorInSpace(values []float64, space ColorSpace) color.Color

ColorInSpace returns a Color in specific color space

func Mix

func Mix(target color.Color, palette *Palette, space ColorSpace) (color.Color, error)

Mix mix the palette colors with proper ratio to match the target color based on a specific color space returns a mixed color or error

Types

type Color

type Color struct {
	colorful.Color
	ColorMeta
	// contains filtered or unexported fields
}

Color named color

func HexColor added in v1.0.1

func HexColor(v string) Color

Hex create a color

func MakeColor

func MakeColor(v color.Color) Color

MakeColor create a color

func (Color) Dense

func (c Color) Dense(colorSpace ColorSpace) *mat.VecDense

Dense returns vec dense of color

func (Color) Ratio

func (c Color) Ratio() float64

Ratio the color ratio in a palette

func (*Color) SetRatio

func (c *Color) SetRatio(ratio float64)

SetRatio update the color ratio in a palette

func (Color) SpaceValues

func (c Color) SpaceValues(space ColorSpace) (float64, float64, float64)

SpaceValues returns float64 values for a specific color space of color

type ColorMeta added in v1.0.1

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

func (ColorMeta) AlternativeName added in v1.0.1

func (c ColorMeta) AlternativeName() string

AlternativeName returns the alternative name of color

func (ColorMeta) BrandName added in v1.0.1

func (c ColorMeta) BrandName() string

BrandName returns the brand name of color

func (ColorMeta) Name added in v1.0.1

func (c ColorMeta) Name() string

Name returns the name of color

func (ColorMeta) SerialNo added in v1.0.1

func (c ColorMeta) SerialNo() string

SerialNo returns the serial number of color

func (*ColorMeta) SetAlternativeName added in v1.0.1

func (c *ColorMeta) SetAlternativeName(name string)

SetAlternativeName update the color alternative name

func (*ColorMeta) SetBrandName added in v1.0.1

func (c *ColorMeta) SetBrandName(name string)

SetBrandName update the color brand name

func (*ColorMeta) SetFormatter added in v1.0.1

func (c *ColorMeta) SetFormatter(fn func() string)

SetFormatter set String formatter for color meta

func (*ColorMeta) SetName added in v1.0.1

func (c *ColorMeta) SetName(name string)

SetName update the color name

func (*ColorMeta) SetSerialNo added in v1.0.1

func (c *ColorMeta) SetSerialNo(v string)

SetSerialNo update the color serial number

func (ColorMeta) String added in v1.0.1

func (c ColorMeta) String() string

type ColorSpace

type ColorSpace int
const (
	RGB ColorSpace = iota
	// LAB CIE-L*a*b*: A perceptually uniform color space, i.e. distances are meaningful. L* in [0..1] and a*, b* almost in [-1..1].
	LAB
	// HSL Hue in [0..360], Saturation and Luminance in [0..1]. For legacy reasons; please forget that it exists.
	HSL
	// HSV Hue in [0..360], Saturation and Value in [0..1]. You're better off using HCL
	HSV
	// LUV CIE-L*u*v*: Very similar to CIE-L*a*b*, there is no consensus on which one is "better".
	LUV
	// LCH CIE-L*C*h° (HCL): This is generally the most useful one; CIE-L*a*b* space in polar coordinates, i.e. a better HSV. H° is in [0..360], C* almost in [0..1] and L* as in CIE-L*a*b*.
	LCH
	// HSLuv: The better alternative to HSL, see here and here. Hue in [0..360], Saturation and Luminance in [0..1].
	HSLuv
	// HPLuv: A variant of HSLuv. The color space is smoother, but only pastel colors can be included. Because the valid colors are limited, it's easy to get invalid Saturation values way above 1.0, indicating the color can't be represented in HPLuv because it's not pastel.
	HPLuv
	// HCL
	HCL
)

type Palette

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

Palette is named color palette

func NewPalette

func NewPalette(colors ...color.Color) *Palette

NewPalette returns a new palette instance

func (*Palette) AddColors

func (p *Palette) AddColors(colors ...Color) *Palette

AddColors append colors to the palette

func (Palette) Colors

func (p Palette) Colors() []Color

Colors returns a list of colors in palette

func (Palette) Dense

func (p Palette) Dense(colorSpace ColorSpace) *mat.Dense

Dense returns a mat dense of palette

func (Palette) Len added in v1.0.1

func (p Palette) Len() int

Len returns the length of colors in palette

func (Palette) Name

func (p Palette) Name() string

Name returns the name of palette

func (*Palette) SetName

func (p *Palette) SetName(name string)

SetName set palette name

func (Palette) SetRatio

func (p Palette) SetRatio(ratio float64, idx int)

SetRatio update the color ratio at index(idx) in the palette

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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