-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.go
89 lines (74 loc) · 1.38 KB
/
color.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"image/color"
"math"
"strconv"
)
type Color struct {
R float64
G float64
B float64
}
func (c1 *Color) Times(c2 *Color) *Color {
return &Color{
c1.R * c2.R,
c1.G * c2.G,
c1.B * c2.B,
}
}
func (c *Color) Scale(s float64) *Color {
return &Color{
c.R * s,
c.G * s,
c.B * s,
}
}
func (c1 *Color) Add(c2 *Color) *Color {
return &Color{
c1.R + c2.R,
c1.G + c2.G,
c1.B + c2.B,
}
}
func (c1 *Color) Div(s float64) *Color {
return &Color{
c1.R / s,
c1.G / s,
c1.B / s,
}
}
func (c *Color) Clamp() *Color {
r := math.Min(255, math.Max(0, c.R))
g := math.Min(255, math.Max(0, c.G))
b := math.Min(255, math.Max(0, c.B))
return &Color{
r,
g,
b,
}
}
func (c *Color) ToRGBA() color.Color {
r := uint8(math.Min(c.R*255, 255))
g := uint8(math.Min(c.G*255, 255))
b := uint8(math.Min(c.B*255, 255))
return color.RGBA{
r,
g,
b,
255,
}
}
const shadeChars = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\|()1{}[]?-_+~<>i!lI;:,\"^`'. "
const maxBright = 3 * 255
func (c *Color) ToChar() string {
r := int(math.Min(c.R*255, 255))
g := int(math.Min(c.G*255, 255))
b := int(math.Min(c.B*255, 255))
shade := (1 - float64(r+g+b)/float64(maxBright)) * float64((len(shadeChars) - 1))
char := shadeChars[int(shade)]
return "\x1b[38;2;" +
strconv.Itoa(r) + ";" +
strconv.Itoa(g) + ";" +
strconv.Itoa(b) + "m" +
string(char) + "\x1b[0m"
}