-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Replace the 'Luminance' node with a 'Desaturate' node with a better selection of desaturation methods #4529
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -35,24 +35,52 @@ use vector_types::Gradient; | |||||
| // https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#:~:text=%27clrL%27%20%3D%20Color%20Lookup | ||||||
| // https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#:~:text=Color%20Lookup%20(Photoshop%20CS6 | ||||||
|
|
||||||
| /// Conversion from a color to grayscale. | ||||||
| #[cfg_attr(feature = "wasm", derive(tsify::Tsify))] | ||||||
| #[cfg_attr(feature = "std", derive(dyn_any::DynAny))] | ||||||
| #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] | ||||||
| #[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Hash, node_macro::ChoiceType, bytemuck::NoUninit, BufferStruct, FromPrimitive, IntoPrimitive)] | ||||||
| #[widget(Dropdown)] | ||||||
| #[repr(u32)] | ||||||
| pub enum LuminanceCalculation { | ||||||
| pub enum DesaturateMethod { | ||||||
| /// Light level of the color, the Y (luminance) of Rec. 709, which weights the linear-light RGB channels by `0.2126, 0.7152, 0.0722`. | ||||||
| /// | ||||||
| /// Accessibility contrast ratios and SVG luminance masks use this. | ||||||
| #[default] | ||||||
| #[label("sRGB")] | ||||||
| SRGB, | ||||||
| Perceptual, | ||||||
| AverageChannels, | ||||||
| MinimumChannels, | ||||||
| MaximumChannels, | ||||||
| #[label("Luminance (Rec. 709)")] | ||||||
| #[cfg_attr(feature = "serde", serde(alias = "SRGB"))] | ||||||
| LuminanceRec709, | ||||||
| /// Light level approximation for the color, the Y′ (luma) of Rec. 709, which weights the gamma-encoded RGB channels by `0.2126, 0.7152, 0.0722`. | ||||||
| /// | ||||||
| /// CSS filter functions such as `grayscale()` use this. | ||||||
| #[label("Luma (Rec. 709)")] | ||||||
| LumaRec709, | ||||||
| /// Light level approximation for the color, the Y′ (luma) of Rec. 601, which weights the gamma-encoded RGB channels by `0.299, 0.587, 0.114`. | ||||||
| #[label("Luma (Rec. 601)")] | ||||||
| LumaRec601, | ||||||
| /// Perceptually uniform scale from black to white, the L (lightness) of OkLab. | ||||||
| #[label("Lightness (OkLab)")] | ||||||
| #[cfg_attr(feature = "serde", serde(alias = "Perceptual"))] | ||||||
| LightnessOkLab, | ||||||
| /// Mean of the three linear-light RGB channels. | ||||||
| #[menu_separator] | ||||||
| #[cfg_attr(feature = "serde", serde(alias = "AverageChannels"))] | ||||||
| ChannelsAverage, | ||||||
| /// Smallest of the three linear-light RGB channels. | ||||||
| #[cfg_attr(feature = "serde", serde(alias = "MinimumChannels"))] | ||||||
| ChannelsMinimum, | ||||||
| /// Largest of the three linear-light RGB channels, the V (value) of HSV. | ||||||
| #[cfg_attr(feature = "serde", serde(alias = "MaximumChannels"))] | ||||||
| ChannelsMaximum, | ||||||
| /// Midpoint of the largest and smallest gamma-encoded RGB channels, the L (lightness) of HSL. | ||||||
| /// | ||||||
| /// The classic "Desaturate" command of many image editors uses this. | ||||||
| #[label("Lightness (HSL)")] | ||||||
| LightnessHsl, | ||||||
| } | ||||||
|
|
||||||
| #[node_macro::node(category("Raster: Adjustment"), shader_node(PerPixelAdjust))] | ||||||
| fn luminance<T: Adjust<Color>>( | ||||||
| fn desaturate<T: Adjust<Color>>( | ||||||
| _: impl Ctx, | ||||||
| #[implementations( | ||||||
| Raster<CPU>, | ||||||
|
|
@@ -61,18 +89,38 @@ fn luminance<T: Adjust<Color>>( | |||||
| )] | ||||||
| #[gpu_image] | ||||||
| input: Item<T>, | ||||||
| luminance_calc: Item<LuminanceCalculation>, | ||||||
| method: Item<DesaturateMethod>, | ||||||
| ) -> Item<T> { | ||||||
| let mut input = input; | ||||||
| let luminance_calc = luminance_calc.into_element(); | ||||||
| let method = method.into_element(); | ||||||
|
|
||||||
| input.element_mut().adjust(|color| { | ||||||
| let luminance = match luminance_calc { | ||||||
| LuminanceCalculation::SRGB => color.luminance_rec_709(), | ||||||
| LuminanceCalculation::Perceptual => color.luminance_perceptual(), | ||||||
| LuminanceCalculation::AverageChannels => color.average_rgb_channels(), | ||||||
| LuminanceCalculation::MinimumChannels => color.minimum_rgb_channels(), | ||||||
| LuminanceCalculation::MaximumChannels => color.maximum_rgb_channels(), | ||||||
| // Gamma-encoded formulas are decoded as if they were a gray | ||||||
| let gamma = || color.to_gamma_srgb_channels(); | ||||||
| let luminance = match method { | ||||||
| DesaturateMethod::LuminanceRec709 => color.luminance_rec_709(), | ||||||
| DesaturateMethod::LumaRec709 => { | ||||||
| let [r, g, b, _] = gamma(); | ||||||
| srgb_to_linear(0.2126 * r + 0.7152 * g + 0.0722 * b) | ||||||
| } | ||||||
| DesaturateMethod::LumaRec601 => { | ||||||
| let [r, g, b, _] = gamma(); | ||||||
| srgb_to_linear(0.299 * r + 0.587 * g + 0.114 * b) | ||||||
| } | ||||||
| DesaturateMethod::LightnessOkLab => { | ||||||
| // A gray's OkLab lightness is the cube root of its linear value, so cubing gives the gray of equal lightness | ||||||
| let lightness = color.lightness_oklab(); | ||||||
| lightness * lightness * lightness | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer a
Suggested change
|
||||||
| } | ||||||
| DesaturateMethod::ChannelsAverage => color.average_rgb_channels(), | ||||||
| DesaturateMethod::ChannelsMinimum => color.minimum_rgb_channels(), | ||||||
| DesaturateMethod::ChannelsMaximum => color.maximum_rgb_channels(), | ||||||
| DesaturateMethod::LightnessHsl => { | ||||||
| // The transfer curve is monotonic, so the extremes are found first and only they are encoded | ||||||
| let max = linear_to_srgb(color.maximum_rgb_channels()); | ||||||
| let min = linear_to_srgb(color.minimum_rgb_channels()); | ||||||
| srgb_to_linear((max + min) / 2.) | ||||||
| } | ||||||
| }; | ||||||
| color.map_rgb(|_| luminance) | ||||||
| }); | ||||||
|
|
@@ -1401,11 +1449,11 @@ fn color_balance<T: Adjust<Color>>( | |||||
| #[cfg(feature = "std")] | ||||||
| mod _graphene_hash_impls { | ||||||
| use super::{ | ||||||
| AdjustmentChannel, CellularDistanceFunction, CellularReturnType, DomainWarpType, FractalType, LuminanceCalculation, NoiseType, RedGreenBlue, RedGreenBlueAlpha, RelativeAbsolute, | ||||||
| AdjustmentChannel, CellularDistanceFunction, CellularReturnType, DesaturateMethod, DomainWarpType, FractalType, NoiseType, RedGreenBlue, RedGreenBlueAlpha, RelativeAbsolute, | ||||||
| SelectiveColorChoice, TonalRange, | ||||||
| }; | ||||||
| graphene_hash::impl_via_hash!( | ||||||
| LuminanceCalculation, | ||||||
| DesaturateMethod, | ||||||
| RedGreenBlue, | ||||||
| RedGreenBlueAlpha, | ||||||
| NoiseType, | ||||||
|
|
||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.