1package ansi
2
3type outputColorKind uint8
4
5const (
6 outputColorDefault outputColorKind = iota
7 outputColorANSI16
8 outputColorANSI256
9 outputColorRGB
10)
11
12type outputColor struct {
13 kind outputColorKind
14 index int
15 rgb RGB
16}
17
18type outputStyle struct {
19 bold bool
20 italic bool
21 inverse bool
22 attrs StyleAttrs
23 underlineStyle UnderlineStyle
24 foreground outputColor
25 background outputColor
26 underlineColor outputColor
27}
28
29type styleProjector struct {
30 profile ColorProfile
31}
32
33func (p styleProjector) project(style Style) outputStyle {
34 return outputStyle{
35 bold: style.Bold,
36 italic: style.Italic,
37 inverse: style.Inverse,
38 attrs: style.Attrs,
39 underlineStyle: style.UnderlineStyle,
40 foreground: p.projectColor(style.Foreground, style.Foreground >= 0, style.ForegroundRGB, style.HasForegroundRGB, true),
41 background: p.projectColor(style.Background, style.Background >= 0, style.BackgroundRGB, style.HasBackgroundRGB, true),
42 underlineColor: p.projectColor(style.UnderlineColor, style.HasUnderlineColor, style.UnderlineColorRGB, style.HasUnderlineColorRGB, false),
43 }
44}
45
46func (p styleProjector) projectColor(index int, hasIndex bool, rgb RGB, hasRGB, basicColor bool) outputColor {
47 switch p.profile {
48 case ColorProfileANSI256:
49 if hasRGB {
50 return outputColor{kind: outputColorANSI256, index: rgbToANSI256(rgb)}
51 }
52 if hasIndex && index >= 0 && index <= 255 {
53 return outputColor{kind: outputColorANSI256, index: index}
54 }
55 case ColorProfileANSI16:
56 if !basicColor {
57 return outputColor{}
58 }
59 if hasRGB {
60 return outputColor{kind: outputColorANSI16, index: rgbToANSI16(rgb)}
61 }
62 if hasIndex {
63 if index >= 0 && index < 16 {
64 return outputColor{kind: outputColorANSI16, index: index}
65 }
66 if resolved, ok := ansi256ToRGB(index); ok {
67 return outputColor{kind: outputColorANSI16, index: rgbToANSI16(resolved)}
68 }
69 }
70 case ColorProfileMonochrome:
71 return outputColor{}
72 default:
73 if hasRGB {
74 return outputColor{kind: outputColorRGB, rgb: rgb}
75 }
76 if hasIndex {
77 return outputColor{kind: outputColorANSI256, index: index}
78 }
79 }
80 return outputColor{}
81}