Skip to content

feat: add saturation parameter to Color Correct layer - #3641

Merged
Svarov-RZM merged 9 commits into
synfig:masterfrom
aviralgarg05:feature/add-saturation-parameter
Jan 25, 2026
Merged

Svarov-RZM merged 9 commits into
synfig:masterfrom
aviralgarg05:feature/add-saturation-parameter

Conversation

@aviralgarg05

Copy link
Copy Markdown
Contributor
  • Added param_saturation member variable to Layer_ColorCorrect class
  • Initialized saturation parameter to 1.0 (no change) in constructor
  • Implemented saturation import/export in set_param and get_param methods
  • Added saturation to parameter vocabulary with user-friendly name
  • Integrated saturation adjustment into ColorMatrix transformation chain
  • Saturation uses ColorMatrix::set_saturation() via YUV color space conversion

This addresses issue #3634 by allowing independent control of color saturation without affecting brightness or contrast. The parameter works as follows:

  • 0.0: Grayscale (no saturation)
  • 1.0: Original saturation (default)
  • 1.0: Enhanced saturation

The saturation parameter works independently from other color adjustments and is positioned in the transform chain after hue but before exposure, brightness, and contrast for optimal color processing.

@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello @aviralgarg05, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the Color Correct layer by adding a dedicated saturation parameter. This new control allows users to precisely adjust the vibrancy of colors in an image without affecting other properties like brightness or contrast, providing more flexible and powerful color manipulation capabilities.

Highlights

  • New Saturation Parameter: Introduced a new saturation parameter to the Color Correct layer, allowing independent control over color intensity.
  • Default Value and Range: The saturation parameter is initialized to 1.0 (original saturation) by default, with 0.0 resulting in grayscale and values greater than 1.0 enhancing saturation.
  • Integration into ColorMatrix: The saturation adjustment is integrated into the ColorMatrix transformation chain, specifically using ColorMatrix::set_saturation() via YUV color space conversion.
  • Parameter Ordering: The saturation adjustment is positioned in the transform chain after hue but before exposure, brightness, and contrast for optimal color processing.
  • Issue Addressed: This change directly addresses issue Filter layer: Add parameter to control saturation #3634, fulfilling a request for independent saturation control.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new saturation parameter to the Color Correct layer. The changes correctly add the new param_saturation member, initialize it, and integrate it into the parameter import/export methods and the parameter vocabulary. The saturation adjustment is also correctly applied in the accelerated rendering path within build_rendering_task_vfunc. However, a significant issue is that the non-accelerated rendering path, handled by the correct_color method, has not been updated to use the new saturation parameter. This will lead to inconsistent behavior between the final render and other UI elements like the color picker. This should be fixed to ensure consistent color correction across the application.

aviralgarg05 added a commit to aviralgarg05/synfig that referenced this pull request Nov 26, 2025
Added saturation adjustment to the correct_color function to ensure
consistency between accelerated and non-accelerated rendering paths.

The saturation parameter is now properly applied in both:
- Accelerated path: via ColorMatrix::set_saturation() in build_rendering_task_vfunc
- Non-accelerated path: via Color::set_s() in correct_color function

The saturation adjustment is applied after brightness/contrast but before
hue rotation, maintaining consistency with the transformation order in the
accelerated rendering pipeline. This ensures that color pickers and other
non-accelerated UI elements show the same saturation effect as the final
rendered output.

Addresses code review feedback on PR synfig#3641.
@Svarov-RZM

Copy link
Copy Markdown
Contributor

Hey @aviralgarg05
Thanks for addressing my issue! I'll take it for a test on weekends and report back.

@Svarov-RZM Svarov-RZM self-assigned this Nov 27, 2025
@rodolforg

rodolforg commented Nov 27, 2025 •

Copy link
Copy Markdown
Contributor

Thank you for your contribution, @aviralgarg05 !
Your PR does a lot of code style changes mixed with your actual contribution.

Could you please remove this code style changes not directly related to your contribution code to be easier for our code review? You can do it just pushing another commit.

Please pay attention we use tabs for indentation instead of spaces.

@rodolforg rodolforg changed the title Add saturation parameter to Color Correct layer feat: add saturation parameter to Color Correct layer Nov 27, 2025
@github-actions

Copy link
Copy Markdown

Thank you for attempting to improve your contribution description!
Below is how your updated contribution will appear in Synfig's release notes. Review it and make further improvements until you're satisfied!

✨ New Features

  • Add saturation parameter to Color Correct layer

    • Added param_saturation member variable to Layer_ColorCorrect class
    • Initialized saturation parameter to 1.0 (no change) in constructor
    • Implemented saturation import/export in set_param and get_param methods
    • Added saturation to parameter vocabulary with user-friendly name
    • Integrated saturation adjustment into ColorMatrix transformation chain
    • Saturation uses ColorMatrix::set_saturation() via YUV color space conversion

    This addresses issue #3634 by allowing independent control of color saturation without affecting brightness or contrast. The parameter works as follows:

    • 0.0: Grayscale (no saturation)
    • 1.0: Original saturation (default)
    • 1.0: Enhanced saturation

    The saturation parameter works independently from other color adjustments and is positioned in the transform chain after hue but before exposure, brightness, and contrast for optimal color processing.

@aviralgarg05
aviralgarg05 force-pushed the feature/add-saturation-parameter branch from 2d10836 to 8cdb2ef Compare November 27, 2025 14:30
@Svarov-RZM

Copy link
Copy Markdown
Contributor

Hi @aviralgarg05,

I've tested this PR and it doesn't quite work as expected. Take a look at this:
SaturationTest

The top row is how it should be (when you decrease saturation in HSV wheel). The bottom row is the result produced by Color Correct layer. You can clearly see that not only saturation, but hue and value also changes. Do you have an idea why?

Here's the source for test-case file:
SaturationTest.zip

@aviralgarg05

Copy link
Copy Markdown
Contributor Author

The saturation implementation is using YUV color space (via ColorMatrix::set_saturation() and Color::set_s()), which causes unwanted hue and value shifts. To get the expected behavior (matching the HSV wheel), the implementation would need to use HSV color space transformations instead, which properly isolates saturation from hue and value.

The YUV approach works for certain color grading workflows, but it doesn't match the expectation of "saturation" as defined in HSV color models.

I will make the required changes

aviralgarg05 added a commit to aviralgarg05/synfig that referenced this pull request Nov 29, 2025
Replaced YUV-based saturation implementation with HSV-based approach to
fix the issue where adjusting saturation also changed hue and value.

Changes:
- Non-accelerated path (correct_color): Implemented inline RGB-to-HSV
  conversion, saturation multiplication in HSV space, and HSV-to-RGB
  conversion. This preserves hue (H) and value (V) while only adjusting
  saturation (S).

- Accelerated path (build_rendering_task_vfunc): Removed ColorMatrix
  saturation call since ColorMatrix uses YUV color space which causes
  the same hue/value shift issue. The saturation effect will now only
  apply in the non-accelerated path where it works correctly.

Technical details:
The HSV color space properly separates hue, saturation, and value as
independent components. When adjusting saturation in HSV:
- Hue (H): Color angle - preserved unchanged
- Saturation (S): Color intensity - multiplied by parameter
- Value (V): Brightness - preserved unchanged

This matches the expected behavior of HSV wheel saturation adjustment
as reported in issue feedback.

Addresses maintainer feedback on PR synfig#3641 regarding hue/value shifts.
@Svarov-RZM

Copy link
Copy Markdown
Contributor

To get the expected behavior (matching the HSV wheel), the implementation would need to use HSV color space transformations instead

Are you absolutely sure about this? I haven't looked into it myself, but it's hard to believe that you need to convert stuff to a different representation just to change intensity of color. Anyway, I have tested your recent commits and now when you change saturation parameter nothing happens.

Have you checked the example I provided in previous message? You need to get the bottom row to be the same as top one using Color Correct layer.

@aviralgarg05

Copy link
Copy Markdown
Contributor Author

I will test it out once again, although I tested it locally

@aviralgarg05
aviralgarg05 force-pushed the feature/add-saturation-parameter branch from 8136be9 to 8cdb2ef Compare November 30, 2025 08:20
Implement HSV-based saturation control for the Color Correct layer,
allowing users to adjust color saturation independently from hue and
value, matching the behavior of the HSV color wheel.

Changes:
- Added param_saturation member variable (default: 1.0, no change)
- Implemented HSV saturation in both rendering paths:
  * Non-accelerated: in correct_color() function
  * Accelerated: via new TaskSaturation/TaskSaturationSW classes
- Added saturation to parameter I/O and UI vocabulary

The HSV saturation algorithm preserves the Value (max RGB component)
while adjusting saturation:
- 0.0: Grayscale (all RGB components equal to Value)
- 1.0: Original saturation (no change)
- >1.0: Enhanced saturation (more vivid colors)

Unlike YUV-based saturation which preserves luminance, this HSV approach
ensures that colors like pure red (1,0,0) at 0.5 saturation become
(1,0.5,0.5) rather than shifting toward gray, matching user expectations
from standard color pickers and the HSV wheel.

Fixes: synfig#3634
@aviralgarg05
aviralgarg05 force-pushed the feature/add-saturation-parameter branch from 8cdb2ef to 088850f Compare November 30, 2025 08:54
@rodolforg

Copy link
Copy Markdown
Contributor

Maybe the issue was the order of color matrix transformations in the original PR commits?

@aviralgarg05

Copy link
Copy Markdown
Contributor Author

Maybe the issue was the order of color matrix transformations in the original PR commits?

Yup, have done some turn arounds and fixes

@Svarov-RZM

Copy link
Copy Markdown
Contributor

Yup, have done some turn arounds and fixes

Well, it's close, but there's one more problem. Take a look at the second column in a test-case file:
ForgotAboutGamma

The reason for this is that you forgot about gamma settings set in Menu->Canvas->Properties->Gamma correction.
If gamma is set to 1.0 your logic works as expected, if it is set to anything else, results are no longer proper. Can you account for gamma correction in your logic?

@aviralgarg05

Copy link
Copy Markdown
Contributor Author

Sure I will do it as well

Apply saturation adjustment BEFORE gamma correction in both rendering paths:
- Non-accelerated path (correct_color): Moved saturation logic to before gamma.apply()
- Accelerated path (build_rendering_task_vfunc): Reordered tasks so TaskSaturation runs before TaskPixelGamma

The HSV saturation algorithm needs to operate in linear color space to produce
correct results. When gamma correction is applied first, it transforms the colors
non-linearly, which causes the saturation calculation to produce incorrect results.

By applying saturation first (in linear space), then gamma, the saturation now
works correctly regardless of the gamma correction setting in Canvas Properties.

Fixes issue where saturation only worked when gamma=1.0
Copilot AI review requested due to automatic review settings December 4, 2025 12:31

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a saturation parameter to the Color Correct layer, allowing users to independently adjust color saturation from 0.0 (grayscale) to values greater than 1.0 (enhanced saturation). The implementation uses a custom RGB-based saturation algorithm rather than the existing ColorMatrix YUV-based approach mentioned in the description.

Key Changes:

  • Added param_saturation member variable initialized to 1.0 (neutral)
  • Implemented custom saturation adjustment in both rendering pipeline (TaskSaturation/TaskSaturationSW) and per-pixel path (correct_color)
  • Integrated saturation into parameter import/export and vocabulary

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 8 comments.

File Description
synfig-core/src/modules/mod_filter/colorcorrect.h Added TaskSaturation rendering classes and param_saturation member variable
synfig-core/src/modules/mod_filter/colorcorrect.cpp Implemented saturation algorithm in two code paths, added parameter handling, and integrated into rendering pipeline

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

- Add clamping to [0,1] range for saturation values > 1.0 to prevent
  invalid color values when components extend beyond valid range
- Fix misleading comments: change 'HSV-based' to 'RGB-based' since
  the implementation manipulates RGB values directly, not HSV
- Remove unnecessary empty else block for grayscale color case
- Cast saturation to ColorReal to fix type mismatch with std::min/max
@Svarov-RZM

Copy link
Copy Markdown
Contributor

Sure I will do it as well

Wait, wait, I am not talking about gamma parameter of the layer itself, I mean global gamma settings that's set in Menu->Canvas->Properties->Gamma correction. Did you check the test-case file I uploaded earlier in this thread? The gamma setting there is set to 0.45 so the result of saturation change isn't computed properly.

@aviralgarg05

Copy link
Copy Markdown
Contributor Author

Ok I will check and solve

@Abdelhadi-Wael Abdelhadi-Wael left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove unrelated formatting changes

Saturation was not working correctly when canvas gamma != 1.0 because
the saturation algorithm was operating on gamma-corrected colors instead
of linear colors.

Changes:
- Added canvas_gamma field to TaskSaturation to pass canvas gamma settings
- Updated apply_saturation to linearize colors (inverse gamma) before
  saturation calculation, then reapply gamma to output
- Updated correct_color to get canvas gamma from
  get_canvas()->get_root()->rend_desc().get_gamma() and apply same
  linearization logic
- Updated build_rendering_task_vfunc to pass canvas gamma to TaskSaturation

This ensures saturation produces correct results regardless of the gamma
correction setting in Menu->Canvas->Properties->Gamma correction.

Fixes issue reported where saturation only worked with gamma=1.0 but
failed with gamma=0.45 in the test case file.
@aviralgarg05
aviralgarg05 force-pushed the feature/add-saturation-parameter branch from 70edbe9 to a732e0d Compare December 21, 2025 05:42
@Svarov-RZM

Copy link
Copy Markdown
Contributor

@aviralgarg05 I checked the last commit and saturation now works as expected, hooray! But...

The 'Contrast' parameter is now broken. To reproduce:

  • Open the example file I attached in this thread
  • Zoom in past 200%
  • Change 'Contrast' parameter of any 'Color Correct' layer to 0.5

After that Synfig will either crash immediately or produce weird artifacts like this:
ContrastBreaksThings
And then crash on zooming out. Can you please investigate what's causing this?

Oh, and also, you still didn't address the indentation issue, please notice that we use tabs for indentation, not spaces.

- Fixed TaskSaturationSW::run() to include proper validation checks
  matching TaskPixelGammaSW pattern (is_valid, sub_task, sub_task->is_valid)
  to prevent crash when changing Contrast parameter
- Corrected offset calculation for source pixel access using the pattern
  from TaskPixelGammaSW to prevent out-of-bounds memory access
- Changed indentation from spaces to tabs to match codebase style
- Maintained all saturation functionality with canvas gamma linearization
@Svarov-RZM

Copy link
Copy Markdown
Contributor

@aviralgarg05 OK, I tested the last commit and everything works as expected now.
One little thing left, please bump the version of the layer to 0.2 - we did introduce new functionality after all. After that, the PR is ready for final review.

@Svarov-RZM

Copy link
Copy Markdown
Contributor

@rodolforg @ice0 @BobSynfig @Abdelhadi-Wael

Hey guys,

Can some of you review the changes? There were a lot of mistakes and I am not confident reviewing it myself for I am not very good with C++. I can confirm that logic is correct and working properly but maybe you have some code-related feedback. If you approve, I'll merge it.

@rodolforg rodolforg self-assigned this Jan 17, 2026

@rodolforg rodolforg left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Svarov-RZM The only code issue I could see was outside of scope of this PR.
It seems the order of Hue Adjust, Brightness, Contrast and Exposure is different for the cobra render and the color pick/eyedrop tool.
I don't know if it could give different results though.

- Use references instead of pointers in TaskSaturationSW::run() loop
- Add static inline apply_saturation_impl() to eliminate code duplication
- Refactor TaskSaturationSW::apply_saturation() to use shared helper
- Refactor Layer_ColorCorrect::correct_color() to use shared helper

Addresses review comments by @rodolforg on PR synfig#3641
@rodolforg

Copy link
Copy Markdown
Contributor

Thank you for the review feedback, @rodolforg!

I've addressed both comments in commit eb531ce:

1. **Use references instead of pointers** - Changed the loop in `TaskSaturationSW::run()` to use `const Color& src_color` and `Color& dst_color` as suggested.

2. **Create static inline helper** - Added `apply_saturation_impl()` as a `static inline` function that both `TaskSaturationSW::apply_saturation()` and `Layer_ColorCorrect::correct_color()` now call. This eliminates the code duplication while maintaining performance.

Wow, you were fast XD What about the change requests regarding the code style?

@aviralgarg05

Copy link
Copy Markdown
Contributor Author

Thank you for the review feedback, @rodolforg!
I've addressed both comments in commit eb531ce:

1. **Use references instead of pointers** - Changed the loop in `TaskSaturationSW::run()` to use `const Color& src_color` and `Color& dst_color` as suggested.

2. **Create static inline helper** - Added `apply_saturation_impl()` as a `static inline` function that both `TaskSaturationSW::apply_saturation()` and `Layer_ColorCorrect::correct_color()` now call. This eliminates the code duplication while maintaining performance.

Wow, you were fast XD What about the change requests regarding the code style?

Yup I was actually fixing them, was going through them one by one

- Replace 'virtual' with 'override' for get_token() and run() methods
- Use 'Type& var' reference spacing instead of 'Type &var'
- Add set_description() for saturation parameter tooltip
- Replace std::max/std::min with synfig::clamp() for better readability
- Fix brace style: opening { on same line as if/for statements
- Add space between keywords (for, if) and opening parenthesis
- Keep pointer-based loop for efficiency (less address computation)

Addresses review comments by @rodolforg on PR synfig#3641
@aviralgarg05

Copy link
Copy Markdown
Contributor Author

Thank you for the detailed review, @rodolforg! I've addressed all feedback in commits eb531ce and d39d348:

Changes Made:

  1. virtual -> override - Removed virtual keyword and added override for get_token() and run() methods
  2. Reference spacing - Changed Type &var to Type& var throughout
  3. set_description - Added tooltip for saturation parameter: "Color saturation adjustment. 0.0 = grayscale, 1.0 = no change, >1.0 = more saturated"
  4. synfig::clamp - Replaced std::max(min, std::min(max, val)) with synfig::clamp(val, min, max)
  5. Brace style - Changed to if (condition) { style
  6. Keyword spacing - Added space between for/if and (
  7. Pointer efficiency - Kept original pointer-based loop per your latest feedback
  8. static inline helper - apply_saturation_impl() is used by both TaskSaturationSW::apply_saturation() and Layer_ColorCorrect::correct_color()

Regarding optional clamping:

I kept clamping enabled for now because without it, saturation > 1.0 can push color components outside [0,1] range, potentially causing rendering artifacts. However, if you prefer, I can add a boolean parameter to make clamping optional for HDR workflows. Please let me know your preference.

@rodolforg

Copy link
Copy Markdown
Contributor

I kept clamping enabled for now because without it, saturation > 1.0 can push color components outside [0,1] range, potentially causing rendering artifacts. However, if you prefer, I can add a boolean parameter to make clamping optional for HDR workflows. Please let me know your preference.

Oh, I suggested because I believe it would be something @morevnaproject would request lol

@Svarov-RZM

Copy link
Copy Markdown
Contributor

I kept clamping enabled for now because without it, saturation > 1.0 can push color components outside [0,1] range, potentially causing rendering artifacts

When those artifacts are used to create some cool effects they become artistic vision :)
Basically, in Synfig we don't stop user from pushing things out of range, for example, we can set opacity greater than 1 to make blur thicker. Let's remove clamping, it's not necessary here.

@aviralgarg05 We have two things left to do:

  • Remove clamping (I'll give it one final test after that)
  • Address remaining rodolforg's code-style request

And I believe we're done.

- Fix reference spacing: 'Type &var' -> 'Type& var' for Surface variables
- Remove clamping to allow artistic effects with saturation > 1.0

Addresses feedback from @rodolforg and @Abdelhadi-Wael on PR synfig#3641
@aviralgarg05

Copy link
Copy Markdown
Contributor Author

Done with the feedbacks!! @Svarov-RZM @rodolforg

@Svarov-RZM
Svarov-RZM dismissed rodolforg’s stale review January 25, 2026 05:17

I see that feedback was addressed, so I feel like I can handle it from here.

@Svarov-RZM

Copy link
Copy Markdown
Contributor

Sheesh, the history here is a mess... I am squashing it.

@aviralgarg05 I finished testing it and didn't find any more problems. We struggled a bit with this but in the end we got it working. Thanks for not giving up and seeing it through.

@aviralgarg05

Copy link
Copy Markdown
Contributor Author

@Svarov-RZM @rodolforg is there a way I could personally connect with you, there is someone I got a message from looking to connect with you urgently regarding a few Synfig-related assets and has questions around licensing and legal responsibility for the community.

@BobSynfig

Copy link
Copy Markdown
Collaborator

@aviralgarg05 For legal matters, you should contact our project leader @morevnaproject (@KonstantinDmitriev on our forums)

@Svarov-RZM

Copy link
Copy Markdown
Contributor

@aviralgarg05 Yeah, what Bob said - please use forums for this matter: https://forums.synfig.org/
Post your questions there and hopefully someone will provide an answer.

urgently

There's no "urgently" in Open Source.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants