@@ -32,25 +32,22 @@ impl Color {
32
32
// TODO implement all the other color constants using code generation
33
33
34
34
/// Transparent black.
35
- pub const TRANSPARENT_BLACK : Color = Self :: new ( 0.0 , 0.0 , 0.0 , 0.0 ) ;
35
+ pub const TRANSPARENT_BLACK : Color = Self :: from_rgba ( 0.0 , 0.0 , 0.0 , 0.0 ) ;
36
36
37
- /// Transparent white. Equivalent of `Color.TRANSPARENT` in GDScript.
38
- pub const TRANSPARENT_WHITE : Color = Self :: new ( 1.0 , 1.0 , 1.0 , 0.0 ) ;
37
+ /// Transparent white.
38
+ ///
39
+ /// _Godot equivalent: `Color.TRANSPARENT`_
40
+ pub const TRANSPARENT_WHITE : Color = Self :: from_rgba ( 1.0 , 1.0 , 1.0 , 0.0 ) ;
39
41
40
42
/// Opaque black.
41
- pub const BLACK : Color = Self :: new ( 0.0 , 0.0 , 0.0 , 1.0 ) ;
43
+ pub const BLACK : Color = Self :: from_rgba ( 0.0 , 0.0 , 0.0 , 1.0 ) ;
42
44
43
45
/// Opaque white.
44
- pub const WHITE : Color = Self :: new ( 1.0 , 1.0 , 1.0 , 1.0 ) ;
46
+ pub const WHITE : Color = Self :: from_rgba ( 1.0 , 1.0 , 1.0 , 1.0 ) ;
45
47
46
48
/// Constructs a new `Color` with the given components.
47
- pub const fn new ( r : f32 , g : f32 , b : f32 , a : f32 ) -> Self {
48
- Self { r, g, b, a }
49
- }
50
-
51
- /// Constructs a new `Color` with the given components. More explicit alias of [`Color::new`].
52
49
pub const fn from_rgba ( r : f32 , g : f32 , b : f32 , a : f32 ) -> Self {
53
- Self :: new ( r, g, b, a)
50
+ Self { r, g, b, a }
54
51
}
55
52
56
53
/// Constructs a new `Color` with the given color components, and the alpha channel set to 1.
@@ -59,26 +56,30 @@ impl Color {
59
56
}
60
57
61
58
/// Constructs a new `Color` with the given components as bytes. 0 is mapped to 0.0, 255 is
62
- /// mapped to 1.0. Equivalent of the global constructor function `Color8` in GDScript.
59
+ /// mapped to 1.0.
60
+ ///
61
+ /// _Godot equivalent: the global `Color8` function_
63
62
pub fn from_rgba8 ( r : u8 , g : u8 , b : u8 , a : u8 ) -> Self {
64
63
Self :: from_rgba ( from_u8 ( r) , from_u8 ( g) , from_u8 ( b) , from_u8 ( a) )
65
64
}
66
65
67
- /// Constructs a new `Color` with the given components as bytes . 0 is mapped to 0.0, 65535
68
- /// (`0xffff`) is mapped to 1.0.
66
+ /// Constructs a new `Color` with the given components as `u16` words . 0 is mapped to 0.0,
67
+ /// 65535 (`0xffff`) is mapped to 1.0.
69
68
pub fn from_rgba16 ( r : u16 , g : u16 , b : u16 , a : u16 ) -> Self {
70
69
Self :: from_rgba ( from_u16 ( r) , from_u16 ( g) , from_u16 ( b) , from_u16 ( a) )
71
70
}
72
71
73
- /// Constructs a new `Color` from a 32-bits value with the given channel `order`. Passing
74
- /// `ColorChannelOrder::Rgba` is the equivalent of `Color.hex` in GDScript.
72
+ /// Constructs a new `Color` from a 32-bits value with the given channel `order`.
73
+ ///
74
+ /// _Godot equivalent: `Color.hex`, if `ColorChannelOrder::Rgba` is used_
75
75
pub fn from_u32_rgba ( u : u32 , order : ColorChannelOrder ) -> Self {
76
76
let [ r, g, b, a] = order. unpack ( u. to_be_bytes ( ) ) ;
77
77
Color :: from_rgba8 ( r, g, b, a)
78
78
}
79
79
80
- /// Constructs a new `Color` from a 64-bits value with the given channel `order`. Passing
81
- /// `ColorChannelOrder::Rgba` is the equivalent of `Color.hex64` in GDScript.
80
+ /// Constructs a new `Color` from a 64-bits value with the given channel `order`.
81
+ ///
82
+ /// _Godot equivalent: `Color.hex64`, if `ColorChannelOrder::Rgba` is used_
82
83
pub 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)
@@ -109,12 +110,14 @@ impl Color {
109
110
///
110
111
/// Returns `None` if the string is neither a valid HTML color code nor an existing color name.
111
112
///
113
+ /// Most color constants have an alpha of 1; use [`Color::with_alpha`] to change it.
114
+ ///
112
115
/// [color_constants]: https://docs.godotengine.org/en/latest/classes/class_color.html#constants
113
116
/// [cheat_sheet]: https://raw.githubusercontent.com/godotengine/godot-docs/master/img/color_constants.png
114
117
pub fn from_string < S : Into < GodotString > > ( string : S ) -> Option < Self > {
115
118
let color = InnerColor :: from_string (
116
119
string. into ( ) ,
117
- Self :: new ( f32:: NAN , f32:: NAN , f32:: NAN , f32:: NAN ) ,
120
+ Self :: from_rgba ( f32:: NAN , f32:: NAN , f32:: NAN , f32:: NAN ) ,
118
121
) ;
119
122
// Assumption: the implementation of `from_string` in the engine will never return any NaN
120
123
// upon success.
@@ -125,12 +128,17 @@ impl Color {
125
128
}
126
129
}
127
130
128
- /// Equivalent to [`Color::from_hsva`] with `a` set to 1.0.
131
+ /// Constructs a `Color` from an [HSV profile](https://en.wikipedia.org/wiki/HSL_and_HSV). The
132
+ /// hue (`h`), saturation (`s`), and value (`v`) are typically between 0.0 and 1.0. Alpha is
133
+ /// set to 1; use [`Color::with_alpha`] to change it.
129
134
pub fn from_hsv ( h : f64 , s : f64 , v : f64 ) -> Self {
130
135
InnerColor :: from_hsv ( h, s, v, 1.0 )
131
136
}
132
137
133
- /// Equivalent to [`Color::from_ok_hsl`] with `a` set to 1.0.
138
+ /// Constructs a `Color` from an [OK HSL
139
+ /// profile](https://bottosson.github.io/posts/colorpicker/). The hue (`h`), saturation (`s`),
140
+ /// and lightness (`l`) are typically between 0.0 and 1.0. Alpha is set to 1; use
141
+ /// [`Color::with_alpha`] to change it.
134
142
pub fn from_ok_hsl ( h : f64 , s : f64 , l : f64 ) -> Self {
135
143
InnerColor :: from_ok_hsl ( h, s, l, 1.0 )
136
144
}
@@ -241,7 +249,7 @@ impl Color {
241
249
}
242
250
243
251
/// Returns the color with its `r`, `g`, and `b` components inverted:
244
- /// `Color::new (1 - r, 1 - g, 1 - b, a)`.
252
+ /// `Color::from_rgba (1 - r, 1 - g, 1 - b, a)`.
245
253
#[ must_use]
246
254
pub fn inverted ( self ) -> Self {
247
255
self . as_inner ( ) . inverted ( )
@@ -465,7 +473,7 @@ impl ops::SubAssign<Color> for Color {
465
473
impl ops:: Neg for Color {
466
474
type Output = Self ;
467
475
fn neg ( self ) -> Self {
468
- Self :: new ( -self . r , -self . g , -self . b , -self . a )
476
+ Self :: from_rgba ( -self . r , -self . g , -self . b , -self . a )
469
477
}
470
478
}
471
479
0 commit comments