-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolor.go
More file actions
107 lines (101 loc) · 2.07 KB
/
color.go
File metadata and controls
107 lines (101 loc) · 2.07 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"fmt"
"image/color"
"math"
"time"
)
var tickIntervals = []time.Duration{
time.Nanosecond,
5 * time.Nanosecond,
10 * time.Nanosecond,
50 * time.Nanosecond,
100 * time.Nanosecond,
500 * time.Nanosecond,
time.Microsecond,
5 * time.Microsecond,
10 * time.Microsecond,
50 * time.Microsecond,
100 * time.Microsecond,
500 * time.Microsecond,
time.Millisecond,
5 * time.Millisecond,
10 * time.Millisecond,
50 * time.Millisecond,
100 * time.Millisecond,
500 * time.Millisecond,
time.Second,
5 * time.Second,
10 * time.Second,
30 * time.Second,
time.Minute,
5 * time.Minute,
10 * time.Minute,
}
func brighten(c color.NRGBA) color.NRGBA {
return color.NRGBA{
R: byte(min(int(c.R)+40, 255)),
G: byte(min(int(c.G)+40, 255)),
B: byte(min(int(c.B)+40, 255)),
A: c.A,
}
}
func spanColor(spanID, traceID int64) color.NRGBA {
p := spanID ^ traceID
hue := float64(uint16(p)) / 0xFFFF * 360.0
return hslColor(hue, 0.4, 0.3)
}
func hslColor(h, s, l float64) color.NRGBA {
c := (1 - math.Abs(2*l-1)) * s
h2 := h / 60.0
x := c * (1 - math.Abs(math.Mod(h2, 2)-1))
var r, g, b float64
switch {
case h2 < 1:
r, g, b = c, x, 0
case h2 < 2:
r, g, b = x, c, 0
case h2 < 3:
r, g, b = 0, c, x
case h2 < 4:
r, g, b = 0, x, c
case h2 < 5:
r, g, b = x, 0, c
default:
r, g, b = c, 0, x
}
m := l - c/2
return color.NRGBA{
R: byte((r + m) * 255),
G: byte((g + m) * 255),
B: byte((b + m) * 255),
A: 0xFF,
}
}
func formatDuration(d time.Duration) string {
if d == 0 {
return "0"
}
if d < time.Microsecond {
return fmt.Sprintf("%dns", d.Nanoseconds())
}
if d < time.Millisecond {
us := float64(d.Nanoseconds()) / 1e3
if us == float64(int64(us)) {
return fmt.Sprintf("%d\u00B5s", int64(us))
}
return fmt.Sprintf("%.1f\u00B5s", us)
}
if d < time.Second {
ms := float64(d.Nanoseconds()) / 1e6
if ms == float64(int64(ms)) {
return fmt.Sprintf("%dms", int64(ms))
}
return fmt.Sprintf("%.1fms", ms)
}
s := d.Seconds()
if s == float64(int64(s)) {
return fmt.Sprintf("%ds", int64(s))
}
return fmt.Sprintf("%.1fs", s)
}