mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-15 22:28:10 +08:00
Add "Blend" node (#1024)
* Add Blend node * Add more implementations Currently, known buggy implementations: * Color Burn * Saturation Opacity is currently achieved by linear interpolation, this will be changed as soon as all filters are implemented. * Add more implementations Currently, known incorrect implementations: * Color Burn * Saturation Not yet Tested: * Linear Burn * Linear Dodge * Vivid Light * Linear Light * Pin Light * Hard Mix * Subtract * Divide Opacity is currently achieved by linear interpolation, this will be changed as soon as all filters are implemented. * Cleanup * Removed Unused Code * Fixed Clamping Issue * Fixed Inverted Opacity * Moved Opacity Calculation from individual Blend Functions into 'blend_node' function * Fix 'Color Burn' blend mode Currently, known incorrect implementations: * Saturation Not yet Tested: * Linear Burn * Darker Color * Linear Dodge * Lighter Color * Vivid Light * Linear Light * Pin Light * Hard Mix * Subtract * Divide Opacity is currently achieved by linear interpolation, this will be changed as soon as all filters are implemented. * Fix 'Saturation' blend mode Currently, known incorrect implementations: * None :D Not yet Tested: * Linear Burn * Darker Color * Linear Dodge * Lighter Color * Vivid Light * Linear Light * Pin Light * Hard Mix * Subtract * Divide Opacity is currently achieved by linear interpolation, this will be changed as soon as all filters are implemented. * Final Cleanups * Add proper Inputs * cargo fmt * Add test for doubling number * Display implementation for ProtoNetwork * Switch top and bottom * Add input types for blend image node * Fix test --------- Co-authored-by: 0hypercube <0hypercube@gmail.com> Co-authored-by: Dennis Kobert <dennis@kobert.dev>
This commit is contained in:
@@ -39,6 +39,121 @@ impl std::fmt::Display for LuminanceCalculation {
|
||||
}
|
||||
}
|
||||
|
||||
impl BlendMode {
|
||||
pub fn list() -> [BlendMode; 26] {
|
||||
[
|
||||
BlendMode::Normal,
|
||||
BlendMode::Multiply,
|
||||
BlendMode::Darken,
|
||||
BlendMode::ColorBurn,
|
||||
BlendMode::LinearBurn,
|
||||
BlendMode::DarkerColor,
|
||||
BlendMode::Screen,
|
||||
BlendMode::Lighten,
|
||||
BlendMode::ColorDodge,
|
||||
BlendMode::LinearDodge,
|
||||
BlendMode::LighterColor,
|
||||
BlendMode::Overlay,
|
||||
BlendMode::SoftLight,
|
||||
BlendMode::HardLight,
|
||||
BlendMode::VividLight,
|
||||
BlendMode::LinearLight,
|
||||
BlendMode::PinLight,
|
||||
BlendMode::HardMix,
|
||||
BlendMode::Difference,
|
||||
BlendMode::Exclusion,
|
||||
BlendMode::Subtract,
|
||||
BlendMode::Divide,
|
||||
BlendMode::Hue,
|
||||
BlendMode::Saturation,
|
||||
BlendMode::Color,
|
||||
BlendMode::Luminosity,
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, DynAny, specta::Type, Hash)]
|
||||
pub enum BlendMode {
|
||||
#[default]
|
||||
// Basic group
|
||||
Normal,
|
||||
// Not supported by SVG, but we should someday support: Dissolve
|
||||
|
||||
// Darken group
|
||||
Multiply,
|
||||
Darken,
|
||||
ColorBurn,
|
||||
LinearBurn,
|
||||
DarkerColor,
|
||||
|
||||
// Lighten group
|
||||
Screen,
|
||||
Lighten,
|
||||
ColorDodge,
|
||||
LinearDodge,
|
||||
LighterColor,
|
||||
|
||||
// Contrast group
|
||||
Overlay,
|
||||
SoftLight,
|
||||
HardLight,
|
||||
VividLight,
|
||||
LinearLight,
|
||||
PinLight,
|
||||
HardMix,
|
||||
|
||||
// Inversion group
|
||||
Difference,
|
||||
Exclusion,
|
||||
Subtract,
|
||||
Divide,
|
||||
|
||||
// Component group
|
||||
Hue,
|
||||
Saturation,
|
||||
Color,
|
||||
Luminosity,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for BlendMode {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
BlendMode::Normal => write!(f, "Normal"),
|
||||
|
||||
BlendMode::Multiply => write!(f, "Multiply"),
|
||||
BlendMode::Darken => write!(f, "Darken"),
|
||||
BlendMode::ColorBurn => write!(f, "Color Burn"),
|
||||
BlendMode::LinearBurn => write!(f, "Linear Burn"),
|
||||
BlendMode::DarkerColor => write!(f, "Darker Color"),
|
||||
|
||||
BlendMode::Screen => write!(f, "Screen"),
|
||||
BlendMode::Lighten => write!(f, "Lighten"),
|
||||
BlendMode::ColorDodge => write!(f, "Color Dodge"),
|
||||
BlendMode::LinearDodge => write!(f, "Linear Dodge"),
|
||||
BlendMode::LighterColor => write!(f, "Lighter Color"),
|
||||
|
||||
BlendMode::Overlay => write!(f, "Overlay"),
|
||||
BlendMode::SoftLight => write!(f, "Soft Light"),
|
||||
BlendMode::HardLight => write!(f, "Hard Light"),
|
||||
BlendMode::VividLight => write!(f, "Vivid Light"),
|
||||
BlendMode::LinearLight => write!(f, "Linear Light"),
|
||||
BlendMode::PinLight => write!(f, "Pin Light"),
|
||||
BlendMode::HardMix => write!(f, "Hard Mix"),
|
||||
|
||||
BlendMode::Difference => write!(f, "Difference"),
|
||||
BlendMode::Exclusion => write!(f, "Exclusion"),
|
||||
BlendMode::Subtract => write!(f, "Subtract"),
|
||||
BlendMode::Divide => write!(f, "Divide"),
|
||||
|
||||
BlendMode::Hue => write!(f, "Hue"),
|
||||
BlendMode::Saturation => write!(f, "Saturation"),
|
||||
BlendMode::Color => write!(f, "Color"),
|
||||
BlendMode::Luminosity => write!(f, "Luminosity"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Default)]
|
||||
pub struct LuminanceNode<LuminanceCalculation> {
|
||||
luma_calculation: LuminanceCalculation,
|
||||
@@ -211,6 +326,52 @@ fn threshold_node(color: Color, luma_calculation: LuminanceCalculation, threshol
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct BlendNode<BlendMode, Opacity> {
|
||||
blend_mode: BlendMode,
|
||||
opacity: Opacity,
|
||||
}
|
||||
|
||||
#[node_macro::node_fn(BlendNode)]
|
||||
fn blend_node(input: (Color, Color), blend_mode: BlendMode, opacity: f64) -> Color {
|
||||
let (source_color, backdrop) = input;
|
||||
let actual_opacity = 1. - (opacity / 100.) as f32;
|
||||
return match blend_mode {
|
||||
BlendMode::Normal => backdrop.blend_rgb(source_color, Color::blend_normal),
|
||||
BlendMode::Multiply => backdrop.blend_rgb(source_color, Color::blend_multiply),
|
||||
BlendMode::Darken => backdrop.blend_rgb(source_color, Color::blend_darken),
|
||||
BlendMode::ColorBurn => backdrop.blend_rgb(source_color, Color::blend_color_burn),
|
||||
BlendMode::LinearBurn => backdrop.blend_rgb(source_color, Color::blend_linear_burn),
|
||||
BlendMode::DarkerColor => backdrop.blend_darker_color(source_color),
|
||||
|
||||
BlendMode::Screen => backdrop.blend_rgb(source_color, Color::blend_screen),
|
||||
BlendMode::Lighten => backdrop.blend_rgb(source_color, Color::blend_lighten),
|
||||
BlendMode::ColorDodge => backdrop.blend_rgb(source_color, Color::blend_color_dodge),
|
||||
BlendMode::LinearDodge => backdrop.blend_rgb(source_color, Color::blend_linear_dodge),
|
||||
BlendMode::LighterColor => backdrop.blend_lighter_color(source_color),
|
||||
|
||||
BlendMode::Overlay => source_color.blend_rgb(backdrop, Color::blend_hardlight),
|
||||
BlendMode::SoftLight => backdrop.blend_rgb(source_color, Color::blend_softlight),
|
||||
BlendMode::HardLight => backdrop.blend_rgb(source_color, Color::blend_hardlight),
|
||||
BlendMode::VividLight => backdrop.blend_rgb(source_color, Color::blend_vivid_light),
|
||||
BlendMode::LinearLight => backdrop.blend_rgb(source_color, Color::blend_linear_light),
|
||||
BlendMode::PinLight => backdrop.blend_rgb(source_color, Color::blend_pin_light),
|
||||
BlendMode::HardMix => backdrop.blend_rgb(source_color, Color::blend_hard_mix),
|
||||
|
||||
BlendMode::Difference => backdrop.blend_rgb(source_color, Color::blend_exclusion),
|
||||
BlendMode::Exclusion => backdrop.blend_rgb(source_color, Color::blend_exclusion),
|
||||
BlendMode::Subtract => backdrop.blend_rgb(source_color, Color::blend_subtract),
|
||||
BlendMode::Divide => backdrop.blend_rgb(source_color, Color::blend_divide),
|
||||
|
||||
BlendMode::Hue => backdrop.blend_hue(source_color),
|
||||
BlendMode::Saturation => backdrop.blend_saturation(source_color),
|
||||
BlendMode::Color => backdrop.blend_color(source_color),
|
||||
BlendMode::Luminosity => backdrop.blend_luminosity(source_color),
|
||||
}
|
||||
.lerp(backdrop, actual_opacity)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct VibranceNode<Vibrance> {
|
||||
vibrance: Vibrance,
|
||||
|
||||
@@ -239,6 +239,174 @@ impl Color {
|
||||
self.map_rgb(|c| (c + d).clamp(0., 1.))
|
||||
}
|
||||
|
||||
pub fn saturation(&self) -> f32 {
|
||||
let max = (self.red).max(self.green).max(self.blue);
|
||||
let min = (self.red).min(self.green).min(self.blue);
|
||||
|
||||
max - min
|
||||
}
|
||||
|
||||
pub fn with_saturation(&self, saturation: f32) -> Color {
|
||||
let [hue, _, lightness, alpha] = self.to_hsla();
|
||||
Color::from_hsla(hue, saturation, lightness, alpha)
|
||||
}
|
||||
|
||||
pub fn blend_normal(_c_b: f32, c_s: f32) -> f32 {
|
||||
c_s
|
||||
}
|
||||
|
||||
pub fn blend_multiply(c_b: f32, c_s: f32) -> f32 {
|
||||
c_s * c_b
|
||||
}
|
||||
|
||||
pub fn blend_darken(c_b: f32, c_s: f32) -> f32 {
|
||||
c_s.min(c_b)
|
||||
}
|
||||
|
||||
pub fn blend_color_burn(c_b: f32, c_s: f32) -> f32 {
|
||||
if c_b == 1. {
|
||||
1.
|
||||
} else if c_s == 0. {
|
||||
0.
|
||||
} else {
|
||||
1. - ((1. - c_b) / c_s).min(1.)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn blend_linear_burn(c_b: f32, c_s: f32) -> f32 {
|
||||
c_b + c_s - 1.
|
||||
}
|
||||
|
||||
pub fn blend_darker_color(&self, other: Color) -> Color {
|
||||
if self.average_rgb_channels() <= other.average_rgb_channels() {
|
||||
*self
|
||||
} else {
|
||||
other
|
||||
}
|
||||
}
|
||||
|
||||
pub fn blend_screen(c_b: f32, c_s: f32) -> f32 {
|
||||
1. - (1. - c_s) * (1. - c_b)
|
||||
}
|
||||
|
||||
pub fn blend_lighten(c_b: f32, c_s: f32) -> f32 {
|
||||
c_s.max(c_b)
|
||||
}
|
||||
|
||||
pub fn blend_color_dodge(c_b: f32, c_s: f32) -> f32 {
|
||||
if c_s == 1. {
|
||||
1.
|
||||
} else {
|
||||
(c_b / (1. - c_s)).min(1.)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn blend_linear_dodge(c_b: f32, c_s: f32) -> f32 {
|
||||
c_b + c_s
|
||||
}
|
||||
|
||||
pub fn blend_lighter_color(&self, other: Color) -> Color {
|
||||
if self.average_rgb_channels() >= other.average_rgb_channels() {
|
||||
*self
|
||||
} else {
|
||||
other
|
||||
}
|
||||
}
|
||||
|
||||
pub fn blend_softlight(c_b: f32, c_s: f32) -> f32 {
|
||||
if c_s <= 0.5 {
|
||||
c_b - (1. - 2. * c_s) * c_b * (1. - c_b)
|
||||
} else {
|
||||
let d: fn(f32) -> f32 = |x| if x <= 0.25 { ((16. * x - 12.) * x + 4.) * x } else { x.sqrt() };
|
||||
c_b + (2. * c_s - 1.) * (d(c_b) - c_b)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn blend_hardlight(c_b: f32, c_s: f32) -> f32 {
|
||||
if c_s <= 0.5 {
|
||||
Color::blend_multiply(2. * c_s, c_b)
|
||||
} else {
|
||||
Color::blend_screen(2. * c_s - 1., c_b)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn blend_vivid_light(c_b: f32, c_s: f32) -> f32 {
|
||||
if c_s <= 0.5 {
|
||||
Color::blend_color_burn(2. * c_s, c_b)
|
||||
} else {
|
||||
Color::blend_color_dodge(2. * c_s - 1., c_b)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn blend_linear_light(c_b: f32, c_s: f32) -> f32 {
|
||||
if c_s <= 0.5 {
|
||||
Color::blend_linear_burn(2. * c_s, c_b)
|
||||
} else {
|
||||
Color::blend_linear_dodge(2. * c_s - 1., c_b)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn blend_pin_light(c_b: f32, c_s: f32) -> f32 {
|
||||
if c_s <= 0.5 {
|
||||
Color::blend_darken(2. * c_s, c_b)
|
||||
} else {
|
||||
Color::blend_lighten(2. * c_s - 1., c_b)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn blend_hard_mix(c_b: f32, c_s: f32) -> f32 {
|
||||
if Color::blend_linear_light(c_b, c_s) < 0.5 {
|
||||
0.
|
||||
} else {
|
||||
1.
|
||||
}
|
||||
}
|
||||
|
||||
pub fn blend_difference(c_b: f32, c_s: f32) -> f32 {
|
||||
(c_b - c_s).abs()
|
||||
}
|
||||
|
||||
pub fn blend_exclusion(c_b: f32, c_s: f32) -> f32 {
|
||||
c_b + c_s - 2. * c_b * c_s
|
||||
}
|
||||
|
||||
pub fn blend_subtract(c_b: f32, c_s: f32) -> f32 {
|
||||
c_b - c_s
|
||||
}
|
||||
|
||||
pub fn blend_divide(c_b: f32, c_s: f32) -> f32 {
|
||||
if c_b == 0. {
|
||||
1.
|
||||
} else {
|
||||
c_b / c_s
|
||||
}
|
||||
}
|
||||
|
||||
pub fn blend_hue(&self, c_s: Color) -> Color {
|
||||
let sat_b = self.saturation();
|
||||
let lum_b = self.luminance_rec_601();
|
||||
c_s.with_saturation(sat_b).with_luminance(lum_b)
|
||||
}
|
||||
|
||||
pub fn blend_saturation(&self, c_s: Color) -> Color {
|
||||
let sat_s = c_s.saturation();
|
||||
let lum_b = self.luminance_rec_601();
|
||||
|
||||
self.with_saturation(sat_s).with_luminance(lum_b)
|
||||
}
|
||||
|
||||
pub fn blend_color(&self, c_s: Color) -> Color {
|
||||
let lum_b = self.luminance_rec_601();
|
||||
|
||||
c_s.with_luminance(lum_b)
|
||||
}
|
||||
|
||||
pub fn blend_luminosity(&self, c_s: Color) -> Color {
|
||||
let lum_s = c_s.luminance_rec_601();
|
||||
|
||||
self.with_luminance(lum_s)
|
||||
}
|
||||
|
||||
/// Return the all components as a tuple, first component is red, followed by green, followed by blue, followed by alpha.
|
||||
///
|
||||
/// # Examples
|
||||
@@ -425,6 +593,23 @@ impl Color {
|
||||
pub fn map_rgb<F: Fn(f32) -> f32>(&self, f: F) -> Self {
|
||||
Self::from_rgbaf32_unchecked(f(self.r()), f(self.g()), f(self.b()), self.a())
|
||||
}
|
||||
pub fn blend_rgb<F: Fn(f32, f32) -> f32>(&self, other: Color, f: F) -> Self {
|
||||
let color = Color {
|
||||
red: f(self.red, other.red),
|
||||
green: f(self.green, other.green),
|
||||
blue: f(self.blue, other.blue),
|
||||
alpha: self.alpha,
|
||||
};
|
||||
if *self == Color::RED {
|
||||
debug!("{} {} {} {}", color.red, color.green, color.blue, color.alpha);
|
||||
}
|
||||
Color {
|
||||
red: f(self.red, other.red).clamp(0., 1.),
|
||||
green: f(self.green, other.green).clamp(0., 1.),
|
||||
blue: f(self.blue, other.blue).clamp(0., 1.),
|
||||
alpha: self.alpha,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user