4
4
#[ allow( clippy:: exhaustive_structs) ]
5
5
pub struct Color {
6
6
/// Red
7
- pub r : u16 ,
7
+ pub red : u16 ,
8
8
/// Green
9
- pub g : u16 ,
9
+ pub green : u16 ,
10
10
/// Blue
11
- pub b : u16 ,
11
+ pub blue : u16 ,
12
+ /// Can almost always be ignored as it is rarely set to
13
+ /// something other than the default (`0xffff`).
14
+ pub alpha : u16 ,
12
15
}
13
16
14
17
impl Color {
@@ -22,29 +25,47 @@ impl Color {
22
25
/// let is_dark = color.perceived_lightness() <= 0.5;
23
26
/// ```
24
27
pub fn perceived_lightness ( & self ) -> f32 {
25
- let color = xterm_color:: Color :: rgb ( self . r , self . g , self . b ) ;
28
+ let color = xterm_color:: Color {
29
+ red : self . red ,
30
+ green : self . green ,
31
+ blue : self . blue ,
32
+ alpha : self . alpha ,
33
+ } ;
26
34
color. perceived_lightness ( )
27
35
}
28
36
29
37
/// Converts the color to 8 bit precision per channel by scaling each channel.
30
38
///
31
39
/// ```
32
40
/// # use terminal_colorsaurus::Color;
33
- /// let white = Color { r : u16::MAX, g : u16::MAX, b : u16::MAX };
34
- /// assert_eq!((u8::MAX, u8::MAX, u8::MAX), white.scale_to_8bit());
41
+ /// let white = Color { red : u16::MAX, green : u16::MAX, blue: u16::MAX, alpha : u16::MAX };
42
+ /// assert_eq!((u8::MAX, u8::MAX, u8::MAX, u8::MAX ), white.scale_to_8bit());
35
43
///
36
- /// let black = Color { r : 0, g : 0, b : 0 };
37
- /// assert_eq!((0, 0, 0), black.scale_to_8bit());
44
+ /// let black = Color { red : 0, green : 0, blue : 0, alpha: u16::MAX };
45
+ /// assert_eq!((0, 0, 0, u8::MAX ), black.scale_to_8bit());
38
46
/// ```
39
- pub fn scale_to_8bit ( & self ) -> ( u8 , u8 , u8 ) {
47
+ pub fn scale_to_8bit ( & self ) -> ( u8 , u8 , u8 , u8 ) {
40
48
(
41
- scale_to_u8 ( self . r ) ,
42
- scale_to_u8 ( self . g ) ,
43
- scale_to_u8 ( self . b ) ,
49
+ scale_to_u8 ( self . red ) ,
50
+ scale_to_u8 ( self . green ) ,
51
+ scale_to_u8 ( self . blue ) ,
52
+ scale_to_u8 ( self . alpha ) ,
44
53
)
45
54
}
46
55
}
47
56
57
+ #[ cfg( test) ]
58
+ impl Color {
59
+ pub ( crate ) const fn rgb ( red : u16 , green : u16 , blue : u16 ) -> Self {
60
+ Self {
61
+ red,
62
+ green,
63
+ blue,
64
+ alpha : u16:: MAX ,
65
+ }
66
+ }
67
+ }
68
+
48
69
fn scale_to_u8 ( channel : u16 ) -> u8 {
49
70
( channel as u32 * ( u8:: MAX as u32 ) / ( u16:: MAX as u32 ) ) as u8
50
71
}
@@ -53,36 +74,57 @@ fn scale_to_u8(channel: u16) -> u8 {
53
74
impl From < Color > for rgb:: RGB16 {
54
75
fn from ( value : Color ) -> Self {
55
76
rgb:: RGB16 {
56
- r : value. r ,
57
- g : value. g ,
58
- b : value. b ,
77
+ r : value. red ,
78
+ g : value. green ,
79
+ b : value. blue ,
80
+ }
81
+ }
82
+ }
83
+
84
+ #[ cfg( feature = "rgb" ) ]
85
+ impl From < Color > for rgb:: RGBA16 {
86
+ fn from ( value : Color ) -> Self {
87
+ rgb:: RGBA16 {
88
+ r : value. red ,
89
+ g : value. green ,
90
+ b : value. blue ,
91
+ a : value. alpha ,
59
92
}
60
93
}
61
94
}
62
95
63
96
#[ cfg( feature = "rgb" ) ]
64
97
impl From < Color > for rgb:: RGB8 {
65
98
fn from ( value : Color ) -> Self {
66
- let ( r, g, b) = value. scale_to_8bit ( ) ;
99
+ let ( r, g, b, _ ) = value. scale_to_8bit ( ) ;
67
100
rgb:: RGB8 { r, g, b }
68
101
}
69
102
}
70
103
104
+ #[ cfg( feature = "rgb" ) ]
105
+ impl From < Color > for rgb:: RGBA8 {
106
+ fn from ( value : Color ) -> Self {
107
+ let ( r, g, b, a) = value. scale_to_8bit ( ) ;
108
+ rgb:: RGBA8 { r, g, b, a }
109
+ }
110
+ }
111
+
71
112
#[ cfg( feature = "rgb" ) ]
72
113
impl From < rgb:: RGB16 > for Color {
73
114
fn from ( value : rgb:: RGB16 ) -> Self {
74
115
Color {
75
- r : value. r ,
76
- g : value. g ,
77
- b : value. b ,
116
+ red : value. r ,
117
+ green : value. g ,
118
+ blue : value. b ,
119
+ alpha : u16:: MAX ,
78
120
}
79
121
}
80
122
}
81
123
82
124
#[ cfg( feature = "anstyle" ) ]
83
125
impl From < Color > for anstyle:: RgbColor {
84
126
fn from ( value : Color ) -> Self {
85
- let ( r, g, b) = value. scale_to_8bit ( ) ;
127
+ let ( r, g, b, _ ) = value. scale_to_8bit ( ) ;
86
128
anstyle:: RgbColor ( r, g, b)
87
129
}
88
130
}
@@ -100,9 +142,10 @@ mod tests {
100
142
#[ test]
101
143
fn white_has_perceived_lightness_100 ( ) {
102
144
let white = Color {
103
- r : u16:: MAX ,
104
- g : u16:: MAX ,
105
- b : u16:: MAX ,
145
+ red : u16:: MAX ,
146
+ green : u16:: MAX ,
147
+ blue : u16:: MAX ,
148
+ alpha : u16:: MAX ,
106
149
} ;
107
150
assert_eq ! ( 1.0 , white. perceived_lightness( ) )
108
151
}
0 commit comments