Skip to content

Commit 6c1b5a1

Browse files
committed
Several Color constructors are now const
1 parent 5fc4358 commit 6c1b5a1

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

godot-core/src/builtin/color.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,28 +58,29 @@ impl Color {
5858
/// mapped to 1.0.
5959
///
6060
/// _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 {
6262
Self::from_rgba(from_u8(r), from_u8(g), from_u8(b), from_u8(a))
6363
}
6464

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 {
6869
Self::from_rgba(from_u16(r), from_u16(g), from_u16(b), from_u16(a))
6970
}
7071

7172
/// Constructs a new `Color` from a 32-bits value with the given channel `order`.
7273
///
7374
/// _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 {
7576
let [r, g, b, a] = order.unpack(u.to_be_bytes());
7677
Color::from_rgba8(r, g, b, a)
7778
}
7879

7980
/// Constructs a new `Color` from a 64-bits value with the given channel `order`.
8081
///
8182
/// _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 {
8384
let [r, g, b, a] = order.unpack(to_be_words(u));
8485
Color::from_rgba16(r, g, b, a)
8586
}
@@ -383,7 +384,7 @@ pub enum ColorChannelOrder {
383384
}
384385

385386
impl ColorChannelOrder {
386-
fn pack<T>(self, rgba: [T; 4]) -> [T; 4] {
387+
const fn pack<T: Copy>(self, rgba: [T; 4]) -> [T; 4] {
387388
let [r, g, b, a] = rgba;
388389
match self {
389390
ColorChannelOrder::RGBA => [r, g, b, a],
@@ -392,7 +393,7 @@ impl ColorChannelOrder {
392393
}
393394
}
394395

395-
fn unpack<T>(self, xyzw: [T; 4]) -> [T; 4] {
396+
const fn unpack<T: Copy>(self, xyzw: [T; 4]) -> [T; 4] {
396397
let [x, y, z, w] = xyzw;
397398
match self {
398399
ColorChannelOrder::RGBA => [x, y, z, w],
@@ -510,12 +511,12 @@ impl ops::Neg for Color {
510511
}
511512

512513
/// 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 {
514515
byte as f32 / 255.0
515516
}
516517

517518
/// 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 {
519520
byte as f32 / 65535.0
520521
}
521522

@@ -542,7 +543,7 @@ fn from_be_words(words: [u16; 4]) -> u64 {
542543
}
543544

544545
/// 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] {
546547
let w = (u & 0xffff) as u16;
547548
u >>= 16;
548549
let z = (u & 0xffff) as u16;

0 commit comments

Comments
 (0)