color

package
v0.21.2 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2022 License: BSD-2-Clause Imports: 6 Imported by: 1

Documentation

Overview

Package color implements XEP-0392: Consistent Color Generation v0.4.

Example
package main

import (
	"image"
	"image/color"
	"image/draw"
	"image/png"
	"os"

	"golang.org/x/image/font"
	"golang.org/x/image/font/inconsolata"
	"golang.org/x/image/math/fixed"

	colorgen "mellium.im/xmpp/color"
)

const (
	lum    = 128
	cvd    = colorgen.None
	factor = 0.4
	inv    = 1 - factor
)

// naively mix a foreground color with a background color ignoring the alpha
// channel.
func mix(fg color.Color, bg color.Color) color.Color {
	rb, gb, bb, _ := bg.RGBA()
	rf, gf, bf, _ := fg.RGBA()

	const maxu16 = 1<<16 - 1
	return color.RGBA{
		R: uint8(factor*float32(maxu16-rb) + inv*float32(rf)),
		G: uint8(factor*float32(maxu16-gb) + inv*float32(gf)),
		B: uint8(factor*float32(maxu16-bb) + inv*float32(bf)),
		A: 255,
	}
}

func main() {
	strs := []string{
		"Beautiful",
		"Catchup",
		"Dandelion",
		"Fuego Borrego",
		"Green Giant",
		"Mailman",
		"Papa Shrimp",
		"Pockets",
		"Spoon Foot",
		"Sunshine",
		"Thespian",
		"Twinkle Toes",
		"Zodiac",
	}

	img := image.NewRGBA(image.Rect(0, 0, 300, 216))
	parts := []color.Color{color.Black, color.White}

	for x, bg := range parts {
		bounds := img.Bounds()
		w := bounds.Max.X / len(parts)
		bounds.Min.X = w * x
		bounds.Max.X = w * (x + 1)
		draw.Draw(img, bounds, &image.Uniform{bg}, image.Point{}, draw.Src)

		for y, s := range strs {
			d := &font.Drawer{
				Dst: img,
				Src: image.NewUniform(mix(
					colorgen.String(s, lum, cvd),
					bg,
				)),
				Face: inconsolata.Regular8x16,
				Dot: fixed.Point26_6{
					X: fixed.Int26_6((12 + bounds.Min.X) * 64),
					Y: fixed.Int26_6(16 * (y + 1) * 64),
				},
			}

			d.DrawString(s)
		}
	}

	f, err := os.Create("gonicks.png")
	if err != nil {
		panic(err)
	}
	defer f.Close()
	if err := png.Encode(f, img); err != nil {
		panic(err)
	}
}
Output:

Index

Examples

Constants

View Source
const Size = 2

Size is the length of the hash output.

Variables

This section is empty.

Functions

func Bytes

func Bytes(b []byte, luma uint8, cvd CVD) color.YCbCr

Bytes converts a byte slice to a color.YCbCr.

For more information see Sum.

func Hash

func Hash(cvd CVD) hash.Hash

Hash returns a new hash.Hash computing the Y'CbCr color. For more information see Sum.

func String

func String(s string, luma uint8, cvd CVD) color.YCbCr

String converts a string to a color.YCbCr.

For more information see Sum.

func Sum

func Sum(data []byte, cvd CVD) [Size]byte

Sum returns a color in the Y'CbCr colorspace in the form [Cb, Cr] that is consistent for the same inputs.

If a color vision deficiency constant is provided (other than None), the algorithm attempts to avoid confusable colors.

Types

type CVD added in v0.19.0

type CVD uint8

CVD represents a color vision deficiency to correct for.

const (
	None CVD = iota
	RedGreen
	Blue
)

A list of color vision deficiencies.

func (CVD) String added in v0.19.0

func (i CVD) String() string

Jump to

Keyboard shortcuts

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