1
+ /// The color mode, either foreground or background.
2
+ enum ColorMode { foreground, background }
3
+
4
+ /// Represents a color in the terminal.
5
+ /// This is a base class for all color types.
1
6
class Color {
7
+ /// The value of the color
2
8
final dynamic value;
3
9
4
- const Color (this .value);
10
+ /// The mode of the color, either foreground or background.
11
+ final ColorMode mode;
12
+
13
+ /// Creates a new color with the given value and mode.
14
+ const Color (this .value, this .mode);
15
+
16
+ /// Creates a new foreground color with the given value.
17
+ const Color .fg (this .value) : mode = ColorMode .foreground;
18
+
19
+ /// Creates a new background color with the given value.
20
+ const Color .bg (this .value) : mode = ColorMode .background;
5
21
6
- static const Color none = Color (0 );
22
+ /// This color should be ignored for rendering.
23
+ static const Color none = Color .fg (0 );
7
24
25
+ /// Returns true if the color is the default color.
26
+ bool get isNone => value == 0 ;
27
+
28
+ /// Returns true is the color is not the default color.
29
+ bool get isNotNone => ! isNone;
30
+
31
+ /// The formatted value of the color, used in ANSI escape codes.
8
32
String get formatted => value.toString ();
9
33
34
+ /// The string representation of the color mode.
35
+ String get modeString => mode.toString ().split ('.' ).last;
36
+
10
37
@override
11
- String toString () => 'Color($value )' ;
38
+ String toString () => isNone ? 'Color.none' : 'Color ($value , $ modeString )' ;
12
39
13
40
@override
14
41
operator == (Object other) {
@@ -22,35 +49,85 @@ class Color {
22
49
int get hashCode => value.hashCode;
23
50
}
24
51
52
+ /// Represents an RGB color in the terminal.
25
53
class RGBColor extends Color {
54
+ /// The red value of the color.
26
55
final int red;
56
+
57
+ /// The green value of the color.
27
58
final int green;
59
+
60
+ /// The blue value of the color.
28
61
final int blue;
29
62
30
- const RGBColor (this .red, this .green, this .blue) : super ('$red ;$green ;$blue ' );
63
+ /// Creates a new RGB color with the given red, green, and blue values.
64
+ const RGBColor (
65
+ this .red,
66
+ this .green,
67
+ this .blue, [
68
+ ColorMode mode = ColorMode .foreground,
69
+ ]) : super ('$red ;$green ;$blue ' , mode);
70
+
71
+ /// Creates a new RGB foreground color with the given red, green, and blue values.
72
+ const RGBColor .fg (
73
+ this .red,
74
+ this .green,
75
+ this .blue,
76
+ ) : super ('$red ;$green ;$blue ' , ColorMode .foreground);
77
+
78
+ /// Creates a new RGB background color with the given red, green, and blue values.
79
+ const RGBColor .bg (
80
+ this .red,
81
+ this .green,
82
+ this .blue,
83
+ ) : super ('$red ;$green ;$blue ' , ColorMode .background);
31
84
32
85
@override
33
- String get formatted => '$red ;$green ;$blue ' ;
86
+ String get formatted => '$formattedMode ;$ red ;$green ;$blue ' ;
34
87
35
- String get formattedForeground => '38;2;$formatted ' ;
36
- String get formattedBackground => '48;2;$formatted ' ;
88
+ /// The formatted mode of the color, used in ANSI escape codes.
89
+ /// It is usually followed by the r;g;b components of the color.
90
+ String get formattedMode => mode == ColorMode .foreground ? '38;2' : '48;2' ;
37
91
38
92
@override
39
- String toString () => 'RGBColor($red , $green , $blue )' ;
93
+ String toString () => isNone ? 'RGBColor.none' : 'RGBColor($red , $green , $blue )' ;
40
94
}
41
95
42
96
class ANSIColor extends Color {
43
97
final int color;
44
98
45
- const ANSIColor (this .color) : super (color);
99
+ /// Creates a new ANSI color with the given value.
100
+ ANSIColor (this .color)
101
+ : super (
102
+ color,
103
+ isForegroundColor (color)
104
+ ? ColorMode .foreground
105
+ : ColorMode .background);
106
+
107
+ /// Creates a new ANSI foreground color with the given value.
108
+ const ANSIColor .fg (this .color) : super .fg (color);
109
+
110
+ /// Creates a new ANSI background color with the given value.
111
+ const ANSIColor .bg (this .color) : super .bg (color);
112
+
113
+ /// Returns true if the color is a foreground color.
114
+ bool get isForeground => mode == ColorMode .foreground;
115
+
116
+ /// Returns true if the color is a background color.
117
+ bool get isBackground => mode == ColorMode .background;
118
+
119
+ /// Returns true if the color is a foreground color.
120
+ static bool isForegroundColor (int color) =>
121
+ ((color >= 30 && color <= 37 ) || (color >= 90 && color <= 97 ));
46
122
47
- bool get isForeground => (color >= 30 && color <= 37 ) && (color >= 90 && color <= 97 );
48
- bool get isBackground => (color >= 40 && color <= 47 ) && (color >= 100 && color <= 107 );
123
+ /// Returns true if the color is a background color.
124
+ static bool isBackgroundColor (int color) =>
125
+ ((color >= 40 && color <= 47 ) || (color >= 100 && color <= 107 ));
49
126
50
127
@override
51
128
String get formatted => color.toString ();
52
129
53
130
@override
54
- String toString () => 'ANSIColor($color )' ;
131
+ String toString () => isNone ? 'ANSIColor.none' : 'ANSIColor($color )' ;
55
132
}
56
133
0 commit comments