-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gradient.go
57 lines (44 loc) · 1.45 KB
/
gradient.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright (c) Liam Stanley <[email protected]>. All rights reserved. Use of
// this source code is governed by the MIT license that can be found in
// the LICENSE file.
package tint
import (
"image/color"
"github.com/charmbracelet/lipgloss"
"github.com/lucasb-eyer/go-colorful"
)
// FromTo converts a color from one color space to another colorful's BlendLuv --
// which blends two colors in the CIE-L*u*v* color-space. The result is a slice of
// lipgloss.TerminalColor (compatible with lipgloss methods).
func FromTo(from, to color.Color, steps int) (gradient []lipgloss.TerminalColor) {
gradient = make([]lipgloss.TerminalColor, steps)
for i, hex := range FromToHex(from, to, steps) {
gradient[i] = lipgloss.Color(hex)
}
return gradient
}
// FromToHex converts a color from one color space to another colorful's BlendLuv --
// which blends two colors in the CIE-L*u*v* color-space. The result is a slice of
// hex colors.
func FromToHex(from, to color.Color, steps int) (gradient []string) {
start, ok := colorful.MakeColor(from)
if !ok {
panic("invalid from color specified")
}
end, ok := colorful.MakeColor(to)
if !ok {
panic("invalid to color specified")
}
fsteps := float64(steps)
for i := 0.0; i < fsteps; i++ {
gradient = append(gradient, start.BlendLuv(end, i/fsteps).Hex())
}
return gradient
}
func Hex(c lipgloss.TerminalColor) string {
col, ok := colorful.MakeColor(c)
if !ok {
panic("invalid color specified")
}
return col.Hex()
}