xmpp: mellium.im/xmpp/color Index | Examples | Files

package color

import "mellium.im/xmpp/color"

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

Code:

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.ZP, 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)
    }
}

Index

Examples

Package Files

color.go

Constants

const (
    None uint8 = iota
    RedGreen
    Blue
)

A list of color vision deficiencies.

const Size = 2

Size is the length of the hash output.

func Bytes Uses

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

Bytes converts a byte slice to a color.YCbCr.

For more information see Sum.

func Hash Uses

func Hash(cvd uint8) hash.Hash

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

func String Uses

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

String converts a string to a color.YCbCr.

For more information see Sum.

func Sum Uses

func Sum(data []byte, cvd uint8) [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.

Package color imports 5 packages (graph) and is imported by 1 packages. Updated 2019-03-30. Refresh now. Tools for package owners.