Adding "fluent" methods to modify color channels. (#12099)
Fixes #12075
This commit is contained in:
		
							parent
							
								
									e689d46015
								
							
						
					
					
						commit
						10aef141f3
					
				| @ -47,6 +47,21 @@ impl Hsla { | |||||||
|     pub const fn hsl(hue: f32, saturation: f32, lightness: f32) -> Self { |     pub const fn hsl(hue: f32, saturation: f32, lightness: f32) -> Self { | ||||||
|         Self::new(hue, saturation, lightness, 1.0) |         Self::new(hue, saturation, lightness, 1.0) | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     /// Return a copy of this color with the hue channel set to the given value.
 | ||||||
|  |     pub const fn with_hue(self, hue: f32) -> Self { | ||||||
|  |         Self { hue, ..self } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     /// Return a copy of this color with the saturation channel set to the given value.
 | ||||||
|  |     pub const fn with_saturation(self, saturation: f32) -> Self { | ||||||
|  |         Self { saturation, ..self } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     /// Return a copy of this color with the lightness channel set to the given value.
 | ||||||
|  |     pub const fn with_lightness(self, lightness: f32) -> Self { | ||||||
|  |         Self { lightness, ..self } | ||||||
|  |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| impl Default for Hsla { | impl Default for Hsla { | ||||||
|  | |||||||
| @ -52,6 +52,21 @@ impl Lcha { | |||||||
|             alpha: 1.0, |             alpha: 1.0, | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     /// Return a copy of this color with the hue channel set to the given value.
 | ||||||
|  |     pub const fn with_hue(self, hue: f32) -> Self { | ||||||
|  |         Self { hue, ..self } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     /// Return a copy of this color with the chroma channel set to the given value.
 | ||||||
|  |     pub const fn with_chroma(self, chroma: f32) -> Self { | ||||||
|  |         Self { chroma, ..self } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     /// Return a copy of this color with the lightness channel set to the given value.
 | ||||||
|  |     pub const fn with_lightness(self, lightness: f32) -> Self { | ||||||
|  |         Self { lightness, ..self } | ||||||
|  |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| impl Default for Lcha { | impl Default for Lcha { | ||||||
|  | |||||||
| @ -50,6 +50,21 @@ impl LinearRgba { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     /// Return a copy of this color with the red channel set to the given value.
 | ||||||
|  |     pub const fn with_red(self, red: f32) -> Self { | ||||||
|  |         Self { red, ..self } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     /// Return a copy of this color with the green channel set to the given value.
 | ||||||
|  |     pub const fn with_green(self, green: f32) -> Self { | ||||||
|  |         Self { green, ..self } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     /// Return a copy of this color with the blue channel set to the given value.
 | ||||||
|  |     pub const fn with_blue(self, blue: f32) -> Self { | ||||||
|  |         Self { blue, ..self } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     /// Make the color lighter or darker by some amount
 |     /// Make the color lighter or darker by some amount
 | ||||||
|     fn adjust_lightness(&mut self, amount: f32) { |     fn adjust_lightness(&mut self, amount: f32) { | ||||||
|         let luminance = self.luminance(); |         let luminance = self.luminance(); | ||||||
|  | |||||||
| @ -50,6 +50,21 @@ impl Oklaba { | |||||||
|             alpha: 1.0, |             alpha: 1.0, | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     /// Return a copy of this color with the 'l' channel set to the given value.
 | ||||||
|  |     pub const fn with_l(self, l: f32) -> Self { | ||||||
|  |         Self { l, ..self } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     /// Return a copy of this color with the 'a' channel set to the given value.
 | ||||||
|  |     pub const fn with_a(self, a: f32) -> Self { | ||||||
|  |         Self { a, ..self } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     /// Return a copy of this color with the 'b' channel set to the given value.
 | ||||||
|  |     pub const fn with_b(self, b: f32) -> Self { | ||||||
|  |         Self { b, ..self } | ||||||
|  |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| impl Default for Oklaba { | impl Default for Oklaba { | ||||||
|  | |||||||
| @ -67,6 +67,21 @@ impl Srgba { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     /// Return a copy of this color with the red channel set to the given value.
 | ||||||
|  |     pub const fn with_red(self, red: f32) -> Self { | ||||||
|  |         Self { red, ..self } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     /// Return a copy of this color with the green channel set to the given value.
 | ||||||
|  |     pub const fn with_green(self, green: f32) -> Self { | ||||||
|  |         Self { green, ..self } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     /// Return a copy of this color with the blue channel set to the given value.
 | ||||||
|  |     pub const fn with_blue(self, blue: f32) -> Self { | ||||||
|  |         Self { blue, ..self } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     /// New `Srgba` from a CSS-style hexadecimal string.
 |     /// New `Srgba` from a CSS-style hexadecimal string.
 | ||||||
|     ///
 |     ///
 | ||||||
|     /// # Examples
 |     /// # Examples
 | ||||||
|  | |||||||
| @ -39,7 +39,7 @@ impl Xyza { | |||||||
|     /// * `x` - x-axis. [0.0, 1.0]
 |     /// * `x` - x-axis. [0.0, 1.0]
 | ||||||
|     /// * `y` - y-axis. [0.0, 1.0]
 |     /// * `y` - y-axis. [0.0, 1.0]
 | ||||||
|     /// * `z` - z-axis. [0.0, 1.0]
 |     /// * `z` - z-axis. [0.0, 1.0]
 | ||||||
|     pub const fn rgb(x: f32, y: f32, z: f32) -> Self { |     pub const fn xyz(x: f32, y: f32, z: f32) -> Self { | ||||||
|         Self { |         Self { | ||||||
|             x, |             x, | ||||||
|             y, |             y, | ||||||
| @ -47,6 +47,21 @@ impl Xyza { | |||||||
|             alpha: 1.0, |             alpha: 1.0, | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     /// Return a copy of this color with the 'x' channel set to the given value.
 | ||||||
|  |     pub const fn with_x(self, x: f32) -> Self { | ||||||
|  |         Self { x, ..self } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     /// Return a copy of this color with the 'y' channel set to the given value.
 | ||||||
|  |     pub const fn with_y(self, y: f32) -> Self { | ||||||
|  |         Self { y, ..self } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     /// Return a copy of this color with the 'z' channel set to the given value.
 | ||||||
|  |     pub const fn with_z(self, z: f32) -> Self { | ||||||
|  |         Self { z, ..self } | ||||||
|  |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| impl Default for Xyza { | impl Default for Xyza { | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 Talin
						Talin