gradient

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2023 License: MIT Imports: 6 Imported by: 2

README

gradient

Go

A gradient that can generate slices of color.RGBA of arbitrary size.

The sample program below parses two colors from hex strings and uses them to generate a gradient of nine colors.

package main

import (
	"fmt"

	"github.com/ebeeton/gradient"
)

func main() {
	// Create two colors from hex strings of the form #RRGGBBAA.
	opaqueGreen, err := gradient.ColorFromHex("#00FF00FF")
	if err != nil {
		panic(err)
	}
	transparentMagenta, err := gradient.ColorFromHex("#FF00FF00")
	if err != nil {
		panic(err)
	}

	// Create a slice of stops with the colors at either "end".
	stops := []gradient.Stop{
		{Color: opaqueGreen, Position: 0.0},
		{Color: transparentMagenta, Position: 1.0},
	}

	// Get a gradient of nine colors and print them in hex. The second parameter
	// for ColorToHex includes the alpha channel value in the output.
	g := gradient.GetGradient(stops, 9)
	for _, c := range g {
		fmt.Println(gradient.ColorToHex(c, true))
	}
}

Output:

#00ff00ff
#1fdf1fdf
#3fbf3fbf
#5f9f5f9f
#7f7f7f7f
#9f5f9f5f
#bf3fbf3f
#df1fdf1f
#ff00ff00

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ColorFromHex

func ColorFromHex(h string) (color.RGBA, error)

ColorFromHex converts a hex color string of the form #RRGGBB or #RRGGBBAA to a color.RGBA. If the former is provided alpha defaults to 255.

func ColorToHex added in v0.0.1

func ColorToHex(c color.RGBA, alpha bool) string

ColorToHex converts a color.RGBA to a hex color string of the form #RRGGBB or #RRGGBBAA depending on the alpha parameter.

func GetGradient

func GetGradient(stops []Stop, count int) []color.RGBA

GetGradient generates a gradient of count number of colors from a slice of Stops. The resulting slice of color.RGBA can be used as a palette.

Types

type Stop

type Stop struct {
	Color    color.RGBA
	Position float64
}

Stop is a hexadecimal color value and its position in a linear gradient from 0 to 1.

Jump to

Keyboard shortcuts

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