Add 'transfer' constructor to 'SRgb'; Add 'as_colour' and 'as_mut_colour' methods to 'Alpha'; Add 'from_rgb' constructor to 'SRgb' and 'OpRgb'; Fix 'SRgb::untransfer' not considering negative values; Update docs;

This commit is contained in:
Gabriel Bjørnager Jensen 2025-03-26 13:10:23 +01:00
parent 7f1a7cec20
commit d58ab5ebe3
6 changed files with 83 additions and 8 deletions

View file

@ -3,6 +3,14 @@
This is the changelog of [Polywave](https://crates.io/crates/polywave/).
See `README.md` for more information.
## 0.8.0
* Add `transfer` constructor to `SRgb`
* Add `as_colour` and `as_mut_colour` methods to `Alpha`
* Add `from_rgb` constructor to `SRgb` and `OpRgb`
* Fix `SRgb::untransfer` not considering negative values
* Update docs
## 0.7.0
* Add `map` method to our colours (except `Html`)

View file

@ -8,7 +8,7 @@
[package]
name = "polywave"
version = "0.7.0"
version = "0.8.0"
authors = ["Gabriel Bjørnager Jensen"]
edition = "2021"
rust-version = "1.82"

View file

@ -42,8 +42,22 @@ impl<T: BalancedColour> Alpha<T> {
Self { colour, alpha }
}
/// Retrieves a reference to the colour part.
#[inline]
#[must_use]
pub const fn as_colour(&self) -> &T {
&self.colour
}
/// Retrieves a mutable reference to the colour part.
#[inline]
#[must_use]
pub const fn as_mut_colour(&mut self) -> &mut T {
&mut self.colour
}
/// Detaches the colour from its alpha channel.
#[inline(always)]
#[inline]
#[must_use]
pub const fn detach(self) -> (T, T::Component) {
let alpha = self.alpha;

View file

@ -38,6 +38,15 @@ impl<T: Component> OpRgb<T> {
Self(colour)
}
/// Reinterprets a rwa RGB colour as opRGB.
///
/// The provided colour is *scaled* to fit the opRGB gamut.
#[inline(always)]
#[must_use]
pub const fn from_rgb(colour: Rgb<T>) -> Self {
Self(colour)
}
/// Maps the opRGB colour's channels.
#[inline]
#[must_use]

View file

@ -38,6 +38,15 @@ impl<T: Component> SRgb<T> {
Self(colour)
}
/// Reinterprets a raw RGB colour as sRGB.
///
/// The provided colour is *scaled* to fit the sRGB gamut.
#[inline(always)]
#[must_use]
pub const fn from_rgb(colour: Rgb<T>) -> Self {
Self(colour)
}
/// Maps the sRGB colour's channels.
#[inline]
#[must_use]
@ -74,21 +83,56 @@ macro_rules! impl_conversions {
($($tys:ty),+$(,)?) => {
$(
impl ::polywave::rgb::SRgb<$tys> {
/// Transfers a raw RGB value to perceptual RGB, scaling to the sRGB gamut.
///
/// The transfer is done as by the sRGB transfer function.
#[cfg(feature = "std")]
#[inline]
#[must_use]
pub fn transfer(colour: Rgb<$tys>) -> Self {
let colour = colour.map(|mut colour| {
let sign = colour;
colour = colour.abs();
colour = if colour > 0.003_130_800 {
colour.powf(const { 1.0 / 2.4 }) * 1.055 - 0.055
} else {
colour * 12.920
};
colour = colour.copysign(sign);
colour
});
Self::from_rgb(colour)
}
/// "Untransfers" the gamma-encoded sRGB.
///
/// sRGB channels are encoded using the *transfer* function.
/// This method is the inverse of this gamma function.
/// sRGB channels are encoded using the [*transfer*](https://en.wikipedia.org/wiki/SRGB#Transfer_function_(%22gamma%22)) function (see [`transfer`](Self::transfer)).
/// This method serves as the inverse of this function.
///
/// Note that the returned value is no longer sRGB as sRGB is strictly gamma-encoded.
#[cfg(feature = "std")]
#[inline]
#[must_use]
pub fn untransfer(self) -> Rgb<$tys> {
self.as_rgb().map(|colour| {
if colour > 0.040_450 {
self.as_rgb().map(|mut colour| {
let sign = colour;
colour = colour.abs();
colour = if colour > 0.040_450 {
((colour + 0.055) / 1.055).powf(2.4)
} else {
colour / 12.920
}
};
colour = colour.copysign(sign);
colour
})
}
}

View file

@ -18,7 +18,7 @@ macro_rules! def_named_colour {
"` <span style=\"aspect-ratio: 1 / 1; background-color: ",
::core::stringify!($html_name),
"; border-radius: calc(1em / 3); display: inline-block; height: 1em; text-align: center; vertical-align: middle;\"",
"></span> HTML named colour.",
"></span> HTML named colour.\n",
)]
pub const $rust_name: Self = $value;
)*