diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b84354..0b832fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,8 @@ +# 0.8.49 + +* Added `.value()` getter on grayscale pixel types to avoid direct field access. + # 0.8.48 * Deprecated `alt::GRAY8`-`alt::GRAYA16` type aliases, because they will be moved in the next major version. diff --git a/Cargo.toml b/Cargo.toml index 78118b1..6ae91f0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rgb" -version = "0.8.48" +version = "0.8.49" authors = ["Kornel LesiƄski ", "James Forster "] include = ["src/**/*", "Cargo.toml", "README.md", "examples/*.rs", "LICENSE"] description = "`struct RGB/RGBA/etc.` for sharing pixels between crates + convenience methods for color manipulation.\nAllows no-copy high-level interoperability. Also adds common convenience methods and implements standard Rust traits to make `RGB`/`RGBA` pixels and slices first-class Rust objects." diff --git a/src/formats/gray.rs b/src/formats/gray.rs index 1a5bae9..0f97fb5 100644 --- a/src/formats/gray.rs +++ b/src/formats/gray.rs @@ -9,8 +9,34 @@ pub struct Gray_v08( pub T, ); +impl Gray_v08 { + /// Reads the `.0` field + /// + /// This function isn't necessary, but it is forwards-compatible with the next major version of the RGB crate. + pub fn value(self) -> T { + self.0 + } + + /// Add alpha component to this pixel + #[allow(deprecated)] + pub fn with_alpha(self, add_alpha_value: T) -> crate::formats::gray_alpha::GrayAlpha_v08 { + crate::formats::gray_alpha::GrayAlpha_v08(self.0, add_alpha_value) + } +} + #[cfg(feature = "unstable-experimental")] /// A `Grayscale` pixel (rgb crate v0.9) +/// +/// This is the new gray pixel type as opposed to the legacy gray type +/// (`rgb::Gray`) which is kept for backwards-compatibility. +/// +/// # Examples +/// +/// ``` +/// use rgb::Gray; +/// +/// let pixel: Gray = Gray { v: 0 }; +/// ``` #[allow(non_camel_case_types)] #[repr(C)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] @@ -33,6 +59,21 @@ impl core::ops::Deref for Gray_v08 { } } +#[cfg(feature = "unstable-experimental")] +impl Gray_v09 { + /// Reads the `.v` field + /// + /// This function isn't necessary, but it is forwards-compatible with the next major version of the RGB crate. + pub fn value(self) -> T { + self.v + } + + /// Add alpha component to this pixel + pub fn with_alpha(self, add_alpha_value: T) -> crate::formats::gray_a::GrayA { + crate::formats::gray_a::GrayA { v: self.v, a: add_alpha_value } + } +} + #[test] #[cfg(feature = "unstable-experimental")] fn swizzle() { diff --git a/src/formats/gray_a.rs b/src/formats/gray_a.rs index ac503f8..31543cd 100644 --- a/src/formats/gray_a.rs +++ b/src/formats/gray_a.rs @@ -11,3 +11,12 @@ pub struct GrayA { /// Alpha Component pub a: A, } + +impl GrayA { + /// Reads the `.v` field + /// + /// This function isn't necessary, but it is forwards-compatible with the next major version of the RGB crate. + pub fn value(self) -> T { + self.v + } +} diff --git a/src/formats/gray_alpha.rs b/src/formats/gray_alpha.rs index 13197e6..890ae8c 100644 --- a/src/formats/gray_alpha.rs +++ b/src/formats/gray_alpha.rs @@ -20,6 +20,15 @@ pub struct GrayAlpha_v08( pub A, ); +impl GrayAlpha_v08 { + /// Reads the `.0` field + /// + /// This function isn't necessary, but it is forwards-compatible with the next major version of the RGB crate. + pub fn value(self) -> T { + self.0 + } +} + impl Deref for GrayAlpha_v08 { type Target = GrayA; diff --git a/src/legacy/alt.rs b/src/legacy/alt.rs index a889ab2..ae6adcf 100644 --- a/src/legacy/alt.rs +++ b/src/legacy/alt.rs @@ -104,7 +104,7 @@ impl GrayAlpha { impl GrayAlpha { /// Create a new `GrayAlpha` with the new alpha value, but same gray value #[doc(hidden)] - #[deprecated(note = "use .with_alpha(a) instead")] + #[deprecated(note = "use .with_alpha(a) instead; this will become a getter in the future")] pub fn alpha(&self, a: A) -> Self { self.with_alpha(a) } diff --git a/src/legacy/internal/rgb.rs b/src/legacy/internal/rgb.rs index ecb7169..fb184bb 100644 --- a/src/legacy/internal/rgb.rs +++ b/src/legacy/internal/rgb.rs @@ -8,7 +8,6 @@ impl BGR { /// Convenience function for creating a new pixel /// Warning: The order of arguments is R,G,B #[deprecated(note = "This function has a misleading order of arguments. Use BGR{} literal instead")] - #[inline(always)] pub const fn new(r: T, g: T, b: T) -> Self { Self { b, g, r } } @@ -90,7 +89,7 @@ macro_rules! impl_rgb_to_alpha { impl $RGB { /// Convenience function for converting to RGBA #[doc(hidden)] - #[deprecated(note = "use .with_alpha(a) instead")] + #[deprecated(note = "use .with_alpha(a) instead; this will become a getter in the future")] pub fn alpha(&self, a: T) -> $RGBA { self.with_alpha(a) } @@ -108,7 +107,8 @@ macro_rules! impl_rgb_to_alpha { } /// Convenience function for converting to RGBA with alpha channel of a different type than type of the pixels - #[inline(always)] + #[inline(never)] + #[deprecated(note = "use .with_alpha(a) instead")] pub fn new_alpha(&self, a: A) -> $RGBA { $RGBA { r: self.r.clone(), diff --git a/src/legacy/internal/rgba.rs b/src/legacy/internal/rgba.rs index c23d5aa..d31bb98 100644 --- a/src/legacy/internal/rgba.rs +++ b/src/legacy/internal/rgba.rs @@ -245,7 +245,6 @@ impl RGBA { impl BGRA { /// Provide a mutable view of only RGB components (leaving out alpha). /// Useful to change color without changing opacity. - #[inline(always)] #[deprecated(note = "This function will change. Use bgr_mut()")] pub fn rgb_mut(&mut self) -> &mut BGR { unsafe { &mut *(self as *mut _ as *mut BGR) } @@ -306,7 +305,6 @@ impl BGRA { /// Copy RGB components out of the RGBA struct /// /// Note: you can use `.into()` to convert between other types - #[inline(always)] #[deprecated(note = "This function will change. Use bgr()")] pub fn rgb(&self) -> BGR { BGR {