Debian Codegraph @

renderer/rgb.go

raw
1 package renderer
2
3 import (
4 "fmt"
5 "strconv"
6 "strings"
7
8 "github.com/fatih/color"
9 )
10
11 // SGR parameters used to build 24-bit ("true color") sequences.
12 //
13 // fatih/color keeps the equivalent prefixes unexported, so tablewriter defines
14 // them here. This lets RGB colors be emitted through the already vendored
15 // fatih/color version, without requiring a newer release of that dependency.
16 const (
17 fgTrueColor color.Attribute = 38 // select foreground color
18 bgTrueColor color.Attribute = 48 // select background color
19 trueColorMode color.Attribute = 2 // "2" selects the 24-bit RGB sub-mode
20 )
21
22 // clampRGBComponent constrains a single color channel to the valid 0-255 range
23 // so out-of-range values produce a usable color instead of a malformed escape
24 // sequence.
25 func clampRGBComponent(v int) color.Attribute {
26 switch {
27 case v < 0:
28 return 0
29 case v > 255:
30 return 255
31 default:
32 return color.Attribute(v)
33 }
34 }
35
36 // RGB returns foreground Colors for a 24-bit ("true color") value. Each of r, g
37 // and b is a channel in the range 0-255; values outside that range are clamped.
38 //
39 // The result is a plain Colors slice, so it can be used anywhere Colors are
40 // accepted and combined with regular attributes via append, for example:
41 //
42 // Tint{FG: append(renderer.RGB(255, 128, 0), color.Bold)}
43 //
44 // Terminals that do not support 24-bit color may ignore or approximate the
45 // sequence.
46 func RGB(r, g, b int) Colors {
47 return Colors{fgTrueColor, trueColorMode, clampRGBComponent(r), clampRGBComponent(g), clampRGBComponent(b)}
48 }
49
50 // BgRGB returns background Colors for a 24-bit ("true color") value. It behaves
51 // like RGB but sets the background rather than the foreground.
52 func BgRGB(r, g, b int) Colors {
53 return Colors{bgTrueColor, trueColorMode, clampRGBComponent(r), clampRGBComponent(g), clampRGBComponent(b)}
54 }
55
56 // Hex parses a hexadecimal color string and returns foreground Colors for the
57 // equivalent 24-bit color. Both the shorthand "#RGB" and full "#RRGGBB" forms
58 // are accepted, with or without the leading '#'. An error is returned for
59 // strings that are not a valid hex color.
60 func Hex(s string) (Colors, error) {
61 r, g, b, err := parseHexColor(s)
62 if err != nil {
63 return nil, err
64 }
65 return RGB(r, g, b), nil
66 }
67
68 // BgHex behaves like Hex but returns background Colors.
69 func BgHex(s string) (Colors, error) {
70 r, g, b, err := parseHexColor(s)
71 if err != nil {
72 return nil, err
73 }
74 return BgRGB(r, g, b), nil
75 }
76
77 // parseHexColor decodes a "#RGB" or "#RRGGBB" color (the leading '#' is
78 // optional) into its red, green and blue components.
79 func parseHexColor(s string) (r, g, b int, err error) {
80 h := strings.TrimPrefix(strings.TrimSpace(s), "#")
81
82 switch len(h) {
83 case 3:
84 // Expand shorthand: "abc" -> "aabbcc".
85 h = string([]byte{h[0], h[0], h[1], h[1], h[2], h[2]})
86 case 6:
87 // Already full length.
88 default:
89 return 0, 0, 0, fmt.Errorf("tablewriter: invalid hex color %q: want \"#RGB\" or \"#RRGGBB\"", s)
90 }
91
92 v, err := strconv.ParseUint(h, 16, 32)
93 if err != nil {
94 return 0, 0, 0, fmt.Errorf("tablewriter: invalid hex color %q: %w", s, err)
95 }
96
97 return int(v >> 16 & 0xFF), int(v >> 8 & 0xFF), int(v & 0xFF), nil
98 }