Add Rgbf struct with conversions and arithmetic operations.

This commit is contained in:
Overlord committed 2026-10-19 23:17:05 +02:00
1 parent 635e212daa
commit 27d5ef042a
4 files changed
+167 -7

No files matched your search

+3 -1
View File
@@ -1,4 +1,4 @@
use super::Rgb;
use super::{ Rgb, Rgbf };
#[derive(Debug, Clone, Copy, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
@@ -11,4 +11,6 @@ pub struct Hsl {
impl Hsl
{
pub fn to_rgb(self) -> Rgb { todo!() }
pub fn to_rgbf(self) -> Rgbf { todo!() }
}
+11 -1
View File
@@ -1,4 +1,4 @@
use super::Rgb;
use super::{ Rgb, Rgbf };
#[derive(Debug, Clone, Copy, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
@@ -11,4 +11,14 @@ pub struct Hsv {
impl Hsv
{
pub fn to_rgb(self) -> Rgb { todo!() }
pub fn to_rgbf(self) -> Rgbf { todo!() }
}
/// Wraps a hue in degrees into `0..360`.
/// `rem_euclid` rounds tiny negative input up to exactly 360, which is the same angle as 0.
pub(super) fn normalize_hue(h: f32) -> f32
{
let h = h.rem_euclid(360.0);
if h >= 360.0 { 0.0 } else { h }
}
+3 -1
View File
@@ -1,4 +1,4 @@
use super::Rgb;
use super::{ Rgb, Rgbf };
use crate::smooth::Lerp;
#[derive(Debug, Clone, Copy, PartialEq, Default)]
@@ -13,6 +13,8 @@ impl LinearRgb
{
pub fn to_rgb(self) -> Rgb { todo!() }
pub fn to_rgbf(self) -> Rgbf { todo!() }
pub fn luminance(self) -> f32 { todo!() }
}
+150 -4
View File
@@ -1,4 +1,14 @@
use super::{ Rgb, Hsv, Hsl };
use std::ops::{ Add, Sub, Mul, Div };
use super::transfer;
use super::rgb::unit_to_u8;
use super::hsv::normalize_hue;
use super::{ Rgb, LinearRgb, Hsv, Hsl };
use crate::smooth::Lerp;
/// An sRGB colour with `f32` channels, nominally in `0..=1`.
/// Still gamma-compressed like `Rgb`; this is the space HSV and HSL are defined in.
/// Blend and average in `LinearRgb` instead.
#[derive(Debug, Clone, Copy, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Rgbf {
@@ -9,9 +19,145 @@ pub struct Rgbf {
impl Rgbf
{
pub fn to_rgb(self) -> Rgb { todo!() }
#[must_use]
pub const fn new(r: f32, g: f32, b: f32) -> Self { Self { r, g, b } }
pub fn to_hsv(self) -> Hsv { todo!() }
/// Rounds each channel to a byte, clamping to `0..=1` first.
/// NaN gives 0.
#[must_use]
pub fn to_rgb(self) -> Rgb { Rgb::new(unit_to_u8(self.r), unit_to_u8(self.g), unit_to_u8(self.b)) }
pub fn to_hsl(self) -> Hsl { todo!() }
/// Decodes the colour to linear light.
#[must_use]
pub fn to_linear(self) -> LinearRgb
{
LinearRgb {
r: transfer::srgb_to_linear(self.r),
g: transfer::srgb_to_linear(self.g),
b: transfer::srgb_to_linear(self.b),
}
}
/// Clamps each channel to `0..=1`.
#[must_use]
pub const fn clamp(self) -> Self
{
Self::new(self.r.clamp(0.0, 1.0), self.g.clamp(0.0, 1.0), self.b.clamp(0.0, 1.0))
}
#[must_use]
pub fn to_hsv(self) -> Hsv
{
let (h, max, min) = self.hue_max_min();
let s = if max <= f32::EPSILON { 0.0 } else { (max - min) / max };
Hsv { h, s, v: max }
}
#[must_use]
pub fn to_hsl(self) -> Hsl
{
let (h, max, min) = self.hue_max_min();
let delta = max - min;
let l = (max + min) / 2.0;
let s = if delta < f32::EPSILON { 0.0 } else { delta / (max + min).min(2.0 - max - min) };
Hsl { h, s, l }
}
/// The hue in degrees, plus the largest and smallest channel.
/// HSV and HSL share the hue; they only differ in how they turn max and min into the other two values.
fn hue_max_min(self) -> (f32, f32, f32)
{
let Self { r, g, b } = self;
let max = r.max(g).max(b);
let min = r.min(g).min(b);
let delta = max - min;
let h = if delta < f32::EPSILON { 0.0 }
else if max == r { 60.0 * ((g - b) / delta).rem_euclid(6.0) }
else if max == g { 60.0 * ((b - r) / delta + 2.0) }
else { 60.0 * ((r - g) / delta + 4.0) };
(normalize_hue(h), max, min)
}
}
impl Lerp for Rgbf
{
fn lerp(&self, to: &Self, t: f32) -> Self { *self + (*to - *self) * t }
}
//
impl Add for Rgbf
{
type Output = Self;
fn add(self, o: Self) -> Self { Self::new(self.r + o.r, self.g + o.g, self.b + o.b) }
}
impl Sub for Rgbf
{
type Output = Self;
fn sub(self, o: Self) -> Self { Self::new(self.r - o.r, self.g - o.g, self.b - o.b) }
}
impl Mul<f32> for Rgbf
{
type Output = Self;
fn mul(self, k: f32) -> Self { Self::new(self.r * k, self.g * k, self.b * k) }
}
impl Div<f32> for Rgbf
{
type Output = Self;
fn div(self, k: f32) -> Self { Self::new(self.r / k, self.g / k, self.b / k) }
}
//
impl From<[f32; 3]> for Rgbf
{
fn from([r, g, b]: [f32; 3]) -> Self { Rgbf::new(r, g, b) }
}
impl From<Rgbf> for [f32; 3]
{
fn from(c: Rgbf) -> Self { [c.r, c.g, c.b] }
}
impl From<Rgbf> for LinearRgb
{
fn from(c: Rgbf) -> Self { c.to_linear() }
}
impl From<Rgbf> for Hsv
{
fn from(c: Rgbf) -> Self { c.to_hsv() }
}
impl From<Rgbf> for Hsl
{
fn from(c: Rgbf) -> Self { c.to_hsl() }
}
impl From<LinearRgb> for Rgbf
{
fn from(c: LinearRgb) -> Self { c.to_rgbf() }
}
impl From<Hsv> for Rgbf
{
fn from(c: Hsv) -> Self { c.to_rgbf() }
}
impl From<Hsl> for Rgbf
{
fn from(c: Hsl) -> Self { c.to_rgbf() }
}