|
1 | 1 | use crate::sys;
|
2 |
| -use std::convert::TryFrom; |
| 2 | +use std::convert::{AsMut, AsRef, TryFrom}; |
| 3 | +use std::hash::{Hash, Hasher}; |
3 | 4 | use std::mem::transmute;
|
| 5 | +use std::ops::{Deref, DerefMut}; |
4 | 6 |
|
5 | 7 | use crate::get_error;
|
6 | 8 |
|
@@ -41,15 +43,18 @@ impl Palette {
|
41 | 43 |
|
42 | 44 | /// Creates a palette from the provided colors
|
43 | 45 | #[doc(alias = "SDL_SetPaletteColors")]
|
44 |
| - pub fn with_colors(colors: &[Color]) -> Result<Self, String> { |
| 46 | + pub fn with_colors<C>(colors: &[C]) -> Result<Self, String> |
| 47 | + where |
| 48 | + C: Copy + Into<RColor>, |
| 49 | + { |
45 | 50 | let pal = Self::new(colors.len())?;
|
46 | 51 |
|
47 | 52 | // Already validated, so don't check again
|
48 | 53 | let ncolors = colors.len() as ::libc::c_int;
|
49 | 54 |
|
50 | 55 | let result = unsafe {
|
51 | 56 | let mut raw_colors: Vec<sys::SDL_Color> =
|
52 |
| - colors.iter().map(|color| color.raw()).collect(); |
| 57 | + colors.iter().map(|color| (*color).into().raw).collect(); |
53 | 58 |
|
54 | 59 | let pal_ptr = (&mut raw_colors[0]) as *mut sys::SDL_Color;
|
55 | 60 |
|
@@ -85,21 +90,23 @@ impl_raw_accessors!((Palette, *mut sys::SDL_Palette));
|
85 | 90 |
|
86 | 91 | #[test]
|
87 | 92 | fn create_palette() {
|
88 |
| - let colors: Vec<_> = (0..0xff).map(|u| Color::RGB(u, 0, 0xff - u)).collect(); |
| 93 | + let colors: Vec<_> = (0..0xff).map(|u| RColor::RGB(u, 0, 0xff - u)).collect(); |
89 | 94 |
|
90 | 95 | let palette = Palette::with_colors(&colors).unwrap();
|
91 | 96 |
|
92 | 97 | assert!(palette.len() == 255);
|
93 | 98 | }
|
94 | 99 |
|
95 | 100 | #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
|
| 101 | +#[deprecated(since = "0.35.3", note = "Users should instead use RColor")] |
96 | 102 | pub struct Color {
|
97 | 103 | pub r: u8,
|
98 | 104 | pub g: u8,
|
99 | 105 | pub b: u8,
|
100 | 106 | pub a: u8,
|
101 | 107 | }
|
102 | 108 |
|
| 109 | +#[allow(deprecated)] |
103 | 110 | impl Color {
|
104 | 111 | #[inline]
|
105 | 112 | #[allow(non_snake_case)]
|
@@ -163,30 +170,187 @@ impl Color {
|
163 | 170 | pub const CYAN: Color = Color::RGBA(0, 255, 255, 255);
|
164 | 171 | }
|
165 | 172 |
|
| 173 | +#[allow(deprecated)] |
166 | 174 | impl Into<sys::SDL_Color> for Color {
|
167 | 175 | fn into(self) -> sys::SDL_Color {
|
168 | 176 | self.raw()
|
169 | 177 | }
|
170 | 178 | }
|
171 | 179 |
|
| 180 | +#[allow(deprecated)] |
172 | 181 | impl From<sys::SDL_Color> for Color {
|
173 | 182 | fn from(raw: sys::SDL_Color) -> Color {
|
174 | 183 | Color::RGBA(raw.r, raw.g, raw.b, raw.a)
|
175 | 184 | }
|
176 | 185 | }
|
177 | 186 |
|
| 187 | +#[allow(deprecated)] |
178 | 188 | impl From<(u8, u8, u8)> for Color {
|
179 | 189 | fn from((r, g, b): (u8, u8, u8)) -> Color {
|
180 | 190 | Color::RGB(r, g, b)
|
181 | 191 | }
|
182 | 192 | }
|
183 | 193 |
|
| 194 | +#[allow(deprecated)] |
184 | 195 | impl From<(u8, u8, u8, u8)> for Color {
|
185 | 196 | fn from((r, g, b, a): (u8, u8, u8, u8)) -> Color {
|
186 | 197 | Color::RGBA(r, g, b, a)
|
187 | 198 | }
|
188 | 199 | }
|
189 | 200 |
|
| 201 | +#[allow(deprecated)] |
| 202 | +impl Into<RColor> for Color { |
| 203 | + fn into(self) -> RColor { |
| 204 | + RColor::RGBA(self.r, self.g, self.b, self.a) |
| 205 | + } |
| 206 | +} |
| 207 | + |
| 208 | +#[derive(Copy, Clone)] |
| 209 | +pub struct RColor { |
| 210 | + raw: sys::SDL_Color, |
| 211 | +} |
| 212 | + |
| 213 | +impl RColor { |
| 214 | + #[inline] |
| 215 | + #[allow(non_snake_case)] |
| 216 | + pub const fn RGB(r: u8, g: u8, b: u8) -> RColor { |
| 217 | + RColor { |
| 218 | + raw: sys::SDL_Color { r, g, b, a: 0xff }, |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + #[inline] |
| 223 | + #[allow(non_snake_case)] |
| 224 | + pub const fn RGBA(r: u8, g: u8, b: u8, a: u8) -> RColor { |
| 225 | + RColor { |
| 226 | + raw: sys::SDL_Color { r, g, b, a }, |
| 227 | + } |
| 228 | + } |
| 229 | + |
| 230 | + #[doc(alias = "SDL_MapRGBA")] |
| 231 | + pub fn to_u32(self, format: &PixelFormat) -> u32 { |
| 232 | + unsafe { sys::SDL_MapRGBA(format.raw, self.raw.r, self.raw.g, self.raw.b, self.raw.a) } |
| 233 | + } |
| 234 | + |
| 235 | + #[doc(alias = "SDL_GetRGBA")] |
| 236 | + pub fn from_u32(format: &PixelFormat, pixel: u32) -> RColor { |
| 237 | + let (mut r, mut g, mut b, mut a) = (0, 0, 0, 0); |
| 238 | + |
| 239 | + unsafe { sys::SDL_GetRGBA(pixel, format.raw, &mut r, &mut g, &mut b, &mut a) }; |
| 240 | + RColor::RGBA(r, g, b, a) |
| 241 | + } |
| 242 | + |
| 243 | + pub fn invert(self) -> RColor { |
| 244 | + RColor::RGBA( |
| 245 | + 255 - self.raw.r, |
| 246 | + 255 - self.raw.g, |
| 247 | + 255 - self.raw.b, |
| 248 | + 255 - self.raw.a, |
| 249 | + ) |
| 250 | + } |
| 251 | + |
| 252 | + #[inline] |
| 253 | + pub const fn rgb(self) -> (u8, u8, u8) { |
| 254 | + (self.raw.r, self.raw.g, self.raw.b) |
| 255 | + } |
| 256 | + |
| 257 | + #[inline] |
| 258 | + pub const fn rgba(self) -> (u8, u8, u8, u8) { |
| 259 | + (self.raw.r, self.raw.g, self.raw.b, self.raw.a) |
| 260 | + } |
| 261 | + |
| 262 | + #[inline] |
| 263 | + pub const fn raw(self) -> sys::SDL_Color { |
| 264 | + self.raw |
| 265 | + } |
| 266 | + |
| 267 | + pub const WHITE: RColor = RColor::RGBA(255, 255, 255, 255); |
| 268 | + pub const BLACK: RColor = RColor::RGBA(0, 0, 0, 255); |
| 269 | + pub const GRAY: RColor = RColor::RGBA(128, 128, 128, 255); |
| 270 | + pub const GREY: RColor = RColor::GRAY; |
| 271 | + pub const RED: RColor = RColor::RGBA(255, 0, 0, 255); |
| 272 | + pub const GREEN: RColor = RColor::RGBA(0, 255, 0, 255); |
| 273 | + pub const BLUE: RColor = RColor::RGBA(0, 0, 255, 255); |
| 274 | + pub const MAGENTA: RColor = RColor::RGBA(255, 0, 255, 255); |
| 275 | + pub const YELLOW: RColor = RColor::RGBA(255, 255, 0, 255); |
| 276 | + pub const CYAN: RColor = RColor::RGBA(0, 255, 255, 255); |
| 277 | +} |
| 278 | + |
| 279 | +impl ::std::fmt::Debug for RColor { |
| 280 | + fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> { |
| 281 | + return write!( |
| 282 | + fmt, |
| 283 | + "RColor {{ r: {}, g: {}, b: {}, a: {} }}", |
| 284 | + self.raw.r, self.raw.g, self.raw.b, self.raw.a |
| 285 | + ); |
| 286 | + } |
| 287 | +} |
| 288 | + |
| 289 | +impl PartialEq for RColor { |
| 290 | + fn eq(&self, other: &RColor) -> bool { |
| 291 | + self.raw.r == other.raw.r |
| 292 | + && self.raw.g == other.raw.g |
| 293 | + && self.raw.b == other.raw.b |
| 294 | + && self.raw.a == other.raw.a |
| 295 | + } |
| 296 | +} |
| 297 | + |
| 298 | +impl Eq for RColor {} |
| 299 | + |
| 300 | +impl Hash for RColor { |
| 301 | + fn hash<H: Hasher>(&self, state: &mut H) { |
| 302 | + self.raw.r.hash(state); |
| 303 | + self.raw.g.hash(state); |
| 304 | + self.raw.b.hash(state); |
| 305 | + self.raw.a.hash(state); |
| 306 | + } |
| 307 | +} |
| 308 | + |
| 309 | +impl Deref for RColor { |
| 310 | + type Target = sys::SDL_Color; |
| 311 | + |
| 312 | + fn deref(&self) -> &sys::SDL_Color { |
| 313 | + &self.raw |
| 314 | + } |
| 315 | +} |
| 316 | + |
| 317 | +impl DerefMut for RColor { |
| 318 | + fn deref_mut(&mut self) -> &mut sys::SDL_Color { |
| 319 | + &mut self.raw |
| 320 | + } |
| 321 | +} |
| 322 | + |
| 323 | +impl AsRef<sys::SDL_Color> for RColor { |
| 324 | + fn as_ref(&self) -> &sys::SDL_Color { |
| 325 | + &self.raw |
| 326 | + } |
| 327 | +} |
| 328 | + |
| 329 | +impl AsMut<sys::SDL_Color> for RColor { |
| 330 | + fn as_mut(&mut self) -> &mut sys::SDL_Color { |
| 331 | + &mut self.raw |
| 332 | + } |
| 333 | +} |
| 334 | + |
| 335 | +impl Into<sys::SDL_Color> for RColor { |
| 336 | + fn into(self) -> sys::SDL_Color { |
| 337 | + self.raw |
| 338 | + } |
| 339 | +} |
| 340 | + |
| 341 | +impl From<sys::SDL_Color> for RColor { |
| 342 | + fn from(raw: sys::SDL_Color) -> RColor { |
| 343 | + RColor { raw } |
| 344 | + } |
| 345 | +} |
| 346 | + |
| 347 | +#[allow(deprecated)] |
| 348 | +impl Into<Color> for RColor { |
| 349 | + fn into(self) -> Color { |
| 350 | + Color::RGBA(self.raw.r, self.raw.g, self.raw.b, self.raw.a) |
| 351 | + } |
| 352 | +} |
| 353 | + |
190 | 354 | pub struct PixelMasks {
|
191 | 355 | /// Bits per pixel; usually 15, 16, or 32
|
192 | 356 | pub bpp: u8,
|
|
0 commit comments