Add color weights to Grayscale node and improve luminance handling (#1015)

* Add weighted grayscale node

* Rename nodes, fix grayscale weighting, add luma calc options

* Fix tests

* Add Tint Option

* Improve (but not full fix) tint

---------

Co-authored-by: Keavon Chambers <keavon@keavon.com>
This commit is contained in:
isiko 2023-02-10 21:55:01 +01:00 committed by Keavon Chambers
parent d456640bb8
commit a709a772d5
7 changed files with 285 additions and 34 deletions

View file

@ -371,11 +371,14 @@ mod test {
use super::*;
#[ignore]
#[test]
fn map_node() {
// let array = &mut [Color::from_rgbaf32(1.0, 0.0, 0.0, 1.0).unwrap()];
GrayscaleNode.eval(Color::from_rgbf32_unchecked(1., 0., 0.));
/*let map = ForEachNode(MutWrapper(GrayscaleNode));
// LuminanceNode.eval(Color::from_rgbf32_unchecked(1., 0., 0.));
/*let map = ForEachNode(MutWrapper(LuminanceNode));
(&map).eval(array.iter_mut());
assert_eq!(array[0], Color::from_rgbaf32(0.33333334, 0.33333334, 0.33333334, 1.0).unwrap());*/
}

View file

@ -2,16 +2,48 @@ use super::Color;
use crate::Node;
use core::fmt::Debug;
use dyn_any::{DynAny, StaticType};
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, DynAny, specta::Type, Hash)]
pub enum LuminanceCalculation {
#[default]
SRGB,
Perceptual,
AverageChannels,
}
impl LuminanceCalculation {
pub fn list() -> [LuminanceCalculation; 3] {
[LuminanceCalculation::SRGB, LuminanceCalculation::Perceptual, LuminanceCalculation::AverageChannels]
}
}
impl std::fmt::Display for LuminanceCalculation {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
LuminanceCalculation::SRGB => write!(f, "sRGB"),
LuminanceCalculation::Perceptual => write!(f, "Perceptual"),
LuminanceCalculation::AverageChannels => write!(f, "Average Channels"),
}
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct GrayscaleNode;
pub struct LuminanceNode<LuminanceCalculation> {
luma_calculation: LuminanceCalculation,
}
#[node_macro::node_fn(GrayscaleNode)]
fn grayscale_color_node(color: Color) -> Color {
#[node_macro::node_fn(LuminanceNode)]
fn luminance_color_node(color: Color, luma_calculation: LuminanceCalculation) -> Color {
// TODO: Remove conversion to linear when the whole node graph uses linear color
let color = color.to_linear_srgb();
let luminance = color.luminance();
let luminance = match luma_calculation {
LuminanceCalculation::SRGB => color.luminance_srgb(),
LuminanceCalculation::Perceptual => color.luminance_perceptual(),
LuminanceCalculation::AverageChannels => color.average_rgb_channels(),
};
// TODO: Remove conversion to linear when the whole node graph uses linear color
let luminance = Color::linear_to_srgb(luminance);
@ -19,6 +51,51 @@ fn grayscale_color_node(color: Color) -> Color {
color.map_rgb(|_| luminance)
}
#[derive(Debug, Clone, Copy, Default)]
pub struct GrayscaleNode<Tint, Reds, Yellows, Greens, Cyans, Blues, Magentas> {
tint: Tint,
reds: Reds,
yellows: Yellows,
greens: Greens,
cyans: Cyans,
blues: Blues,
magentas: Magentas,
}
// From <https://stackoverflow.com/a/55233732/775283>
// Works the same for gamma and linear color
#[node_macro::node_fn(GrayscaleNode)]
fn grayscale_color_node(color: Color, tint: Color, reds: f64, yellows: f64, greens: f64, cyans: f64, blues: f64, magentas: f64) -> Color {
let reds = reds as f32 / 100.;
let yellows = yellows as f32 / 100.;
let greens = greens as f32 / 100.;
let cyans = cyans as f32 / 100.;
let blues = blues as f32 / 100.;
let magentas = magentas as f32 / 100.;
let gray_base = color.r().min(color.g()).min(color.b());
let red_part = color.r() - gray_base;
let green_part = color.g() - gray_base;
let blue_part = color.b() - gray_base;
let additional = if red_part == 0. {
let cyan_part = green_part.min(blue_part);
cyan_part * cyans + (green_part - cyan_part) * greens + (blue_part - cyan_part) * blues
} else if green_part == 0. {
let magenta_part = red_part.min(blue_part);
magenta_part * magentas + (red_part - magenta_part) * reds + (blue_part - magenta_part) * blues
} else {
let yellow_part = red_part.min(green_part);
yellow_part * yellows + (red_part - yellow_part) * reds + (green_part - yellow_part) * greens
};
let luminance = gray_base + additional;
// TODO: Fix "Color" blend mode implementation so it matches the expected behavior perfectly (it's currently close)
tint.with_luminance(luminance)
}
#[cfg(not(target_arch = "spirv"))]
pub use hue_shift::HueSaturationNode;
@ -54,18 +131,25 @@ fn invert_image(color: Color) -> Color {
}
#[derive(Debug, Clone, Copy)]
pub struct ThresholdNode<Threshold> {
pub struct ThresholdNode<LuminanceCalculation, Threshold> {
luma_calculation: LuminanceCalculation,
threshold: Threshold,
}
#[node_macro::node_fn(ThresholdNode)]
fn threshold_node(color: Color, threshold: f64) -> Color {
let threshold = Color::srgb_to_linear(threshold as f32);
fn threshold_node(color: Color, luma_calculation: LuminanceCalculation, threshold: f64) -> Color {
let threshold = Color::srgb_to_linear(threshold as f32 / 100.);
// TODO: Remove conversion to linear when the whole node graph uses linear color
let color = color.to_linear_srgb();
if color.luminance() >= threshold {
let luminance = match luma_calculation {
LuminanceCalculation::SRGB => color.luminance_srgb(),
LuminanceCalculation::Perceptual => color.luminance_perceptual(),
LuminanceCalculation::AverageChannels => color.average_rgb_channels(),
};
if luminance >= threshold {
Color::WHITE
} else {
Color::BLACK
@ -108,7 +192,7 @@ pub struct OpacityNode<O> {
#[node_macro::node_fn(OpacityNode)]
fn image_opacity(color: Color, opacity_multiplier: f64) -> Color {
let opacity_multiplier = opacity_multiplier as f32;
let opacity_multiplier = opacity_multiplier as f32 / 100.;
Color::from_rgbaf32_unchecked(color.r(), color.g(), color.b(), color.a() * opacity_multiplier)
}

View file

@ -196,14 +196,28 @@ impl Color {
self.alpha
}
// From https://stackoverflow.com/a/56678483/775283
pub fn luminance(&self) -> f32 {
0.2126 * self.red + 0.7152 * self.green + 0.0722 * self.blue
pub fn average_rgb_channels(&self) -> f32 {
(self.red + self.green + self.blue) / 3.
}
// From https://stackoverflow.com/a/56678483/775283
pub fn perceptual_luminance(&self) -> f32 {
let luminance = self.luminance();
pub fn luminance_srgb(&self) -> f32 {
0.2126 * self.red + 0.7152 * self.green + 0.0722 * self.blue
}
// From https://en.wikipedia.org/wiki/Luma_(video)#Rec._601_luma_versus_Rec._709_luma_coefficients
pub fn luminance_rec_601(&self) -> f32 {
0.299 * self.red + 0.587 * self.green + 0.114 * self.blue
}
// From https://en.wikipedia.org/wiki/Luma_(video)#Rec._601_luma_versus_Rec._709_luma_coefficients
pub fn luminance_rec_601_rounded(&self) -> f32 {
0.3 * self.red + 0.59 * self.green + 0.11 * self.blue
}
// From https://stackoverflow.com/a/56678483/775283
pub fn luminance_perceptual(&self) -> f32 {
let luminance = self.luminance_srgb();
if luminance <= 0.008856 {
(luminance * 903.3) / 100.
@ -212,6 +226,11 @@ impl Color {
}
}
pub fn with_luminance(&self, luminance: f32) -> Color {
let d = luminance - self.luminance_rec_601();
self.map_rgb(|c| (c + d).clamp(0., 1.))
}
/// Return the all components as a tuple, first component is red, followed by green, followed by blue, followed by alpha.
///
/// # Examples