Skip to content

Commit

Permalink
Forward-compat for gray
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Aug 30, 2024
1 parent d6afb5b commit a9a1555
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rgb"
version = "0.8.48"
version = "0.8.49"
authors = ["Kornel Lesiński <kornel@geekhood.net>", "James Forster <james.forsterer@gmail.com>"]
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."
Expand Down
41 changes: 41 additions & 0 deletions src/formats/gray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,34 @@ pub struct Gray_v08<T>(
pub T,
);

impl<T: Copy> Gray_v08<T> {
/// 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<T> {
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<u8> = Gray { v: 0 };
/// ```
#[allow(non_camel_case_types)]
#[repr(C)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand All @@ -33,6 +59,21 @@ impl<T> core::ops::Deref for Gray_v08<T> {
}
}

#[cfg(feature = "unstable-experimental")]
impl<T: Copy> Gray_v09<T> {
/// 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<T> {
crate::formats::gray_a::GrayA { v: self.v, a: add_alpha_value }
}
}

#[test]
#[cfg(feature = "unstable-experimental")]
fn swizzle() {
Expand Down
9 changes: 9 additions & 0 deletions src/formats/gray_a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,12 @@ pub struct GrayA<T, A = T> {
/// Alpha Component
pub a: A,
}

impl<T: Copy> GrayA<T> {
/// 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
}
}
9 changes: 9 additions & 0 deletions src/formats/gray_alpha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ pub struct GrayAlpha_v08<T, A = T>(
pub A,
);

impl<T: Copy> GrayAlpha_v08<T> {
/// 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<T, A> Deref for GrayAlpha_v08<T, A> {
type Target = GrayA<T, A>;

Expand Down
2 changes: 1 addition & 1 deletion src/legacy/alt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl<T, A> GrayAlpha<T, A> {
impl<T: Copy, A: Clone> GrayAlpha<T, A> {
/// 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)
}
Expand Down
6 changes: 3 additions & 3 deletions src/legacy/internal/rgb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ impl<T> BGR<T> {
/// 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 }
}
Expand Down Expand Up @@ -90,7 +89,7 @@ macro_rules! impl_rgb_to_alpha {
impl<T: Clone> $RGB<T> {
/// 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<T> {
self.with_alpha(a)
}
Expand All @@ -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<A>(&self, a: A) -> $RGBA<T, A> {
$RGBA {
r: self.r.clone(),
Expand Down
2 changes: 0 additions & 2 deletions src/legacy/internal/rgba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ impl<T, A> RGBA<T, A> {
impl<T, A> BGRA<T, A> {
/// 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<T> {
unsafe { &mut *(self as *mut _ as *mut BGR<T>) }
Expand Down Expand Up @@ -306,7 +305,6 @@ impl<T: Clone, A> BGRA<T, A> {
/// 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<T> {
BGR {
Expand Down

0 comments on commit a9a1555

Please sign in to comment.