colorgrad

package module
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2022 License: Apache-2.0, MIT Imports: 9 Imported by: 33

README

colorgrad 🎨

Release PkgGoDev Build Status Build Status go report codecov

Go (Golang) color scales library for data visualization, charts, games, maps, generative art and others.

Support This Project

Donate

Index

import "github.com/mazznoer/colorgrad"

Custom Gradient

Basic
grad, err := colorgrad.NewGradient().Build()

img

Custom Colors

Colors() method accept anything that implement color.Color interface.

import "image/color"
import "github.com/lucasb-eyer/go-colorful"

grad, err := colorgrad.NewGradient().
    Colors(
        color.RGBA{0, 206, 209, 255},
        color.RGBA{255, 105, 180, 255},
        colorful.Color{R: 0.274, G: 0.5, B: 0.7},
        colorful.Hsv(50, 1, 1),
        colorful.Hsv(348, 0.9, 0.8),
    ).
    Build()

img

Using Web Color Format

HtmlColors() method accepts named colors, hexadecimal (#rgb, #rgba, #rrggbb, #rrggbbaa), rgb(), rgba(), hsl(), hsla(), hwb(), and hsv().

grad, err := colorgrad.NewGradient().
    HtmlColors("#C41189", "#00BFFF", "#FFD700").
    Build()

img

grad, err := colorgrad.NewGradient().
    HtmlColors("gold", "hotpink", "darkturquoise").
    Build()

img

grad, err := colorgrad.NewGradient().
    HtmlColors(
        "rgb(125,110,221)",
        "rgb(90%,45%,97%)",
        "hsl(229,79%,85%)",
    ).
    Build()

img

Domain & Color Position

Default domain is [0..1].

grad, err := colorgrad.NewGradient().
    HtmlColors("deeppink", "gold", "seagreen").
    Build()

img

Set the domain to [0..100].

grad, err := colorgrad.NewGradient().
    HtmlColors("deeppink", "gold", "seagreen").
    Domain(0, 100).
    Build()

img

Set the domain to [-1..1].

grad, err := colorgrad.NewGradient().
    HtmlColors("deeppink", "gold", "seagreen").
    Domain(-1, 1).
    Build()

img

Set exact position for each color. The domain is [0..1].

grad, err := colorgrad.NewGradient().
    HtmlColors("deeppink", "gold", "seagreen").
    Domain(0, 0.7, 1).
    Build()

img

Set exact position for each color. The domain is [15..80].

grad, err := colorgrad.NewGradient().
    HtmlColors("deeppink", "gold", "seagreen").
    Domain(15, 30, 80).
    Build()

img

Blending Mode
grad, err := colorgrad.NewGradient().
    HtmlColors("#FFF", "#00F").
    Mode(colorgrad.BlendRgb).
    Build()

blend-modes

Interpolation Mode
grad, err := colorgrad.NewGradient().
    HtmlColors("#C41189", "#00BFFF", "#FFD700").
    Interpolation(colorgrad.InterpolationLinear).
    Build()

InterpolationLinear interpolation-linear

InterpolationCatmullRom interpolation-catmull-rom

InterpolationBasis interpolation-basis

Preset Gradients

All preset gradients are in the domain [0..1].

Diverging

colorgrad.BrBG() img

colorgrad.PRGn() img

colorgrad.PiYG() img

colorgrad.PuOr() img

colorgrad.RdBu() img

colorgrad.RdGy() img

colorgrad.RdYlBu() img

colorgrad.RdYlGn() img

colorgrad.Spectral() img

Sequential (Single Hue)

colorgrad.Blues() img

colorgrad.Greens() img

colorgrad.Greys() img

colorgrad.Oranges() img

colorgrad.Purples() img

colorgrad.Reds() img

Sequential (Multi-Hue)

colorgrad.Turbo() img

colorgrad.Viridis() img

colorgrad.Inferno() img

colorgrad.Magma() img

colorgrad.Plasma() img

colorgrad.Cividis() img

colorgrad.Warm() img

colorgrad.Cool() img

colorgrad.CubehelixDefault() img

colorgrad.BuGn() img

colorgrad.BuPu() img

colorgrad.GnBu() img

colorgrad.OrRd() img

colorgrad.PuBuGn() img

colorgrad.PuBu() img

colorgrad.PuRd() img

colorgrad.RdPu() img

colorgrad.YlGnBu() img

colorgrad.YlGn() img

colorgrad.YlOrBr() img

colorgrad.YlOrRd() img

Cyclical

colorgrad.Rainbow() img

colorgrad.Sinebow() img

Parsing GIMP Gradient

import "os"
import "github.com/lucasb-eyer/go-colorful"

foreground := colorful.Color{R: 0, G: 0, B: 0}
background := colorful.Color{R: 1, G: 1, B: 1}
file, err := os.Open("Abstract_1.ggr")

if err != nil {
	panic(err)
}

defer file.Close()
grad, name, err2 := colorgrad.ParseGgr(file, foreground, background)
fmt.Println(name) // Abstract 1

gimp-gradient

Using the Gradient

Get the domain
grad := colorgrad.Rainbow()

fmt.Println(grad.Domain()) // 0 1
Get single color at certain position
grad := colorgrad.Rainbow()

fmt.Println(grad.At(0.0).Hex()) // #6e40aa
fmt.Println(grad.At(0.5).Hex()) // #aff05b
fmt.Println(grad.At(1.0).Hex()) // #6e40aa
Get n colors evenly spaced across gradient

.Colors() will return []color.Color.

grad := colorgrad.Rainbow()

for _, c := range grad.Colors(7) {
    fmt.Println(c)
}

Output:

{0.43021004989867906 0.25023866753368473 0.6663314402976028}
{0.9324699655386229 0.2641323667713124 0.5856493589115485}
{1 0.5479694546462268 0.220207078599215}
{0.68717707266999 0.9403236324800919 0.355050281779196}
{0.36562500000000014 0.9182994327976323 0.5525738542381139}
{0.25 0.589697182728592 0.877839930901789}
{0.4302100498986791 0.2502386675336847 0.6663314402976028}

.ColorfulColors() will return []colorful.Color.

grad := colorgrad.Rainbow()

for _, c := range grad.ColorfulColors(10) {
    fmt.Println(c.Hex())
}

Output:

#6e40aa
#c83dac
#ff5375
#ff8c38
#c9d33a
#7cf659
#5dea8d
#48b8d0
#4775de
#6e40aa
Hard-Edged Gradient

Convert gradient to hard-edged gradient with 11 segments and 0 smoothness.

grad := colorgrad.Rainbow().Sharp(11, 0)

img

This is the effect of different smoothness.

img

Color Schemes

import "github.com/mazznoer/colorgrad/scheme"

scheme.Category10

img

scheme.Accent

img

scheme.Dark2

img

scheme.Paired

img

scheme.Pastel1

img

scheme.Pastel2

img

scheme.Set1

img

scheme.Set2

img

scheme.Set3

img

scheme.Tableau10

img

Examples

Gradient Image
package main

import (
    "image"
    "image/png"
    "os"

    "github.com/mazznoer/colorgrad"
)

func main() {
    grad, _ := colorgrad.NewGradient().
        HtmlColors("#C41189", "#00BFFF", "#FFD700").
        Build()

    w := 1500
    h := 70
    fw := float64(w)

    img := image.NewRGBA(image.Rect(0, 0, w, h))

    for x := 0; x < w; x++ {
        col := grad.At(float64(x) / fw)
        for y := 0; y < h; y++ {
            img.Set(x, y, col)
        }
    }

    file, err := os.Create("gradient.png")
    if err != nil {
        panic(err.Error())
    }
    defer file.Close()
    png.Encode(file, img)
}

Example output:

img

Colored Noise
package main

import (
    "image"
    "image/png"
    "os"

    "github.com/mazznoer/colorgrad"
    "github.com/ojrac/opensimplex-go"
)

func main() {
    w := 600
    h := 350
    scale := 0.02

    grad := colorgrad.Rainbow().Sharp(7, 0.2)
    noise := opensimplex.NewNormalized(996)
    img := image.NewRGBA(image.Rect(0, 0, w, h))

    for y := 0; y < h; y++ {
        for x := 0; x < w; x++ {
            t := noise.Eval2(float64(x)*scale, float64(y)*scale)
            img.Set(x, y, grad.At(t))
        }
    }

    file, err := os.Create("noise.png")
    if err != nil {
        panic(err.Error())
    }
    defer file.Close()
    png.Encode(file, img)
}

Example output:

noise

Playground

Dependencies

Inspirations

Documentation

Overview

Example (CustomGradient)
package main

import (
	"fmt"

	"github.com/mazznoer/colorgrad"
)

func main() {
	grad, err := colorgrad.NewGradient().
		HtmlColors("red", "#FFD700", "lime").
		Domain(0, 0.35, 1).
		Mode(colorgrad.BlendOklab).
		Build()

	if err != nil {
		panic(err)
	}

	fmt.Println(grad.At(0).Hex())
	fmt.Println(grad.At(1).Hex())
}
Output:

#ff0000
#00ff00
Example (PresetGradient)
package main

import (
	"fmt"

	"github.com/mazznoer/colorgrad"
)

func main() {
	grad := colorgrad.Rainbow()
	dmin, dmax := grad.Domain()

	fmt.Println(dmin, dmax)
	fmt.Println(grad.At(0).Hex())
}
Output:

0 1
#6e40aa

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BlendMode

type BlendMode int
const (
	BlendHcl BlendMode = iota
	BlendHsv
	BlendLab
	BlendLinearRgb
	BlendLuv
	BlendRgb
	BlendOklab
)

func (BlendMode) String added in v0.9.1

func (b BlendMode) String() string

type Gradient

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

func Blues

func Blues() Gradient

func BrBG added in v0.3.0

func BrBG() Gradient

func BuGn added in v0.7.0

func BuGn() Gradient

func BuPu added in v0.7.0

func BuPu() Gradient

func Cividis

func Cividis() Gradient

func Cool

func Cool() Gradient

func CubehelixDefault added in v0.2.0

func CubehelixDefault() Gradient

func GnBu added in v0.7.0

func GnBu() Gradient

func Greens

func Greens() Gradient

func Greys

func Greys() Gradient

func Inferno

func Inferno() Gradient

func Magma

func Magma() Gradient

func OrRd added in v0.7.0

func OrRd() Gradient

func Oranges

func Oranges() Gradient

func PRGn added in v0.3.0

func PRGn() Gradient

func ParseGgr added in v0.9.0

func ParseGgr(r io.Reader, fg, bg colorful.Color) (Gradient, string, error)

func PiYG added in v0.3.0

func PiYG() Gradient

func Plasma

func Plasma() Gradient

func PuBu added in v0.7.0

func PuBu() Gradient

func PuBuGn added in v0.7.0

func PuBuGn() Gradient

func PuOr added in v0.3.0

func PuOr() Gradient

func PuRd added in v0.7.0

func PuRd() Gradient

func Purples

func Purples() Gradient

func Rainbow

func Rainbow() Gradient

func RdBu added in v0.3.0

func RdBu() Gradient

func RdGy added in v0.3.0

func RdGy() Gradient

func RdPu added in v0.7.0

func RdPu() Gradient

func RdYlBu added in v0.3.0

func RdYlBu() Gradient

func RdYlGn added in v0.3.0

func RdYlGn() Gradient

func Reds

func Reds() Gradient

func Sinebow

func Sinebow() Gradient

func Spectral

func Spectral() Gradient

func Turbo

func Turbo() Gradient

func Viridis

func Viridis() Gradient

func Warm

func Warm() Gradient

func YlGn added in v0.7.0

func YlGn() Gradient

func YlGnBu added in v0.7.0

func YlGnBu() Gradient

func YlOrBr added in v0.7.0

func YlOrBr() Gradient

func YlOrRd added in v0.7.0

func YlOrRd() Gradient

func (Gradient) At

func (g Gradient) At(t float64) colorful.Color

Get color at certain position

func (Gradient) ColorfulColors added in v0.5.0

func (g Gradient) ColorfulColors(count uint) []colorful.Color

Get n colors evenly spaced across gradient

func (Gradient) Colors

func (g Gradient) Colors(count uint) []color.Color

Get n colors evenly spaced across gradient

func (Gradient) Domain added in v0.5.0

func (g Gradient) Domain() (float64, float64)

Get the gradient domain min and max

func (Gradient) ReflectAt added in v0.9.0

func (g Gradient) ReflectAt(t float64) colorful.Color

Get color at certain position

func (Gradient) RepeatAt added in v0.9.0

func (g Gradient) RepeatAt(t float64) colorful.Color

Get color at certain position

func (Gradient) Sharp added in v0.5.0

func (g Gradient) Sharp(segment uint, smoothness float64) Gradient

Return a new hard-edge gradient

type GradientBuilder

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

func NewGradient

func NewGradient() *GradientBuilder

func (*GradientBuilder) Build

func (gb *GradientBuilder) Build() (Gradient, error)

func (*GradientBuilder) Colors

func (gb *GradientBuilder) Colors(colors ...color.Color) *GradientBuilder

func (*GradientBuilder) Domain

func (gb *GradientBuilder) Domain(domain ...float64) *GradientBuilder

func (*GradientBuilder) HtmlColors added in v0.4.0

func (gb *GradientBuilder) HtmlColors(htmlColors ...string) *GradientBuilder

func (*GradientBuilder) Interpolation added in v0.9.0

func (gb *GradientBuilder) Interpolation(mode Interpolation) *GradientBuilder

func (*GradientBuilder) Mode

func (gb *GradientBuilder) Mode(mode BlendMode) *GradientBuilder

type Interpolation added in v0.9.0

type Interpolation int
const (
	InterpolationLinear Interpolation = iota
	InterpolationCatmullRom
	InterpolationBasis
)

func (Interpolation) String added in v0.9.1

func (i Interpolation) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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