Skip to content

Commit 9ae5ba2

Browse files
alice-i-cecileAlice Cecile
and
Alice Cecile
authored
bevy_color::Color convenience methods (#12171)
# Objective As suggested in #12163 by @cart, we should add convenience constructors to `bevy_color::Color` to match the existing API (easing migration pain) and generally improve ergonomics. ## Solution - Add `const fn Color::rgba(red, green, blue, alpha)` and friends, which directly construct the appropriate variant. - Add `const fn Color::rgb(red, green, blue)` and friends, which impute and alpha value of 1.0. - Add `const BLACK, WHITE, NONE` to `Color`. These are stored in `LinearRgba` to reduce pointless conversion costs and inaccuracy. - Changed the default `Color` from `Srgba::WHITE` to the new linear equivalent for the same reason. --------- Co-authored-by: Alice Cecile <[email protected]>
1 parent cbfeb94 commit 9ae5ba2

File tree

1 file changed

+181
-1
lines changed

1 file changed

+181
-1
lines changed

crates/bevy_color/src/color.rs

Lines changed: 181 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,191 @@ impl Color {
4040
pub fn linear(&self) -> LinearRgba {
4141
(*self).into()
4242
}
43+
44+
/// Creates a new [`Color`] object storing a [`Srgba`] color.
45+
pub const fn srgba(red: f32, green: f32, blue: f32, alpha: f32) -> Self {
46+
Self::Srgba(Srgba {
47+
red,
48+
green,
49+
blue,
50+
alpha,
51+
})
52+
}
53+
54+
/// Creates a new [`Color`] object storing a [`Srgba`] color with an alpha of 1.0.
55+
pub const fn srgb(red: f32, green: f32, blue: f32) -> Self {
56+
Self::Srgba(Srgba {
57+
red,
58+
green,
59+
blue,
60+
alpha: 1.0,
61+
})
62+
}
63+
64+
/// Createsa new [`Color`] object storing a [`LinearRgba`] color.
65+
pub const fn linear_rgba(red: f32, green: f32, blue: f32, alpha: f32) -> Self {
66+
Self::LinearRgba(LinearRgba {
67+
red,
68+
green,
69+
blue,
70+
alpha,
71+
})
72+
}
73+
74+
/// a new [`Color`] object storing a [`LinearRgba`] color with an alpha of 1.0.
75+
pub const fn linear_rgb(red: f32, green: f32, blue: f32) -> Self {
76+
Self::LinearRgba(LinearRgba {
77+
red,
78+
green,
79+
blue,
80+
alpha: 1.0,
81+
})
82+
}
83+
84+
/// Creates a new [`Color`] object storing a [`Hsla`] color.
85+
pub const fn hsla(hue: f32, saturation: f32, lightness: f32, alpha: f32) -> Self {
86+
Self::Hsla(Hsla {
87+
hue,
88+
saturation,
89+
lightness,
90+
alpha,
91+
})
92+
}
93+
94+
/// Creates a new [`Color`] object storing a [`Hsla`] color with an alpha of 1.0.
95+
pub const fn hsl(hue: f32, saturation: f32, lightness: f32) -> Self {
96+
Self::Hsla(Hsla {
97+
hue,
98+
saturation,
99+
lightness,
100+
alpha: 1.0,
101+
})
102+
}
103+
104+
/// Creates a new [`Color`] object storing a [`Hsva`] color.
105+
pub const fn hsva(hue: f32, saturation: f32, value: f32, alpha: f32) -> Self {
106+
Self::Hsva(Hsva {
107+
hue,
108+
saturation,
109+
value,
110+
alpha,
111+
})
112+
}
113+
114+
/// Creates a new [`Color`] object storing a [`Hsva`] color with an alpha of 1.0.
115+
pub const fn hsv(hue: f32, saturation: f32, value: f32) -> Self {
116+
Self::Hsva(Hsva {
117+
hue,
118+
saturation,
119+
value,
120+
alpha: 1.0,
121+
})
122+
}
123+
124+
/// Creates a new [`Color`] object storing a [`Hwba`] color.
125+
pub const fn hwba(hue: f32, whiteness: f32, blackness: f32, alpha: f32) -> Self {
126+
Self::Hwba(Hwba {
127+
hue,
128+
whiteness,
129+
blackness,
130+
alpha,
131+
})
132+
}
133+
134+
/// Creates a new [`Color`] object storing a [`Hwba`] color with an alpha of 1.0.
135+
pub const fn hwb(hue: f32, whiteness: f32, blackness: f32) -> Self {
136+
Self::Hwba(Hwba {
137+
hue,
138+
whiteness,
139+
blackness,
140+
alpha: 1.0,
141+
})
142+
}
143+
144+
/// Creates a new [`Color`] object storing a [`Laba`] color.
145+
pub const fn laba(lightness: f32, a: f32, b: f32, alpha: f32) -> Self {
146+
Self::Laba(Laba {
147+
lightness,
148+
a,
149+
b,
150+
alpha,
151+
})
152+
}
153+
154+
/// Creates a new [`Color`] object storing a [`Laba`] color with an alpha of 1.0.
155+
pub const fn lab(lightness: f32, a: f32, b: f32) -> Self {
156+
Self::Laba(Laba {
157+
lightness,
158+
a,
159+
b,
160+
alpha: 1.0,
161+
})
162+
}
163+
164+
/// Creates a new [`Color`] object storing a [`Lcha`] color.
165+
pub const fn lcha(lightness: f32, chroma: f32, hue: f32, alpha: f32) -> Self {
166+
Self::Lcha(Lcha {
167+
lightness,
168+
chroma,
169+
hue,
170+
alpha,
171+
})
172+
}
173+
174+
/// Creates a new [`Color`] object storing a [`Lcha`] color with an alpha of 1.0.
175+
pub const fn lch(lightness: f32, chroma: f32, hue: f32) -> Self {
176+
Self::Lcha(Lcha {
177+
lightness,
178+
chroma,
179+
hue,
180+
alpha: 1.0,
181+
})
182+
}
183+
184+
/// Creates a new [`Color`] object storing a [`Oklaba`] color.
185+
pub const fn oklaba(l: f32, a: f32, b: f32, alpha: f32) -> Self {
186+
Self::Oklaba(Oklaba { l, a, b, alpha })
187+
}
188+
189+
/// Creates a new [`Color`] object storing a [`Oklaba`] color with an alpha of 1.0.
190+
pub const fn oklab(l: f32, a: f32, b: f32) -> Self {
191+
Self::Oklaba(Oklaba {
192+
l,
193+
a,
194+
b,
195+
alpha: 1.0,
196+
})
197+
}
198+
199+
/// Creates a new [`Color`] object storing a [`Xyza`] color.
200+
pub const fn xyza(x: f32, y: f32, z: f32, alpha: f32) -> Self {
201+
Self::Xyza(Xyza { x, y, z, alpha })
202+
}
203+
204+
/// Creates a new [`Color`] object storing a [`Xyza`] color with an alpha of 1.0.
205+
pub const fn xyz(x: f32, y: f32, z: f32) -> Self {
206+
Self::Xyza(Xyza {
207+
x,
208+
y,
209+
z,
210+
alpha: 1.0,
211+
})
212+
}
213+
214+
/// A fully white [`Color::LinearRgba`] color with an alpha of 1.0.
215+
pub const WHITE: Self = Self::linear_rgb(1.0, 1.0, 1.0);
216+
217+
/// A fully black [`Color::LinearRgba`] color with an alpha of 1.0.
218+
pub const BLACK: Self = Self::linear_rgb(0., 0., 0.);
219+
220+
/// A fully transparent [`Color::LinearRgba`] color.
221+
pub const TRANSPARENT: Self = Self::linear_rgba(0., 0., 0., 0.);
43222
}
44223

45224
impl Default for Color {
225+
/// A fully white [`Color::LinearRgba`] color with an alpha of 1.0.
46226
fn default() -> Self {
47-
Self::Srgba(Srgba::WHITE)
227+
Color::WHITE
48228
}
49229
}
50230

0 commit comments

Comments
 (0)