项目作者: mazznoer

项目描述 :
Go (Golang) color scales library for maps, charts, data-visualization & creative coding.
高级语言: Go
项目地址: git://github.com/mazznoer/colorgrad.git
创建时间: 2020-07-24T15:07:19Z
项目社区:https://github.com/mazznoer/colorgrad

开源协议:Other

下载


colorgrad

Release
PkgGoDev
Build Status
go report

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

Support This Project

Donate

Index

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

Custom Gradient

Basic

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

img

Custom Colors

  1. grad, err := colorgrad.NewGradient().
  2. Colors(
  3. colorgrad.Rgb8(0, 206, 209, 255),
  4. colorgrad.Rgb8(255, 105, 180, 255),
  5. colorgrad.Rgb(0.274, 0.5, 0.7, 1),
  6. colorgrad.Hsv(50, 1, 1, 1),
  7. colorgrad.Hsv(348, 0.9, 0.8, 1),
  8. ).
  9. Build()

img

Using Web Color Format

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

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

img

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

img

  1. grad, err := colorgrad.NewGradient().
  2. HtmlColors(
  3. "rgb(125,110,221)",
  4. "rgb(90%,45%,97%)",
  5. "hsl(229,79%,85%)",
  6. ).
  7. Build()

img

Domain & Color Position

Default domain is [0..1].

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

img

Set the domain to [0..100].

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

img

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

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

img

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

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

img

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

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

img

Blending Mode

  1. grad, err := colorgrad.NewGradient().
  2. HtmlColors("#FFF", "#00F").
  3. Mode(colorgrad.BlendRgb).
  4. Build()

blend-modes

Interpolation Mode

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

InterpolationLinear
interpolation-linear

InterpolationCatmullRom
interpolation-catmull-rom

InterpolationBasis
interpolation-basis

Preset Gradients

See PRESET.md

Parsing GIMP Gradient

  1. import "os"
  2. foreground := colorgrad.Rgb(0, 0, 0, 1)
  3. background := colorgrad.Rgb(1, 1, 1, 1)
  4. file, err := os.Open("Abstract_1.ggr")
  5. if err != nil {
  6. panic(err)
  7. }
  8. defer file.Close()
  9. grad, name, err2 := colorgrad.ParseGgr(file, foreground, background)
  10. fmt.Println(name) // Abstract 1

gimp-gradient

Using the Gradient

Get the domain

  1. grad := colorgrad.Rainbow()
  2. fmt.Println(grad.Domain()) // 0 1

Get single color at certain position

  1. grad := colorgrad.Rainbow()
  2. fmt.Println(grad.At(0.0).HexString()) // #6e40aa
  3. fmt.Println(grad.At(0.5).HexString()) // #aff05b
  4. fmt.Println(grad.At(1.0).HexString()) // #6e40aa

Get n colors evenly spaced across gradient

  1. grad := colorgrad.Rainbow()
  2. for _, c := range grad.Colors(10) {
  3. fmt.Println(c.HexString())
  4. }

Output:

  1. #6e40aa
  2. #c83dac
  3. #ff5375
  4. #ff8c38
  5. #c9d33a
  6. #7cf659
  7. #5dea8d
  8. #48b8d0
  9. #4775de
  10. #6e40aa

Hard-Edged Gradient

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

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

img

This is the effect of different smoothness.

img

Examples

Gradient Image

  1. package main
  2. import (
  3. "image"
  4. "image/png"
  5. "os"
  6. "github.com/mazznoer/colorgrad"
  7. )
  8. func main() {
  9. grad, _ := colorgrad.NewGradient().
  10. HtmlColors("#C41189", "#00BFFF", "#FFD700").
  11. Build()
  12. w := 1500
  13. h := 70
  14. fw := float64(w)
  15. img := image.NewRGBA(image.Rect(0, 0, w, h))
  16. for x := 0; x < w; x++ {
  17. col := grad.At(float64(x) / fw)
  18. for y := 0; y < h; y++ {
  19. img.Set(x, y, col)
  20. }
  21. }
  22. file, err := os.Create("gradient.png")
  23. if err != nil {
  24. panic(err.Error())
  25. }
  26. defer file.Close()
  27. png.Encode(file, img)
  28. }

Example output:

img

Colored Noise

  1. package main
  2. import (
  3. "image"
  4. "image/png"
  5. "os"
  6. "github.com/mazznoer/colorgrad"
  7. "github.com/ojrac/opensimplex-go"
  8. )
  9. func main() {
  10. w := 600
  11. h := 350
  12. scale := 0.02
  13. grad := colorgrad.Rainbow().Sharp(7, 0.2)
  14. noise := opensimplex.NewNormalized(996)
  15. img := image.NewRGBA(image.Rect(0, 0, w, h))
  16. for y := 0; y < h; y++ {
  17. for x := 0; x < w; x++ {
  18. t := noise.Eval2(float64(x)*scale, float64(y)*scale)
  19. img.Set(x, y, grad.At(t))
  20. }
  21. }
  22. file, err := os.Create("noise.png")
  23. if err != nil {
  24. panic(err.Error())
  25. }
  26. defer file.Close()
  27. png.Encode(file, img)
  28. }

Example output:

noise

Playground

Dependencies

Inspirations