| #![allow(clippy::too_many_arguments)] |
|
|
| use crate::curve::CubicSplines; |
| use dyn_any::DynAny; |
| use graphene_core::Node; |
| use graphene_core::blending::BlendMode; |
| use graphene_core::color::Color; |
| use graphene_core::color::Pixel; |
| use graphene_core::context::Ctx; |
| use graphene_core::gradient::GradientStops; |
| use graphene_core::raster::image::Image; |
| use graphene_core::raster_types::{CPU, Raster, RasterDataTable}; |
| use graphene_core::registry::types::{Angle, Percentage, SignedPercentage}; |
| use std::cmp::Ordering; |
| use std::fmt::Debug; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #[derive(Debug, Default, Clone, Copy, Eq, PartialEq, DynAny, Hash, node_macro::ChoiceType, specta::Type, serde::Serialize, serde::Deserialize)] |
| #[widget(Dropdown)] |
| pub enum LuminanceCalculation { |
| #[default] |
| #[label("sRGB")] |
| SRGB, |
| Perceptual, |
| AverageChannels, |
| MinimumChannels, |
| MaximumChannels, |
| } |
|
|
| #[node_macro::node(category("Raster: Adjustment"))] |
| fn luminance<T: Adjust<Color>>( |
| _: impl Ctx, |
| #[implementations( |
| Color, |
| RasterDataTable<CPU>, |
| GradientStops, |
| )] |
| mut input: T, |
| luminance_calc: LuminanceCalculation, |
| ) -> T { |
| input.adjust(|color| { |
| let luminance = match luminance_calc { |
| LuminanceCalculation::SRGB => color.luminance_srgb(), |
| LuminanceCalculation::Perceptual => color.luminance_perceptual(), |
| LuminanceCalculation::AverageChannels => color.average_rgb_channels(), |
| LuminanceCalculation::MinimumChannels => color.minimum_rgb_channels(), |
| LuminanceCalculation::MaximumChannels => color.maximum_rgb_channels(), |
| }; |
| color.map_rgb(|_| luminance) |
| }); |
| input |
| } |
|
|
| #[node_macro::node(category("Raster: Channels"))] |
| fn extract_channel<T: Adjust<Color>>( |
| _: impl Ctx, |
| #[implementations( |
| Color, |
| RasterDataTable<CPU>, |
| GradientStops, |
| )] |
| mut input: T, |
| channel: RedGreenBlueAlpha, |
| ) -> T { |
| input.adjust(|color| { |
| let extracted_value = match channel { |
| RedGreenBlueAlpha::Red => color.r(), |
| RedGreenBlueAlpha::Green => color.g(), |
| RedGreenBlueAlpha::Blue => color.b(), |
| RedGreenBlueAlpha::Alpha => color.a(), |
| }; |
| color.map_rgb(|_| extracted_value).with_alpha(1.) |
| }); |
| input |
| } |
|
|
| #[node_macro::node(category("Raster: Channels"))] |
| fn make_opaque<T: Adjust<Color>>( |
| _: impl Ctx, |
| #[implementations( |
| Color, |
| RasterDataTable<CPU>, |
| GradientStops, |
| )] |
| mut input: T, |
| ) -> T { |
| input.adjust(|color| { |
| if color.a() == 0. { |
| return color.with_alpha(1.); |
| } |
| Color::from_rgbaf32(color.r() / color.a(), color.g() / color.a(), color.b() / color.a(), 1.).unwrap() |
| }); |
| input |
| } |
|
|
| |
| |
| |
| |
| |
| |
| #[node_macro::node(name("Brightness/Contrast"), category("Raster: Adjustment"), properties("brightness_contrast_properties"))] |
| fn brightness_contrast<T: Adjust<Color>>( |
| _: impl Ctx, |
| #[implementations( |
| Color, |
| RasterDataTable<CPU>, |
| GradientStops, |
| )] |
| mut input: T, |
| brightness: SignedPercentage, |
| contrast: SignedPercentage, |
| use_classic: bool, |
| ) -> T { |
| if use_classic { |
| let brightness = brightness as f32 / 255.; |
|
|
| let contrast = contrast as f32 / 100.; |
| let contrast = if contrast > 0. { (contrast * std::f32::consts::FRAC_PI_2 - 0.01).tan() } else { contrast }; |
|
|
| let offset = brightness * contrast + brightness - contrast / 2.; |
|
|
| input.adjust(|color| color.to_gamma_srgb().map_rgb(|c| (c + c * contrast + offset).clamp(0., 1.)).to_linear_srgb()); |
|
|
| return input; |
| } |
|
|
| const WINDOW_SIZE: usize = 1024; |
|
|
| |
| let brightness_is_negative = brightness < 0.; |
| |
| |
| |
| let brightness = (brightness.abs() / 100.).min(103. / 22. - 0.00001) as f32; |
| let brightness_curve_points = CubicSplines { |
| x: [0., 130. - brightness * 26., 233. - brightness * 48., 255.].map(|x| x / 255.), |
| y: [0., 130. + brightness * 51., 233. + brightness * 10., 255.].map(|x| x / 255.), |
| }; |
| let brightness_curve_solutions = brightness_curve_points.solve(); |
| let mut brightness_lut: [f32; WINDOW_SIZE] = std::array::from_fn(|i| { |
| let x = i as f32 / (WINDOW_SIZE as f32 - 1.); |
| brightness_curve_points.interpolate(x, &brightness_curve_solutions) |
| }); |
| |
| if brightness_is_negative { |
| brightness_lut = std::array::from_fn(|i| { |
| let mut x = i; |
| while x > 1 && brightness_lut[x] > i as f32 / WINDOW_SIZE as f32 { |
| x -= 1; |
| } |
| x as f32 / WINDOW_SIZE as f32 |
| }); |
| } |
|
|
| |
| |
| |
| |
| let contrast = contrast as f32 / 100.; |
| let contrast_curve_points = CubicSplines { |
| x: [0., 64., 192., 255.].map(|x| x / 255.), |
| y: [0., 64. - contrast * 30., 192. + contrast * 30., 255.].map(|x| x / 255.), |
| }; |
| let contrast_curve_solutions = contrast_curve_points.solve(); |
| let contrast_lut: [f32; WINDOW_SIZE] = std::array::from_fn(|i| { |
| let x = i as f32 / (WINDOW_SIZE as f32 - 1.); |
| contrast_curve_points.interpolate(x, &contrast_curve_solutions) |
| }); |
|
|
| |
| let combined_lut = brightness_lut.map(|brightness| { |
| let index_in_contrast_lut = (brightness * (contrast_lut.len() - 1) as f32).round() as usize; |
| contrast_lut[index_in_contrast_lut] |
| }); |
| let lut_max = (combined_lut.len() - 1) as f32; |
|
|
| input.adjust(|color| color.to_gamma_srgb().map_rgb(|c| combined_lut[(c * lut_max).round() as usize]).to_linear_srgb()); |
|
|
| input |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| #[node_macro::node(category("Raster: Adjustment"))] |
| fn levels<T: Adjust<Color>>( |
| _: impl Ctx, |
| #[implementations( |
| Color, |
| RasterDataTable<CPU>, |
| GradientStops, |
| )] |
| mut image: T, |
| #[default(0.)] shadows: Percentage, |
| #[default(50.)] midtones: Percentage, |
| #[default(100.)] highlights: Percentage, |
| #[default(0.)] output_minimums: Percentage, |
| #[default(100.)] output_maximums: Percentage, |
| ) -> T { |
| image.adjust(|color| { |
| let color = color.to_gamma_srgb(); |
|
|
| |
| let input_shadows = (shadows / 100.) as f32; |
| let input_midtones = (midtones / 100.) as f32; |
| let input_highlights = (highlights / 100.) as f32; |
|
|
| |
| let output_minimums = (output_minimums / 100.) as f32; |
| let output_maximums = (output_maximums / 100.) as f32; |
|
|
| |
| let midtones = output_minimums + (output_maximums - output_minimums) * input_midtones; |
|
|
| |
| let gamma = if midtones < 0.5 { |
| |
| let x = 1. - midtones * 2.; |
| |
| 1. + 9. * x |
| } else { |
| |
| let x = 1. - midtones; |
| |
| let x = x * 2.; |
| |
| x.max(0.01) |
| }; |
|
|
| |
| let highlights_minus_shadows = (input_highlights - input_shadows).clamp(f32::EPSILON, 1.); |
| let color = color.map_rgb(|c| ((c - input_shadows).max(0.) / highlights_minus_shadows).min(1.)); |
|
|
| |
| let color = color.gamma(gamma); |
|
|
| |
| let color = color.map_rgb(|c| c * (output_maximums - output_minimums) + output_minimums); |
|
|
| color.to_linear_srgb() |
| }); |
| image |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| #[node_macro::node(name("Black & White"), category("Raster: Adjustment"))] |
| async fn black_and_white<T: Adjust<Color>>( |
| _: impl Ctx, |
| #[implementations( |
| Color, |
| RasterDataTable<CPU>, |
| GradientStops, |
| )] |
| mut image: T, |
| #[default(Color::BLACK)] tint: Color, |
| #[default(40.)] |
| #[range((-200., 300.))] |
| reds: Percentage, |
| #[default(60.)] |
| #[range((-200., 300.))] |
| yellows: Percentage, |
| #[default(40.)] |
| #[range((-200., 300.))] |
| greens: Percentage, |
| #[default(60.)] |
| #[range((-200., 300.))] |
| cyans: Percentage, |
| #[default(20.)] |
| #[range((-200., 300.))] |
| blues: Percentage, |
| #[default(80.)] |
| #[range((-200., 300.))] |
| magentas: Percentage, |
| ) -> T { |
| image.adjust(|color| { |
| let color = color.to_gamma_srgb(); |
|
|
| 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 alpha_part = color.a(); |
|
|
| 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; |
|
|
| |
| let color = tint.with_luminance(luminance); |
|
|
| let color = Color::from_rgbaf32(color.r(), color.g(), color.b(), alpha_part).unwrap(); |
|
|
| color.to_linear_srgb() |
| }); |
| image |
| } |
|
|
| |
| |
| |
| #[node_macro::node(name("Hue/Saturation"), category("Raster: Adjustment"))] |
| async fn hue_saturation<T: Adjust<Color>>( |
| _: impl Ctx, |
| #[implementations( |
| Color, |
| RasterDataTable<CPU>, |
| GradientStops, |
| )] |
| mut input: T, |
| hue_shift: Angle, |
| saturation_shift: SignedPercentage, |
| lightness_shift: SignedPercentage, |
| ) -> T { |
| input.adjust(|color| { |
| let color = color.to_gamma_srgb(); |
|
|
| let [hue, saturation, lightness, alpha] = color.to_hsla(); |
|
|
| let color = Color::from_hsla( |
| (hue + hue_shift as f32 / 360.) % 1., |
| |
| (saturation + saturation_shift as f32 / 100.).clamp(0., 1.), |
| |
| (lightness + lightness_shift as f32 / 100.).clamp(0., 1.), |
| alpha, |
| ); |
|
|
| color.to_linear_srgb() |
| }); |
| input |
| } |
|
|
| |
| |
| #[node_macro::node(category("Raster: Adjustment"))] |
| async fn invert<T: Adjust<Color>>( |
| _: impl Ctx, |
| #[implementations( |
| Color, |
| RasterDataTable<CPU>, |
| GradientStops, |
| )] |
| mut input: T, |
| ) -> T { |
| input.adjust(|color| { |
| let color = color.to_gamma_srgb(); |
|
|
| let color = color.map_rgb(|c| color.a() - c); |
|
|
| color.to_linear_srgb() |
| }); |
| input |
| } |
|
|
| |
| |
| #[node_macro::node(category("Raster: Adjustment"))] |
| async fn threshold<T: Adjust<Color>>( |
| _: impl Ctx, |
| #[implementations( |
| Color, |
| RasterDataTable<CPU>, |
| GradientStops, |
| )] |
| mut image: T, |
| #[default(50.)] min_luminance: Percentage, |
| #[default(100.)] max_luminance: Percentage, |
| luminance_calc: LuminanceCalculation, |
| ) -> T { |
| image.adjust(|color| { |
| let min_luminance = Color::srgb_to_linear(min_luminance as f32 / 100.); |
| let max_luminance = Color::srgb_to_linear(max_luminance as f32 / 100.); |
|
|
| let luminance = match luminance_calc { |
| LuminanceCalculation::SRGB => color.luminance_srgb(), |
| LuminanceCalculation::Perceptual => color.luminance_perceptual(), |
| LuminanceCalculation::AverageChannels => color.average_rgb_channels(), |
| LuminanceCalculation::MinimumChannels => color.minimum_rgb_channels(), |
| LuminanceCalculation::MaximumChannels => color.maximum_rgb_channels(), |
| }; |
|
|
| if luminance >= min_luminance && luminance <= max_luminance { Color::WHITE } else { Color::BLACK } |
| }); |
| image |
| } |
|
|
| trait Blend<P: Pixel> { |
| fn blend(&self, under: &Self, blend_fn: impl Fn(P, P) -> P) -> Self; |
| } |
| impl Blend<Color> for Color { |
| fn blend(&self, under: &Self, blend_fn: impl Fn(Color, Color) -> Color) -> Self { |
| blend_fn(*self, *under) |
| } |
| } |
| impl Blend<Color> for Option<Color> { |
| fn blend(&self, under: &Self, blend_fn: impl Fn(Color, Color) -> Color) -> Self { |
| match (self, under) { |
| (Some(a), Some(b)) => Some(blend_fn(*a, *b)), |
| (a, None) => *a, |
| (None, b) => *b, |
| } |
| } |
| } |
| impl Blend<Color> for RasterDataTable<CPU> { |
| fn blend(&self, under: &Self, blend_fn: impl Fn(Color, Color) -> Color) -> Self { |
| let mut result_table = self.clone(); |
|
|
| for (over, under) in result_table.instance_mut_iter().zip(under.instance_ref_iter()) { |
| let data = over.instance.data.iter().zip(under.instance.data.iter()).map(|(a, b)| blend_fn(*a, *b)).collect(); |
|
|
| *over.instance = Raster::new_cpu(Image { |
| data, |
| width: over.instance.width, |
| height: over.instance.height, |
| base64_string: None, |
| }); |
| } |
|
|
| result_table |
| } |
| } |
| impl Blend<Color> for GradientStops { |
| fn blend(&self, under: &Self, blend_fn: impl Fn(Color, Color) -> Color) -> Self { |
| let mut combined_stops = self.iter().map(|(position, _)| position).chain(under.iter().map(|(position, _)| position)).collect::<Vec<_>>(); |
| combined_stops.dedup_by(|&mut a, &mut b| (a - b).abs() < 1e-6); |
| combined_stops.sort_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Equal)); |
|
|
| let stops = combined_stops |
| .into_iter() |
| .map(|&position| { |
| let over_color = self.evaluate(position); |
| let under_color = under.evaluate(position); |
| let color = blend_fn(over_color, under_color); |
| (position, color) |
| }) |
| .collect::<Vec<_>>(); |
|
|
| GradientStops::new(stops) |
| } |
| } |
|
|
| #[node_macro::node(category("Raster"))] |
| async fn blend<T: Blend<Color> + Send>( |
| _: impl Ctx, |
| #[implementations( |
| Color, |
| RasterDataTable<CPU>, |
| GradientStops, |
| )] |
| over: T, |
| #[expose] |
| #[implementations( |
| Color, |
| RasterDataTable<CPU>, |
| GradientStops, |
| )] |
| under: T, |
| blend_mode: BlendMode, |
| #[default(100.)] opacity: Percentage, |
| ) -> T { |
| over.blend(&under, |a, b| blend_colors(a, b, blend_mode, opacity / 100.)) |
| } |
|
|
| #[node_macro::node(category(""), skip_impl)] |
| fn blend_color_pair<BlendModeNode, OpacityNode>(input: (Color, Color), blend_mode: &'n BlendModeNode, opacity: &'n OpacityNode) -> Color |
| where |
| BlendModeNode: Node<'n, (), Output = BlendMode> + 'n, |
| OpacityNode: Node<'n, (), Output = Percentage> + 'n, |
| { |
| let blend_mode = blend_mode.eval(()); |
| let opacity = opacity.eval(()); |
| blend_colors(input.0, input.1, blend_mode, opacity / 100.) |
| } |
|
|
| pub fn apply_blend_mode(foreground: Color, background: Color, blend_mode: BlendMode) -> Color { |
| match blend_mode { |
| |
| BlendMode::Normal => background.blend_rgb(foreground, Color::blend_normal), |
| |
| BlendMode::Darken => background.blend_rgb(foreground, Color::blend_darken), |
| BlendMode::Multiply => background.blend_rgb(foreground, Color::blend_multiply), |
| BlendMode::ColorBurn => background.blend_rgb(foreground, Color::blend_color_burn), |
| BlendMode::LinearBurn => background.blend_rgb(foreground, Color::blend_linear_burn), |
| BlendMode::DarkerColor => background.blend_darker_color(foreground), |
| |
| BlendMode::Lighten => background.blend_rgb(foreground, Color::blend_lighten), |
| BlendMode::Screen => background.blend_rgb(foreground, Color::blend_screen), |
| BlendMode::ColorDodge => background.blend_rgb(foreground, Color::blend_color_dodge), |
| BlendMode::LinearDodge => background.blend_rgb(foreground, Color::blend_linear_dodge), |
| BlendMode::LighterColor => background.blend_lighter_color(foreground), |
| |
| BlendMode::Overlay => foreground.blend_rgb(background, Color::blend_hardlight), |
| BlendMode::SoftLight => background.blend_rgb(foreground, Color::blend_softlight), |
| BlendMode::HardLight => background.blend_rgb(foreground, Color::blend_hardlight), |
| BlendMode::VividLight => background.blend_rgb(foreground, Color::blend_vivid_light), |
| BlendMode::LinearLight => background.blend_rgb(foreground, Color::blend_linear_light), |
| BlendMode::PinLight => background.blend_rgb(foreground, Color::blend_pin_light), |
| BlendMode::HardMix => background.blend_rgb(foreground, Color::blend_hard_mix), |
| |
| BlendMode::Difference => background.blend_rgb(foreground, Color::blend_difference), |
| BlendMode::Exclusion => background.blend_rgb(foreground, Color::blend_exclusion), |
| BlendMode::Subtract => background.blend_rgb(foreground, Color::blend_subtract), |
| BlendMode::Divide => background.blend_rgb(foreground, Color::blend_divide), |
| |
| BlendMode::Hue => background.blend_hue(foreground), |
| BlendMode::Saturation => background.blend_saturation(foreground), |
| BlendMode::Color => background.blend_color(foreground), |
| BlendMode::Luminosity => background.blend_luminosity(foreground), |
| |
| _ => panic!("Used blend mode without alpha blend"), |
| } |
| } |
|
|
| trait Adjust<P> { |
| fn adjust(&mut self, map_fn: impl Fn(&P) -> P); |
| } |
| impl Adjust<Color> for Color { |
| fn adjust(&mut self, map_fn: impl Fn(&Color) -> Color) { |
| *self = map_fn(self); |
| } |
| } |
| impl Adjust<Color> for Option<Color> { |
| fn adjust(&mut self, map_fn: impl Fn(&Color) -> Color) { |
| if let Some(v) = self { |
| *v = map_fn(v) |
| } |
| } |
| } |
| impl Adjust<Color> for GradientStops { |
| fn adjust(&mut self, map_fn: impl Fn(&Color) -> Color) { |
| for (_pos, c) in self.iter_mut() { |
| *c = map_fn(c); |
| } |
| } |
| } |
| impl Adjust<Color> for RasterDataTable<CPU> { |
| fn adjust(&mut self, map_fn: impl Fn(&Color) -> Color) { |
| for instance in self.instance_mut_iter() { |
| for c in instance.instance.data_mut().data.iter_mut() { |
| *c = map_fn(c); |
| } |
| } |
| } |
| } |
|
|
| #[inline(always)] |
| pub fn blend_colors(foreground: Color, background: Color, blend_mode: BlendMode, opacity: f64) -> Color { |
| let target_color = match blend_mode { |
| |
| BlendMode::Erase => return background.alpha_subtract(foreground), |
| BlendMode::Restore => return background.alpha_add(foreground), |
| BlendMode::MultiplyAlpha => return background.alpha_multiply(foreground), |
| blend_mode => apply_blend_mode(foreground, background, blend_mode), |
| }; |
|
|
| background.alpha_blend(target_color.to_associated_alpha(opacity as f32)) |
| } |
|
|
| |
| |
| |
| #[node_macro::node(category("Raster: Adjustment"))] |
| async fn gradient_map<T: Adjust<Color>>( |
| _: impl Ctx, |
| #[implementations( |
| Color, |
| RasterDataTable<CPU>, |
| GradientStops, |
| )] |
| mut image: T, |
| gradient: GradientStops, |
| reverse: bool, |
| ) -> T { |
| image.adjust(|color| { |
| let intensity = color.luminance_srgb(); |
| let intensity = if reverse { 1. - intensity } else { intensity }; |
| gradient.evaluate(intensity as f64).to_linear_srgb() |
| }); |
|
|
| image |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #[node_macro::node(category("Raster: Adjustment"))] |
| async fn vibrance<T: Adjust<Color>>( |
| _: impl Ctx, |
| #[implementations( |
| Color, |
| RasterDataTable<CPU>, |
| GradientStops, |
| )] |
| mut image: T, |
| vibrance: SignedPercentage, |
| ) -> T { |
| image.adjust(|color| { |
| let vibrance = vibrance as f32 / 100.; |
| |
| |
| let slowed_vibrance = if vibrance >= 0. { vibrance } else { vibrance * 0.5 }; |
|
|
| let channel_max = color.r().max(color.g()).max(color.b()); |
| let channel_min = color.r().min(color.g()).min(color.b()); |
| let channel_difference = channel_max - channel_min; |
|
|
| let scale_multiplier = if channel_max == color.r() { |
| let green_blue_difference = (color.g() - color.b()).abs(); |
| let t = (green_blue_difference / channel_difference).min(1.); |
| t * 0.5 + 0.5 |
| } else { |
| 1. |
| }; |
| let scale = slowed_vibrance * scale_multiplier * (2. - channel_difference); |
| let channel_reduction = channel_min * scale; |
| let scale = 1. + scale * (1. - channel_difference); |
|
|
| let luminance_initial = color.to_linear_srgb().luminance_srgb(); |
| let altered_color = color.map_rgb(|c| c * scale - channel_reduction).to_linear_srgb(); |
| let luminance = altered_color.luminance_srgb(); |
| let altered_color = altered_color.map_rgb(|c| c * luminance_initial / luminance); |
|
|
| let channel_max = altered_color.r().max(altered_color.g()).max(altered_color.b()); |
| let altered_color = if Color::linear_to_srgb(channel_max) > 1. { |
| let scale = (1. - luminance) / (channel_max - luminance); |
| altered_color.map_rgb(|c| (c - luminance) * scale + luminance) |
| } else { |
| altered_color |
| }; |
| let altered_color = altered_color.to_gamma_srgb(); |
|
|
| if vibrance >= 0. { |
| altered_color |
| } else { |
| |
| let luminance = color.luminance_rec_601(); |
|
|
| |
| |
| let factor = -slowed_vibrance; |
| altered_color.map_rgb(|c| c * (1. - factor) + luminance * factor) |
| } |
| }); |
| image |
| } |
|
|
| |
| #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, DynAny, node_macro::ChoiceType, specta::Type, serde::Serialize, serde::Deserialize)] |
| #[widget(Radio)] |
| pub enum RedGreenBlue { |
| #[default] |
| Red, |
| Green, |
| Blue, |
| } |
|
|
| |
| #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, DynAny, node_macro::ChoiceType, specta::Type, serde::Serialize, serde::Deserialize)] |
| #[widget(Radio)] |
| pub enum RedGreenBlueAlpha { |
| #[default] |
| Red, |
| Green, |
| Blue, |
| Alpha, |
| } |
|
|
| |
| #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, DynAny, node_macro::ChoiceType, specta::Type, serde::Serialize, serde::Deserialize)] |
| #[widget(Dropdown)] |
| pub enum NoiseType { |
| #[default] |
| Perlin, |
| #[label("OpenSimplex2")] |
| OpenSimplex2, |
| #[label("OpenSimplex2S")] |
| OpenSimplex2S, |
| Cellular, |
| ValueCubic, |
| Value, |
| WhiteNoise, |
| } |
|
|
| #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, DynAny, node_macro::ChoiceType, specta::Type, serde::Serialize, serde::Deserialize)] |
| |
| pub enum FractalType { |
| #[default] |
| None, |
| #[label("Fractional Brownian Motion")] |
| FBm, |
| Ridged, |
| PingPong, |
| #[label("Progressive (Domain Warp Only)")] |
| DomainWarpProgressive, |
| #[label("Independent (Domain Warp Only)")] |
| DomainWarpIndependent, |
| } |
|
|
| |
| #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, DynAny, node_macro::ChoiceType, specta::Type, serde::Serialize, serde::Deserialize)] |
| pub enum CellularDistanceFunction { |
| #[default] |
| Euclidean, |
| #[label("Euclidean Squared (Faster)")] |
| EuclideanSq, |
| Manhattan, |
| Hybrid, |
| } |
|
|
| #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, DynAny, node_macro::ChoiceType, specta::Type, serde::Serialize, serde::Deserialize)] |
| pub enum CellularReturnType { |
| CellValue, |
| #[default] |
| #[label("Nearest (F1)")] |
| Nearest, |
| #[label("Next Nearest (F2)")] |
| NextNearest, |
| #[label("Average (F1 / 2 + F2 / 2)")] |
| Average, |
| #[label("Difference (F2 - F1)")] |
| Difference, |
| #[label("Product (F2 * F1 / 2)")] |
| Product, |
| #[label("Division (F1 / F2)")] |
| Division, |
| } |
|
|
| |
| #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, DynAny, node_macro::ChoiceType, specta::Type, serde::Serialize, serde::Deserialize)] |
| #[widget(Dropdown)] |
| pub enum DomainWarpType { |
| #[default] |
| None, |
| #[label("OpenSimplex2")] |
| OpenSimplex2, |
| #[label("OpenSimplex2 Reduced")] |
| OpenSimplex2Reduced, |
| BasicGrid, |
| } |
|
|
| |
| |
| |
| #[node_macro::node(category("Raster: Adjustment"), properties("channel_mixer_properties"))] |
| async fn channel_mixer<T: Adjust<Color>>( |
| _: impl Ctx, |
| #[implementations( |
| Color, |
| RasterDataTable<CPU>, |
| GradientStops, |
| )] |
| mut image: T, |
|
|
| monochrome: bool, |
|
|
| #[default(40.)] |
| #[name("Red")] |
| monochrome_r: f64, |
| #[default(40.)] |
| #[name("Green")] |
| monochrome_g: f64, |
| #[default(20.)] |
| #[name("Blue")] |
| monochrome_b: f64, |
| #[default(0.)] |
| #[name("Constant")] |
| monochrome_c: f64, |
|
|
| #[default(100.)] |
| #[name("(Red) Red")] |
| red_r: f64, |
| #[default(0.)] |
| #[name("(Red) Green")] |
| red_g: f64, |
| #[default(0.)] |
| #[name("(Red) Blue")] |
| red_b: f64, |
| #[default(0.)] |
| #[name("(Red) Constant")] |
| red_c: f64, |
|
|
| #[default(0.)] |
| #[name("(Green) Red")] |
| green_r: f64, |
| #[default(100.)] |
| #[name("(Green) Green")] |
| green_g: f64, |
| #[default(0.)] |
| #[name("(Green) Blue")] |
| green_b: f64, |
| #[default(0.)] |
| #[name("(Green) Constant")] |
| green_c: f64, |
|
|
| #[default(0.)] |
| #[name("(Blue) Red")] |
| blue_r: f64, |
| #[default(0.)] |
| #[name("(Blue) Green")] |
| blue_g: f64, |
| #[default(100.)] |
| #[name("(Blue) Blue")] |
| blue_b: f64, |
| #[default(0.)] |
| #[name("(Blue) Constant")] |
| blue_c: f64, |
|
|
| |
| _output_channel: RedGreenBlue, |
| ) -> T { |
| image.adjust(|color| { |
| let color = color.to_gamma_srgb(); |
|
|
| let (r, g, b, a) = color.components(); |
|
|
| let color = if monochrome { |
| let (monochrome_r, monochrome_g, monochrome_b, monochrome_c) = (monochrome_r as f32 / 100., monochrome_g as f32 / 100., monochrome_b as f32 / 100., monochrome_c as f32 / 100.); |
|
|
| let gray = (r * monochrome_r + g * monochrome_g + b * monochrome_b + monochrome_c).clamp(0., 1.); |
|
|
| Color::from_rgbaf32_unchecked(gray, gray, gray, a) |
| } else { |
| let (red_r, red_g, red_b, red_c) = (red_r as f32 / 100., red_g as f32 / 100., red_b as f32 / 100., red_c as f32 / 100.); |
| let (green_r, green_g, green_b, green_c) = (green_r as f32 / 100., green_g as f32 / 100., green_b as f32 / 100., green_c as f32 / 100.); |
| let (blue_r, blue_g, blue_b, blue_c) = (blue_r as f32 / 100., blue_g as f32 / 100., blue_b as f32 / 100., blue_c as f32 / 100.); |
|
|
| let red = (r * red_r + g * red_g + b * red_b + red_c).clamp(0., 1.); |
| let green = (r * green_r + g * green_g + b * green_b + green_c).clamp(0., 1.); |
| let blue = (r * blue_r + g * blue_g + b * blue_b + blue_c).clamp(0., 1.); |
|
|
| Color::from_rgbaf32_unchecked(red, green, blue, a) |
| }; |
|
|
| color.to_linear_srgb() |
| }); |
| image |
| } |
|
|
| #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, DynAny, node_macro::ChoiceType, specta::Type, serde::Serialize, serde::Deserialize)] |
| #[widget(Radio)] |
| pub enum RelativeAbsolute { |
| #[default] |
| Relative, |
| Absolute, |
| } |
|
|
| #[repr(C)] |
| #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, DynAny, node_macro::ChoiceType, specta::Type, serde::Serialize, serde::Deserialize)] |
| pub enum SelectiveColorChoice { |
| #[default] |
| Reds, |
| Yellows, |
| Greens, |
| Cyans, |
| Blues, |
| Magentas, |
|
|
| #[menu_separator] |
| Whites, |
| Neutrals, |
| Blacks, |
| } |
|
|
| |
| |
| |
| |
| |
| |
| #[node_macro::node(category("Raster: Adjustment"), properties("selective_color_properties"))] |
| async fn selective_color<T: Adjust<Color>>( |
| _: impl Ctx, |
| #[implementations( |
| Color, |
| RasterDataTable<CPU>, |
| GradientStops, |
| )] |
| mut image: T, |
|
|
| mode: RelativeAbsolute, |
|
|
| #[name("(Reds) Cyan")] r_c: f64, |
| #[name("(Reds) Magenta")] r_m: f64, |
| #[name("(Reds) Yellow")] r_y: f64, |
| #[name("(Reds) Black")] r_k: f64, |
|
|
| #[name("(Yellows) Cyan")] y_c: f64, |
| #[name("(Yellows) Magenta")] y_m: f64, |
| #[name("(Yellows) Yellow")] y_y: f64, |
| #[name("(Yellows) Black")] y_k: f64, |
|
|
| #[name("(Greens) Cyan")] g_c: f64, |
| #[name("(Greens) Magenta")] g_m: f64, |
| #[name("(Greens) Yellow")] g_y: f64, |
| #[name("(Greens) Black")] g_k: f64, |
|
|
| #[name("(Cyans) Cyan")] c_c: f64, |
| #[name("(Cyans) Magenta")] c_m: f64, |
| #[name("(Cyans) Yellow")] c_y: f64, |
| #[name("(Cyans) Black")] c_k: f64, |
|
|
| #[name("(Blues) Cyan")] b_c: f64, |
| #[name("(Blues) Magenta")] b_m: f64, |
| #[name("(Blues) Yellow")] b_y: f64, |
| #[name("(Blues) Black")] b_k: f64, |
|
|
| #[name("(Magentas) Cyan")] m_c: f64, |
| #[name("(Magentas) Magenta")] m_m: f64, |
| #[name("(Magentas) Yellow")] m_y: f64, |
| #[name("(Magentas) Black")] m_k: f64, |
|
|
| #[name("(Whites) Cyan")] w_c: f64, |
| #[name("(Whites) Magenta")] w_m: f64, |
| #[name("(Whites) Yellow")] w_y: f64, |
| #[name("(Whites) Black")] w_k: f64, |
|
|
| #[name("(Neutrals) Cyan")] n_c: f64, |
| #[name("(Neutrals) Magenta")] n_m: f64, |
| #[name("(Neutrals) Yellow")] n_y: f64, |
| #[name("(Neutrals) Black")] n_k: f64, |
|
|
| #[name("(Blacks) Cyan")] k_c: f64, |
| #[name("(Blacks) Magenta")] k_m: f64, |
| #[name("(Blacks) Yellow")] k_y: f64, |
| #[name("(Blacks) Black")] k_k: f64, |
|
|
| _colors: SelectiveColorChoice, |
| ) -> T { |
| image.adjust(|color| { |
| let color = color.to_gamma_srgb(); |
|
|
| let (r, g, b, a) = color.components(); |
|
|
| let min = |a: f32, b: f32, c: f32| a.min(b).min(c); |
| let max = |a: f32, b: f32, c: f32| a.max(b).max(c); |
| let med = |a: f32, b: f32, c: f32| a + b + c - min(a, b, c) - max(a, b, c); |
|
|
| let max_channel = max(r, g, b); |
| let min_channel = min(r, g, b); |
|
|
| let pixel_color_range = |choice| match choice { |
| SelectiveColorChoice::Reds => max_channel == r, |
| SelectiveColorChoice::Yellows => min_channel == b, |
| SelectiveColorChoice::Greens => max_channel == g, |
| SelectiveColorChoice::Cyans => min_channel == r, |
| SelectiveColorChoice::Blues => max_channel == b, |
| SelectiveColorChoice::Magentas => min_channel == g, |
| SelectiveColorChoice::Whites => r > 0.5 && g > 0.5 && b > 0.5, |
| SelectiveColorChoice::Neutrals => r > 0. && g > 0. && b > 0. && r < 1. && g < 1. && b < 1., |
| SelectiveColorChoice::Blacks => r < 0.5 && g < 0.5 && b < 0.5, |
| }; |
|
|
| let color_parameter_group_scale_factor_rgb = max(r, g, b) - med(r, g, b); |
| let color_parameter_group_scale_factor_cmy = med(r, g, b) - min(r, g, b); |
|
|
| |
| let (slope_r, slope_g, slope_b) = match mode { |
| RelativeAbsolute::Relative => (r - 1., g - 1., b - 1.), |
| RelativeAbsolute::Absolute => (-1., -1., -1.), |
| }; |
|
|
| let (sum_r, sum_g, sum_b) = [ |
| (SelectiveColorChoice::Reds, (r_c as f32, r_m as f32, r_y as f32, r_k as f32)), |
| (SelectiveColorChoice::Yellows, (y_c as f32, y_m as f32, y_y as f32, y_k as f32)), |
| (SelectiveColorChoice::Greens, (g_c as f32, g_m as f32, g_y as f32, g_k as f32)), |
| (SelectiveColorChoice::Cyans, (c_c as f32, c_m as f32, c_y as f32, c_k as f32)), |
| (SelectiveColorChoice::Blues, (b_c as f32, b_m as f32, b_y as f32, b_k as f32)), |
| (SelectiveColorChoice::Magentas, (m_c as f32, m_m as f32, m_y as f32, m_k as f32)), |
| (SelectiveColorChoice::Whites, (w_c as f32, w_m as f32, w_y as f32, w_k as f32)), |
| (SelectiveColorChoice::Neutrals, (n_c as f32, n_m as f32, n_y as f32, n_k as f32)), |
| (SelectiveColorChoice::Blacks, (k_c as f32, k_m as f32, k_y as f32, k_k as f32)), |
| ] |
| .into_iter() |
| .fold((0., 0., 0.), |acc, (color_parameter_group, (c, m, y, k))| { |
| |
| |
| |
| if (c < f32::EPSILON && m < f32::EPSILON && y < f32::EPSILON && k < f32::EPSILON) || (!pixel_color_range(color_parameter_group)) { |
| return acc; |
| } |
|
|
| let (c, m, y, k) = (c / 100., m / 100., y / 100., k / 100.); |
|
|
| let color_parameter_group_scale_factor = match color_parameter_group { |
| SelectiveColorChoice::Reds | SelectiveColorChoice::Greens | SelectiveColorChoice::Blues => color_parameter_group_scale_factor_rgb, |
| SelectiveColorChoice::Cyans | SelectiveColorChoice::Magentas | SelectiveColorChoice::Yellows => color_parameter_group_scale_factor_cmy, |
| SelectiveColorChoice::Whites => min(r, g, b) * 2. - 1., |
| SelectiveColorChoice::Neutrals => 1. - ((max(r, g, b) - 0.5).abs() + (min(r, g, b) - 0.5).abs()), |
| SelectiveColorChoice::Blacks => 1. - max(r, g, b) * 2., |
| }; |
|
|
| let offset_r = ((c + k * (c + 1.)) * slope_r).clamp(-r, -r + 1.) * color_parameter_group_scale_factor; |
| let offset_g = ((m + k * (m + 1.)) * slope_g).clamp(-g, -g + 1.) * color_parameter_group_scale_factor; |
| let offset_b = ((y + k * (y + 1.)) * slope_b).clamp(-b, -b + 1.) * color_parameter_group_scale_factor; |
|
|
| (acc.0 + offset_r, acc.1 + offset_g, acc.2 + offset_b) |
| }); |
|
|
| let color = Color::from_rgbaf32_unchecked((r + sum_r).clamp(0., 1.), (g + sum_g).clamp(0., 1.), (b + sum_b).clamp(0., 1.), a); |
|
|
| color.to_linear_srgb() |
| }); |
| image |
| } |
|
|
| |
| |
| |
| |
| |
| |
| #[node_macro::node(category("Raster: Adjustment"))] |
| async fn posterize<T: Adjust<Color>>( |
| _: impl Ctx, |
| #[implementations( |
| Color, |
| RasterDataTable<CPU>, |
| GradientStops, |
| )] |
| mut input: T, |
| #[default(4)] |
| #[hard_min(2.)] |
| levels: u32, |
| ) -> T { |
| input.adjust(|color| { |
| let color = color.to_gamma_srgb(); |
|
|
| let levels = levels as f32; |
| let number_of_areas = levels.recip(); |
| let size_of_areas = (levels - 1.).recip(); |
| let channel = |channel: f32| (channel / number_of_areas).floor() * size_of_areas; |
| let color = color.map_rgb(channel); |
|
|
| color.to_linear_srgb() |
| }); |
| input |
| } |
|
|
| |
| |
| |
| |
| |
| |
| #[node_macro::node(category("Raster: Adjustment"), properties("exposure_properties"))] |
| async fn exposure<T: Adjust<Color>>( |
| _: impl Ctx, |
| #[implementations( |
| Color, |
| RasterDataTable<CPU>, |
| GradientStops, |
| )] |
| mut input: T, |
| exposure: f64, |
| offset: f64, |
| #[default(1.)] |
| #[range((0.01, 10.))] |
| #[hard_min(0.0001)] |
| gamma_correction: f64, |
| ) -> T { |
| input.adjust(|color| { |
| let adjusted = color |
| |
| .map_rgb(|c: f32| c * 2_f32.powf(exposure as f32)) |
| |
| .map_rgb(|c: f32| c + offset as f32) |
| |
| .gamma(gamma_correction as f32); |
|
|
| adjusted.map_rgb(|c: f32| c.clamp(0., 1.)) |
| }); |
| input |
| } |
|
|
| #[node_macro::node(category("Raster: Adjustment"))] |
| fn color_overlay<T: Adjust<Color>>( |
| _: impl Ctx, |
| #[implementations( |
| Color, |
| RasterDataTable<CPU>, |
| GradientStops, |
| )] |
| mut image: T, |
| #[default(Color::BLACK)] color: Color, |
| blend_mode: BlendMode, |
| #[default(100.)] opacity: Percentage, |
| ) -> T { |
| let opacity = (opacity as f32 / 100.).clamp(0., 1.); |
|
|
| image.adjust(|pixel| { |
| let image = pixel.map_rgb(|channel| channel * (1. - opacity)); |
|
|
| |
| let associated_pixel = Color::from_rgbaf32_unchecked(pixel.r() * pixel.a(), pixel.g() * pixel.a(), pixel.b() * pixel.a(), pixel.a()); |
| let overlay = apply_blend_mode(color, associated_pixel, blend_mode).map_rgb(|channel| channel * opacity); |
|
|
| Color::from_rgbaf32_unchecked(image.r() + overlay.r(), image.g() + overlay.g(), image.b() + overlay.b(), pixel.a()) |
| }); |
| image |
| } |
|
|
| |
|
|
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #[cfg(test)] |
| mod test { |
| use graphene_core::blending::BlendMode; |
| use graphene_core::color::Color; |
| use graphene_core::raster::image::Image; |
| use graphene_core::raster_types::{Raster, RasterDataTable}; |
|
|
| #[tokio::test] |
| async fn color_overlay_multiply() { |
| let image_color = Color::from_rgbaf32_unchecked(0.7, 0.6, 0.5, 0.4); |
| let image = Image::new(1, 1, image_color); |
|
|
| |
| let overlay_color = Color::GREEN; |
|
|
| |
| let opacity = 100_f64; |
|
|
| let result = super::color_overlay((), RasterDataTable::new(Raster::new_cpu(image.clone())), overlay_color, BlendMode::Multiply, opacity); |
| let result = result.instance_ref_iter().next().unwrap().instance; |
|
|
| |
| assert_eq!(result.data[0], Color::from_rgbaf32_unchecked(0., image_color.g(), 0., image_color.a())); |
| } |
| } |
|
|