@@ -58,28 +58,29 @@ impl Color {
58
58
/// mapped to 1.0.
59
59
///
60
60
/// _Godot equivalent: the global `Color8` function_
61
- pub fn from_rgba8 ( r : u8 , g : u8 , b : u8 , a : u8 ) -> Self {
61
+ pub const fn from_rgba8 ( r : u8 , g : u8 , b : u8 , a : u8 ) -> Self {
62
62
Self :: from_rgba ( from_u8 ( r) , from_u8 ( g) , from_u8 ( b) , from_u8 ( a) )
63
63
}
64
64
65
- /// Constructs a new `Color` with the given components as `u16` words. 0 is mapped to 0.0,
66
- /// 65535 (`0xffff`) is mapped to 1.0.
67
- pub fn from_rgba16 ( r : u16 , g : u16 , b : u16 , a : u16 ) -> Self {
65
+ /// Constructs a new `Color` with the given components as `u16` words.
66
+ ///
67
+ /// 0 is mapped to 0.0, 65535 (`0xFFFF`) is mapped to 1.0.
68
+ pub const fn from_rgba16 ( r : u16 , g : u16 , b : u16 , a : u16 ) -> Self {
68
69
Self :: from_rgba ( from_u16 ( r) , from_u16 ( g) , from_u16 ( b) , from_u16 ( a) )
69
70
}
70
71
71
72
/// Constructs a new `Color` from a 32-bits value with the given channel `order`.
72
73
///
73
74
/// _Godot equivalent: `Color.hex`, if `ColorChannelOrder::Rgba` is used_
74
- pub fn from_u32_rgba ( u : u32 , order : ColorChannelOrder ) -> Self {
75
+ pub const fn from_u32_rgba ( u : u32 , order : ColorChannelOrder ) -> Self {
75
76
let [ r, g, b, a] = order. unpack ( u. to_be_bytes ( ) ) ;
76
77
Color :: from_rgba8 ( r, g, b, a)
77
78
}
78
79
79
80
/// Constructs a new `Color` from a 64-bits value with the given channel `order`.
80
81
///
81
82
/// _Godot equivalent: `Color.hex64`, if `ColorChannelOrder::Rgba` is used_
82
- pub fn from_u64_rgba ( u : u64 , order : ColorChannelOrder ) -> Self {
83
+ pub const fn from_u64_rgba ( u : u64 , order : ColorChannelOrder ) -> Self {
83
84
let [ r, g, b, a] = order. unpack ( to_be_words ( u) ) ;
84
85
Color :: from_rgba16 ( r, g, b, a)
85
86
}
@@ -383,7 +384,7 @@ pub enum ColorChannelOrder {
383
384
}
384
385
385
386
impl ColorChannelOrder {
386
- fn pack < T > ( self , rgba : [ T ; 4 ] ) -> [ T ; 4 ] {
387
+ const fn pack < T : Copy > ( self , rgba : [ T ; 4 ] ) -> [ T ; 4 ] {
387
388
let [ r, g, b, a] = rgba;
388
389
match self {
389
390
ColorChannelOrder :: RGBA => [ r, g, b, a] ,
@@ -392,7 +393,7 @@ impl ColorChannelOrder {
392
393
}
393
394
}
394
395
395
- fn unpack < T > ( self , xyzw : [ T ; 4 ] ) -> [ T ; 4 ] {
396
+ const fn unpack < T : Copy > ( self , xyzw : [ T ; 4 ] ) -> [ T ; 4 ] {
396
397
let [ x, y, z, w] = xyzw;
397
398
match self {
398
399
ColorChannelOrder :: RGBA => [ x, y, z, w] ,
@@ -510,12 +511,12 @@ impl ops::Neg for Color {
510
511
}
511
512
512
513
/// Converts a single channel byte to a float in the range 0 to 1.
513
- fn from_u8 ( byte : u8 ) -> f32 {
514
+ const fn from_u8 ( byte : u8 ) -> f32 {
514
515
byte as f32 / 255.0
515
516
}
516
517
517
518
/// Converts a single channel `u16` word to a float in the range 0 to 1.
518
- fn from_u16 ( byte : u16 ) -> f32 {
519
+ const fn from_u16 ( byte : u16 ) -> f32 {
519
520
byte as f32 / 65535.0
520
521
}
521
522
@@ -542,7 +543,7 @@ fn from_be_words(words: [u16; 4]) -> u64 {
542
543
}
543
544
544
545
/// Unpacks a `u64` into four `u16` words in big-endian order.
545
- fn to_be_words ( mut u : u64 ) -> [ u16 ; 4 ] {
546
+ const fn to_be_words ( mut u : u64 ) -> [ u16 ; 4 ] {
546
547
let w = ( u & 0xffff ) as u16 ;
547
548
u >>= 16 ;
548
549
let z = ( u & 0xffff ) as u16 ;
0 commit comments