@@ -23,12 +23,28 @@ pub struct Color(NonZeroU32);
2323impl Default for Color {
2424 /// Use a theme-defined color (automatic)
2525 fn default ( ) -> Self {
26- let color = Rgba8Srgb :: rgba ( 1 , 0 , 0 , 0 ) ;
27- Color ( NonZeroU32 :: new ( u32:: from_ne_bytes ( color. 0 ) ) . unwrap ( ) )
26+ Self :: DEFAULT
2827 }
2928}
3029
3130impl Color {
31+ /// Use the default theme-defined color
32+ ///
33+ /// As a foreground color, this maps to [`ColorsLinear::text`] or
34+ /// [`ColorsLinear::text_invert`] depending on the background.
35+ ///
36+ /// As a background color, this maps to [`ColorsLinear::edit_bg`].
37+ pub const DEFAULT : Self =
38+ Color ( NonZeroU32 :: new ( u32:: from_ne_bytes ( Rgba8Srgb :: rgba ( 1 , 0 , 0 , 0 ) . 0 ) ) . unwrap ( ) ) ;
39+
40+ /// Use the text-selection color
41+ ///
42+ /// As a foreground color this is identical to [`Self::DEFAULT`].
43+ ///
44+ /// As a background color, this maps to [`ColorsLinear::text_sel_bg`].
45+ pub const SELECTION : Self =
46+ Color ( NonZeroU32 :: new ( u32:: from_ne_bytes ( Rgba8Srgb :: rgba ( 1 , 1 , 0 , 0 ) . 0 ) ) . unwrap ( ) ) ;
47+
3248 /// Use an RGBA sRGB color
3349 ///
3450 /// This will resolve to the default theme color if `color.a() == 0`.
@@ -64,13 +80,9 @@ impl Color {
6480 self . as_rgba_srgb ( ) . map ( |c| c. into ( ) )
6581 }
6682
67- /// Resolve color
68- ///
69- /// If this represents a valid palette index, the palette's color will be
70- /// used, otherwise the color will be inferred from the theme, using `bg`
71- /// if provided.
83+ /// Resolve as (foreground) text color
7284 #[ inline]
73- pub fn resolve_color ( self , theme : & ColorsLinear , bg : Option < Rgba > ) -> Rgba {
85+ pub fn resolve_foreground ( self , theme : & ColorsLinear , bg : Option < Rgba > ) -> Rgba {
7486 if let Some ( col) = self . as_rgba ( ) {
7587 col
7688 } else if let Some ( bg) = bg {
@@ -79,6 +91,18 @@ impl Color {
7991 theme. text
8092 }
8193 }
94+
95+ /// Resolve as background color
96+ #[ inline]
97+ pub fn resolve_background ( self , theme : & ColorsLinear ) -> Rgba {
98+ if let Some ( col) = self . as_rgba ( ) {
99+ col
100+ } else if self == Self :: SELECTION {
101+ theme. text_sel_bg
102+ } else {
103+ theme. edit_bg
104+ }
105+ }
82106}
83107
84108/// Effect formatting marker: text and background color
@@ -93,14 +117,6 @@ pub struct Colors {
93117 pub background : Option < Color > ,
94118}
95119
96- impl Colors {
97- /// Resolve the background color, if any
98- pub fn resolve_background_color ( self , theme : & ColorsLinear ) -> Option < Rgba > {
99- self . background
100- . map ( |bg| bg. as_rgba ( ) . unwrap_or ( theme. edit_bg ) )
101- }
102- }
103-
104120/// Decoration types
105121#[ derive( Clone , Copy , Debug , Default , PartialEq , Eq ) ]
106122#[ cfg_attr( feature = "serde" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
0 commit comments