@@ -35,18 +35,19 @@ pub struct Color {
3535 pub a : u8 ,
3636}
3737
38- impl Default for Color {
39- fn default ( ) -> Self {
40- Self {
41- r : 0 ,
42- g : 0 ,
43- b : 0 ,
44- a : 255 ,
45- }
38+ impl Color {
39+ pub const TRANSPARENT : Self = Self :: new ( 0 , 0 , 0 , 0 ) ;
40+ pub const BLACK : Self = Self :: new ( 0 , 0 , 0 , 255 ) ;
41+ pub const RED : Self = Self :: new ( 255 , 0 , 0 , 255 ) ;
42+ pub const GREEN : Self = Self :: new ( 0 , 255 , 0 , 255 ) ;
43+ pub const BLUE : Self = Self :: new ( 0 , 0 , 255 , 255 ) ;
44+ pub const WHITE : Self = Self :: new ( 255 , 255 , 255 , 255 ) ;
45+
46+ #[ inline]
47+ pub const fn new ( r : u8 , g : u8 , b : u8 , a : u8 ) -> Self {
48+ Self { r, g, b, a }
4649 }
47- }
4850
49- impl Color {
5051 // Linear interpolation in sRGB space; use `lerp_linear` for perceptual correctness.
5152 #[ must_use]
5253 #[ inline]
@@ -186,6 +187,10 @@ impl Color {
186187 #[ must_use]
187188 #[ inline]
188189 pub fn over_srgb_fast ( self , mut dst : Color ) -> Color {
190+ if dst. a == 0 {
191+ dst = self ;
192+ }
193+
189194 let sa = self . a as ColorFloat / 255.0 ;
190195 if sa <= 0.0 {
191196 return dst;
@@ -250,7 +255,7 @@ impl Color {
250255
251256 #[ must_use]
252257 #[ inline]
253- pub fn with_alpha ( self , a : u8 ) -> Self {
258+ pub const fn with_alpha ( self , a : u8 ) -> Self {
254259 Self {
255260 r : self . r ,
256261 g : self . g ,
@@ -260,7 +265,7 @@ impl Color {
260265 }
261266
262267 #[ inline]
263- pub fn from_rgb ( rgb : [ u8 ; 3 ] ) -> Self {
268+ pub const fn from_rgb ( rgb : [ u8 ; 3 ] ) -> Self {
264269 Self {
265270 r : rgb[ 0 ] ,
266271 g : rgb[ 1 ] ,
@@ -271,12 +276,12 @@ impl Color {
271276
272277 #[ must_use]
273278 #[ inline]
274- pub fn into_rgb ( self ) -> [ u8 ; 3 ] {
279+ pub const fn into_rgb ( self ) -> [ u8 ; 3 ] {
275280 [ self . r , self . g , self . b ]
276281 }
277282
278283 #[ inline]
279- pub fn from_rgba ( rgba : [ u8 ; 4 ] ) -> Self {
284+ pub const fn from_rgba ( rgba : [ u8 ; 4 ] ) -> Self {
280285 Self {
281286 r : rgba[ 0 ] ,
282287 g : rgba[ 1 ] ,
@@ -287,7 +292,7 @@ impl Color {
287292
288293 #[ must_use]
289294 #[ inline]
290- pub fn into_rgba ( self ) -> [ u8 ; 4 ] {
295+ pub const fn into_rgba ( self ) -> [ u8 ; 4 ] {
291296 [ self . r , self . g , self . b , self . a ]
292297 }
293298
@@ -695,12 +700,18 @@ impl Color {
695700 }
696701}
697702
703+ impl Default for Color {
704+ fn default ( ) -> Self {
705+ Color :: new ( 0 , 0 , 0 , 255 )
706+ }
707+ }
708+
698709impl fmt:: Display for Color {
699710 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
700711 // default to RGBA hex for lossless stringification
701712 write ! (
702713 f,
703- "#{:02x }{:02x }{:02x }{:02x }" ,
714+ "#{:02X }{:02X }{:02X }{:02X }" ,
704715 self . r, self . g, self . b, self . a
705716 )
706717 }
0 commit comments