-
Notifications
You must be signed in to change notification settings - Fork 18
/
color_256.go
39 lines (31 loc) · 849 Bytes
/
color_256.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
package lol
import (
"fmt"
"math"
)
type color256 struct {
R, G, B uint8
}
func (c *color256) rainbow(freq float64, i float64) {
c.R = uint8(math.Floor(math.Sin(freq*i+0)*127)) + 128
c.G = uint8(math.Floor(math.Sin(freq*i+2.0*math.Pi/3.0)*127)) + 128
c.B = uint8(math.Floor(math.Sin(freq*i+4.0*math.Pi/3.0)*127)) + 128
}
func (c *color256) format() []byte {
// Support for grayscale colors
if c.R == c.G && c.G == c.B && int(c.R) > 232 {
return []byte(fmt.Sprintf("\x1b[38;5;%dm", int(c.R)))
}
// Math for 256: We use the closest value possible
r6 := (uint16(c.R) * 3) / 255
g6 := (uint16(c.G) * 3) / 255
b6 := (uint16(c.B) * 3) / 255
i := 36*r6 + 6*g6 + b6
return []byte(fmt.Sprintf("\x1b[38;5;%dm", i))
}
func (c *color256) reset() []byte {
return []byte("\x1b[0m")
}
func New256Colorer() colorer {
return &color256{}
}