@@ -48,15 +48,14 @@ extension VTAttributes {
4848 }
4949}
5050
51- private func pack( _ color: VTColor ) -> UInt64 {
52- return switch color {
53- case let . rgb( red, green, blue) :
54- // [23-16: red][15-8: green][7-0: blue]
55- ( UInt64 ( red) << 16 ) | ( UInt64 ( green) << 8 ) | ( UInt64 ( blue) << 0 )
56- case let . ansi( color) :
57- // [23-9: reserved][8: intensity][7-0: color]
58- ( UInt64 ( color. intensity == . bright ? 1 : 0 ) << 8 ) | ( UInt64 ( color. color. rawValue) & 0xff )
59- }
51+ private func pack( _ color: ANSIColor , _ intensity: ANSIColorIntensity ) -> UInt64 {
52+ // [23-9: reserved][8: intensity][7-0: color]
53+ return ( UInt64 ( intensity == . bright ? 1 : 0 ) << 8 ) | ( UInt64 ( color. rawValue) & 0xff )
54+ }
55+
56+ private func pack( _ red: UInt8 , _ green: UInt8 , _ blue: UInt8 ) -> UInt64 {
57+ // [23-16: red][15-8: green][7-0: blue]
58+ return ( UInt64 ( red) << 16 ) | ( UInt64 ( green) << 8 ) | ( UInt64 ( blue) << 0 )
6059}
6160
6261private struct Flags : OptionSet {
@@ -121,22 +120,22 @@ public struct VTStyle: Sendable, Equatable {
121120 public init ( foreground: VTColor ? = nil , background: VTColor ? = nil , attributes: VTAttributes = [ ] ) {
122121 var representation = ( UInt64 ( attributes. rawValue) << 8 )
123122
124- if let foreground {
125- switch foreground {
126- case . ansi ( _ ) :
127- representation |= ( pack ( foreground ) << 16 ) | UInt64 ( Flags . ANSIForeground . rawValue )
128- case . rgb ( _ , _ , _ ) :
129- representation |= ( pack ( foreground ) << 16 ) | UInt64 ( Flags . RGBForeground . rawValue )
130- }
123+ switch foreground {
124+ case . none :
125+ representation |= ( pack ( ANSIColor . default , . normal ) << 16 ) | UInt64 ( Flags . ANSIForeground . rawValue )
126+ case let . some ( . ansi ( color , intensity ) ) :
127+ representation |= ( pack ( color , intensity ) << 16 ) | UInt64 ( Flags . ANSIForeground . rawValue )
128+ case let . some ( . rgb ( red , green , blue ) ) :
129+ representation |= ( pack ( red , green , blue ) << 16 ) | UInt64 ( Flags . RGBForeground . rawValue )
131130 }
132131
133- if let background {
134- switch background {
135- case . ansi ( _ ) :
136- representation |= ( pack ( background ) << 40 ) | UInt64 ( Flags . ANSIBackground . rawValue )
137- case . rgb ( _ , _ , _ ) :
138- representation |= ( pack ( background ) << 40 ) | UInt64 ( Flags . RGBBackground . rawValue )
139- }
132+ switch background {
133+ case . none :
134+ representation |= ( pack ( ANSIColor . default , . normal ) << 40 ) | UInt64 ( Flags . ANSIBackground . rawValue )
135+ case let . some ( . ansi ( color , intensity ) ) :
136+ representation |= ( pack ( color , intensity ) << 40 ) | UInt64 ( Flags . ANSIBackground . rawValue )
137+ case let . some ( . rgb ( red , green , blue ) ) :
138+ representation |= ( pack ( red , green , blue ) << 40 ) | UInt64 ( Flags . RGBBackground . rawValue )
140139 }
141140
142141 self . representation = representation
@@ -148,12 +147,12 @@ public struct VTStyle: Sendable, Equatable {
148147
149148 if flags. contains ( . ANSIForeground) {
150149 let bits = representation >> 16
151- guard let color = ANSIColorIdentifier ( rawValue: ( Int ( bits) & 0xff ) ) else {
150+ guard let color = ANSIColor ( rawValue: ( Int ( bits) & 0xff ) ) else {
152151 return nil
153152 }
154153 let intensity = ( bits >> 8 ) & 1 == 1 ? ANSIColorIntensity . bright
155154 : ANSIColorIntensity . normal
156- return . ansi( ANSIColor ( color: color , intensity: intensity) )
155+ return . ansi( color, intensity: intensity)
157156 }
158157
159158 if flags. contains ( . RGBForeground) {
@@ -174,12 +173,12 @@ public struct VTStyle: Sendable, Equatable {
174173
175174 if flags. contains ( . ANSIBackground) {
176175 let bits = representation >> 40
177- guard let color = ANSIColorIdentifier ( rawValue: ( Int ( bits) & 0xff ) ) else {
176+ guard let color = ANSIColor ( rawValue: ( Int ( bits) & 0xff ) ) else {
178177 return nil
179178 }
180179 let intensity = ( bits >> 8 ) & 1 == 1 ? ANSIColorIntensity . bright
181180 : ANSIColorIntensity . normal
182- return . ansi( ANSIColor ( color: color , intensity: intensity) )
181+ return . ansi( color, intensity: intensity)
183182 }
184183
185184 if flags. contains ( . RGBBackground) {
0 commit comments