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
|
package colors
import (
"fmt"
"math"
"strings"
)
const esc = "\\X1B"
type ColorSpace interface {
ToPrecise() PreciseColor
FromPrecise(PreciseColor) ColorSpace
}
func (c PreciseColor) String() string {
return fmt.Sprintf("PC(%.4f, %.4f, %.4f)", c.R, c.G, c.B)
}
// PreciseColor is a color with floating point values for red, green, and blue.
// The extra precision minimizes rounding errors when converting between different
// color spaces. It is used as an intermediate representation when converting between
// different color spaces.
type PreciseColor struct {
R, G, B float64
}
func (c PreciseColor) ToPrecise() PreciseColor {
return c
}
func (c PreciseColor) FromPrecise(p PreciseColor) ColorSpace {
return p
}
func Hex(cs ColorSpace) string {
p := cs.ToPrecise()
return strings.ToUpper(fmt.Sprintf("#%02x%02x%02x",
int(math.Round(p.R*255)),
int(math.Round(p.G*255)),
int(math.Round(p.B*255)),
))
}
func EscapedSeq(cs ColorSpace, fg bool) string {
p := cs.ToPrecise()
mod := 38 // fg by default
if !fg {
mod += 10
}
r := int(math.Round(p.R * 255))
g := int(math.Round(p.G * 255))
b := int(math.Round(p.B * 255))
return fmt.Sprintf("%s[%d;2;%d;%d;%dm",
esc, mod, r, g, b,
)
}
|