1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
use super::*; pub trait GetAlpha<C> { fn get_alpha(&self) -> f64; fn get_opacity(&self) -> f64; fn get_transparency(&self) -> f64; } pub trait SetAlpha<C> { fn set_alpha(&mut self, alpha: f64) -> &Self; fn set_opacity(&mut self, opacity: f64) -> &Self; fn set_transparency(&mut self, transparency: f64) -> &Self; } pub trait HasAlpha<C>: GetAlpha<C> + SetAlpha<C> { fn get_color(&self) -> C; fn set_color(&mut self, color: C) -> &Self; fn split(&self) -> (C, f64) where Self: Sized, { (self.get_color(), self.get_alpha()) } } pub trait AdjustAlpha<C>: Clone + SetAlpha<C> { fn alpha(&self, alpha: f64) -> Self { let mut color = self.clone(); color.set_alpha(alpha); color } fn opacity(&self, opacity: f64) -> Self { let mut color = self.clone(); color.set_opacity(opacity); color } fn transparency(&self, transparency: f64) -> Self { let mut color = self.clone(); color.set_transparency(transparency); color } } #[derive(Clone, Copy, Debug)] pub struct Alpha<C: ColorSpace> { color: C, alpha: f64, } impl<C: ColorSpace> Alpha<C> { pub fn new(color: C, alpha: f64) -> Self { Self { color, alpha } } } impl<C: ColorSpace> GetAlpha<C> for Alpha<C> { fn get_alpha(&self) -> f64 { self.alpha } fn get_opacity(&self) -> f64 { self.alpha * 100. } fn get_transparency(&self) -> f64 { (1. - self.alpha) * 100. } } impl<C: ColorSpace> SetAlpha<C> for Alpha<C> { fn set_alpha(&mut self, alpha: f64) -> &Self { self.alpha = alpha; self } fn set_opacity(&mut self, opacity: f64) -> &Self { self.alpha = utils::clamp(opacity, 0., 100.) / 100.; self } fn set_transparency(&mut self, transparency: f64) -> &Self { self.alpha = 1. - utils::clamp(transparency, 0., 100.) / 100.; self } } impl<C: ColorSpace> HasAlpha<C> for Alpha<C> { fn get_color(&self) -> C { self.color } fn set_color(&mut self, color: C) -> &Self { self.color = color; self } fn split(&self) -> (C, f64) { (self.color, self.alpha) } } impl GetAlpha<Self> for Color { fn get_alpha(&self) -> f64 { self.alpha } fn get_opacity(&self) -> f64 { self.alpha * 100. } fn get_transparency(&self) -> f64 { (1. - self.alpha) * 100. } } impl SetAlpha<Self> for Color { fn set_alpha(&mut self, alpha: f64) -> &Self { self.alpha = alpha; self } fn set_opacity(&mut self, opacity: f64) -> &Self { self.alpha = opacity / 100.; self } fn set_transparency(&mut self, transparency: f64) -> &Self { self.alpha = 1. - transparency / 100.; self } } impl HasAlpha<Self> for Color { fn get_color(&self) -> Self { *self } fn set_color(&mut self, color: Self) -> &Self { self.red = color.red; self.green = color.green; self.blue = color.blue; self } fn split(&self) -> (Self, f64) { (self.get_color(), self.get_alpha()) } } impl GetAlpha<RgbColor> for RgbaColor { fn get_alpha(&self) -> f64 { (self.alpha as f64) / 255.0 } fn get_opacity(&self) -> f64 { self.alpha as f64 / 255. * 100. } fn get_transparency(&self) -> f64 { (1. - self.alpha as f64 / 255.) * 100. } } impl SetAlpha<RgbColor> for RgbaColor { fn set_alpha(&mut self, alpha: f64) -> &Self { self.alpha = (alpha * 255.).round() as u8; self } fn set_opacity(&mut self, opacity: f64) -> &Self { self.alpha = (opacity / 100.).round() as u8; self } fn set_transparency(&mut self, transparency: f64) -> &Self { self.alpha = (1. - transparency / 100.).round() as u8; self } } impl HasAlpha<RgbColor> for RgbaColor { fn get_color(&self) -> RgbColor { self.clone().into() } fn set_color(&mut self, color: RgbColor) -> &Self { self.red = color.red; self.green = color.green; self.blue = color.blue; self } fn split(&self) -> (RgbColor, f64) { (self.get_color(), self.get_alpha()) } } impl From<Alpha<RgbColor>> for Color { fn from(from_color_with_alpha: Alpha<RgbColor>) -> Self { let mut color: Self = from_color_with_alpha.get_color().into_color(); *color.set_alpha(from_color_with_alpha.get_alpha()) } } impl From<Alpha<RgbColor>> for RgbaColor { fn from(from_color_with_alpha: Alpha<RgbColor>) -> Self { let mut color: Self = from_color_with_alpha.get_color().into_color(); *color.set_alpha(from_color_with_alpha.get_alpha()) } } #[cfg(test)] mod test { use super::super::*; #[test] fn new_rgba_from_rgb_with_alpha() { let RgbaColor { red, green, blue, alpha, } = Alpha::new(RgbColor::new(200, 200, 200), 0.50).into_color(); assert_eq!(red, 200); assert_eq!(green, 200); assert_eq!(blue, 200); assert_eq!(alpha, 128); } #[test] fn rgb_color_with_alpha() { let Alpha { color: RgbColor { red, green, blue }, alpha, } = Alpha::new(RgbColor::new(200, 200, 200), 0.5); assert_eq!(red, 200); assert_eq!(green, 200); assert_eq!(blue, 200); assert_eq!(alpha, 0.5); } }