diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 622b015fb6..42a60f1fb1 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -570,15 +570,19 @@ FILE: ../../../flutter/impeller/entity/entity_playground.h FILE: ../../../flutter/impeller/entity/entity_unittests.cc FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend.glsl FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend.vert +FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_color.frag FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_colorburn.frag FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_colordodge.frag FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_darken.frag FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_difference.frag FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_exclusion.frag FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_hardlight.frag +FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_hue.frag FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_lighten.frag +FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_luminosity.frag FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_multiply.frag FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_overlay.frag +FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_saturation.frag FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_screen.frag FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_softlight.frag FILE: ../../../flutter/impeller/entity/shaders/blending/advanced_blend_utils.glsl diff --git a/engine/src/flutter/impeller/aiks/aiks_unittests.cc b/engine/src/flutter/impeller/aiks/aiks_unittests.cc index 1adcd168fa..05f255a3ba 100644 --- a/engine/src/flutter/impeller/aiks/aiks_unittests.cc +++ b/engine/src/flutter/impeller/aiks/aiks_unittests.cc @@ -10,6 +10,7 @@ #include "impeller/aiks/aiks_playground.h" #include "impeller/aiks/canvas.h" #include "impeller/aiks/image.h" +#include "impeller/geometry/color.h" #include "impeller/geometry/geometry_unittests.h" #include "impeller/geometry/path_builder.h" #include "impeller/playground/widgets.h" @@ -544,6 +545,10 @@ TEST_P(AiksTest, ColorWheel) { {"Difference", Entity::BlendMode::kDifference}, {"Exclusion", Entity::BlendMode::kExclusion}, {"Multiply", Entity::BlendMode::kMultiply}, + {"Hue", Entity::BlendMode::kHue}, + {"Saturation", Entity::BlendMode::kSaturation}, + {"Color", Entity::BlendMode::kColor}, + {"Luminosity", Entity::BlendMode::kLuminosity}, }; assert(blends.size() == static_cast(Entity::BlendMode::kLastAdvancedBlendMode) + 1); @@ -594,19 +599,25 @@ TEST_P(AiksTest, ColorWheel) { auto callback = [&](AiksContext& renderer, RenderTarget& render_target) { if (first_frame) { first_frame = false; - ImGui::SetNextWindowSize({350, 200}); - ImGui::SetNextWindowPos({325, 550}); + ImGui::SetNextWindowSize({350, 260}); + ImGui::SetNextWindowPos({25, 25}); } // UI state. static int current_blend_index = 3; static float alpha = 1; + static Color color0 = Color::Red(); + static Color color1 = Color::Green(); + static Color color2 = Color::Blue(); ImGui::Begin("Controls"); { ImGui::ListBox("Blending mode", ¤t_blend_index, blend_mode_names.data(), blend_mode_names.size()); ImGui::SliderFloat("Alpha", &alpha, 0, 1); + ImGui::ColorEdit4("Color A", reinterpret_cast(&color0)); + ImGui::ColorEdit4("Color B", reinterpret_cast(&color1)); + ImGui::ColorEdit4("Color C", reinterpret_cast(&color2)); } ImGui::End(); @@ -629,11 +640,11 @@ TEST_P(AiksTest, ColorWheel) { paint.blend_mode = Entity::BlendMode::kPlus; const Scalar x = std::sin(k2Pi / 3); const Scalar y = -std::cos(k2Pi / 3); - paint.color = Color::Red(); + paint.color = color0; canvas.DrawCircle(Point(-x, y) * 45, 65, paint); - paint.color = Color::Green(); + paint.color = color1; canvas.DrawCircle(Point(0, -1) * 45, 65, paint); - paint.color = Color::Blue(); + paint.color = color2; canvas.DrawCircle(Point(x, y) * 45, 65, paint); } canvas.Restore(); diff --git a/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc b/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc index 91c10ae99d..40f7b49869 100644 --- a/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc +++ b/engine/src/flutter/impeller/display_list/display_list_dispatcher.cc @@ -33,8 +33,7 @@ DisplayListDispatcher::DisplayListDispatcher() = default; DisplayListDispatcher::~DisplayListDispatcher() = default; -static std::optional ToBlendMode(flutter::DlBlendMode mode) { - // TODO(100086): Implement remaining advanced blends. +static Entity::BlendMode ToBlendMode(flutter::DlBlendMode mode) { switch (mode) { case flutter::DlBlendMode::kClear: return Entity::BlendMode::kClear; @@ -87,10 +86,13 @@ static std::optional ToBlendMode(flutter::DlBlendMode mode) { case flutter::DlBlendMode::kMultiply: return Entity::BlendMode::kMultiply; case flutter::DlBlendMode::kHue: + return Entity::BlendMode::kHue; case flutter::DlBlendMode::kSaturation: + return Entity::BlendMode::kSaturation; case flutter::DlBlendMode::kColor: + return Entity::BlendMode::kColor; case flutter::DlBlendMode::kLuminosity: - return std::nullopt; + return Entity::BlendMode::kLuminosity; } FML_UNREACHABLE(); } @@ -242,16 +244,9 @@ void DisplayListDispatcher::setColorFilter( auto dl_blend = filter->asBlend(); auto blend_mode = ToBlendMode(dl_blend->mode()); - if (!blend_mode.has_value()) { - // TODO(100086): Implement remaining advanced blends. - UNIMPLEMENTED; - blend_mode = Entity::BlendMode::kSourceOver; - } - auto color = ToColor(dl_blend->color()); - paint_.color_filter = [blend_mode = blend_mode.value(), - color](FilterInput::Ref input) { + paint_.color_filter = [blend_mode, color](FilterInput::Ref input) { return FilterContents::MakeBlend(blend_mode, {input}, color); }; return; @@ -272,12 +267,7 @@ void DisplayListDispatcher::setInvertColors(bool invert) { // |flutter::Dispatcher| void DisplayListDispatcher::setBlendMode(flutter::DlBlendMode dl_mode) { - if (auto mode = ToBlendMode(dl_mode); mode.has_value()) { - paint_.blend_mode = mode.value(); - } else { - UNIMPLEMENTED; - paint_.blend_mode = Entity::BlendMode::kSourceOver; - } + paint_.blend_mode = ToBlendMode(dl_mode); } // |flutter::Dispatcher| @@ -643,11 +633,7 @@ void DisplayListDispatcher::drawColor(flutter::DlColor color, flutter::DlBlendMode dl_mode) { Paint paint; paint.color = ToColor(color); - if (auto mode = ToBlendMode(dl_mode); mode.has_value()) { - paint.blend_mode = mode.value(); - } else { - FML_DLOG(ERROR) << "Unimplemented blend mode in " << __FUNCTION__; - } + paint.blend_mode = ToBlendMode(dl_mode); canvas_.DrawPaint(paint); } @@ -727,11 +713,7 @@ void DisplayListDispatcher::drawSkVertices(const sk_sp vertices, // |flutter::Dispatcher| void DisplayListDispatcher::drawVertices(const flutter::DlVertices* vertices, flutter::DlBlendMode dl_mode) { - if (auto mode = ToBlendMode(dl_mode); mode.has_value()) { - canvas_.DrawVertices(ToVertices(vertices), mode.value(), paint_); - } else { - FML_DLOG(ERROR) << "Unimplemented blend mode in " << __FUNCTION__; - } + canvas_.DrawVertices(ToVertices(vertices), ToBlendMode(dl_mode), paint_); } // |flutter::Dispatcher| diff --git a/engine/src/flutter/impeller/entity/BUILD.gn b/engine/src/flutter/impeller/entity/BUILD.gn index b15c495aa2..8ac34f0934 100644 --- a/engine/src/flutter/impeller/entity/BUILD.gn +++ b/engine/src/flutter/impeller/entity/BUILD.gn @@ -9,15 +9,19 @@ impeller_shaders("entity_shaders") { shaders = [ "shaders/blending/advanced_blend.vert", + "shaders/blending/advanced_blend_color.frag", "shaders/blending/advanced_blend_colorburn.frag", "shaders/blending/advanced_blend_colordodge.frag", "shaders/blending/advanced_blend_darken.frag", "shaders/blending/advanced_blend_difference.frag", "shaders/blending/advanced_blend_exclusion.frag", "shaders/blending/advanced_blend_hardlight.frag", + "shaders/blending/advanced_blend_hue.frag", "shaders/blending/advanced_blend_lighten.frag", + "shaders/blending/advanced_blend_luminosity.frag", "shaders/blending/advanced_blend_multiply.frag", "shaders/blending/advanced_blend_overlay.frag", + "shaders/blending/advanced_blend_saturation.frag", "shaders/blending/advanced_blend_screen.frag", "shaders/blending/advanced_blend_softlight.frag", "shaders/blending/blend.frag", diff --git a/engine/src/flutter/impeller/entity/contents/content_context.cc b/engine/src/flutter/impeller/entity/contents/content_context.cc index 318ed476d8..f0910f52ec 100644 --- a/engine/src/flutter/impeller/entity/contents/content_context.cc +++ b/engine/src/flutter/impeller/entity/contents/content_context.cc @@ -155,6 +155,8 @@ ContentContext::ContentContext(std::shared_ptr context) CreateDefaultPipeline(*context_); texture_blend_pipelines_[{}] = CreateDefaultPipeline(*context_); + blend_color_pipelines_[{}] = + CreateDefaultPipeline(*context_); blend_colorburn_pipelines_[{}] = CreateDefaultPipeline(*context_); blend_colordodge_pipelines_[{}] = @@ -167,12 +169,17 @@ ContentContext::ContentContext(std::shared_ptr context) CreateDefaultPipeline(*context_); blend_hardlight_pipelines_[{}] = CreateDefaultPipeline(*context_); + blend_hue_pipelines_[{}] = CreateDefaultPipeline(*context_); blend_lighten_pipelines_[{}] = CreateDefaultPipeline(*context_); + blend_luminosity_pipelines_[{}] = + CreateDefaultPipeline(*context_); blend_multiply_pipelines_[{}] = CreateDefaultPipeline(*context_); blend_overlay_pipelines_[{}] = CreateDefaultPipeline(*context_); + blend_saturation_pipelines_[{}] = + CreateDefaultPipeline(*context_); blend_screen_pipelines_[{}] = CreateDefaultPipeline(*context_); blend_softlight_pipelines_[{}] = diff --git a/engine/src/flutter/impeller/entity/contents/content_context.h b/engine/src/flutter/impeller/entity/contents/content_context.h index 3c26d738c8..aa3a5e0787 100644 --- a/engine/src/flutter/impeller/entity/contents/content_context.h +++ b/engine/src/flutter/impeller/entity/contents/content_context.h @@ -12,15 +12,19 @@ #include "fml/logging.h" #include "impeller/base/validation.h" #include "impeller/entity/advanced_blend.vert.h" +#include "impeller/entity/advanced_blend_color.frag.h" #include "impeller/entity/advanced_blend_colorburn.frag.h" #include "impeller/entity/advanced_blend_colordodge.frag.h" #include "impeller/entity/advanced_blend_darken.frag.h" #include "impeller/entity/advanced_blend_difference.frag.h" #include "impeller/entity/advanced_blend_exclusion.frag.h" #include "impeller/entity/advanced_blend_hardlight.frag.h" +#include "impeller/entity/advanced_blend_hue.frag.h" #include "impeller/entity/advanced_blend_lighten.frag.h" +#include "impeller/entity/advanced_blend_luminosity.frag.h" #include "impeller/entity/advanced_blend_multiply.frag.h" #include "impeller/entity/advanced_blend_overlay.frag.h" +#include "impeller/entity/advanced_blend_saturation.frag.h" #include "impeller/entity/advanced_blend_screen.frag.h" #include "impeller/entity/advanced_blend_softlight.frag.h" #include "impeller/entity/blend.frag.h" @@ -51,6 +55,8 @@ using GradientFillPipeline = using SolidFillPipeline = PipelineT; using BlendPipeline = PipelineT; +using BlendColorPipeline = + PipelineT; using BlendColorBurnPipeline = PipelineT; using BlendColorDodgePipeline = @@ -63,12 +69,18 @@ using BlendExclusionPipeline = PipelineT; using BlendHardLightPipeline = PipelineT; +using BlendHuePipeline = + PipelineT; using BlendLightenPipeline = PipelineT; +using BlendLuminosityPipeline = + PipelineT; using BlendMultiplyPipeline = PipelineT; using BlendOverlayPipeline = PipelineT; +using BlendSaturationPipeline = + PipelineT; using BlendScreenPipeline = PipelineT; using BlendSoftLightPipeline = @@ -173,6 +185,11 @@ class ContentContext { // Advanced blends. + std::shared_ptr GetBlendColorPipeline( + ContentContextOptions opts) const { + return GetPipeline(blend_color_pipelines_, opts); + } + std::shared_ptr GetBlendColorBurnPipeline( ContentContextOptions opts) const { return GetPipeline(blend_colorburn_pipelines_, opts); @@ -203,11 +220,21 @@ class ContentContext { return GetPipeline(blend_hardlight_pipelines_, opts); } + std::shared_ptr GetBlendHuePipeline( + ContentContextOptions opts) const { + return GetPipeline(blend_hue_pipelines_, opts); + } + std::shared_ptr GetBlendLightenPipeline( ContentContextOptions opts) const { return GetPipeline(blend_lighten_pipelines_, opts); } + std::shared_ptr GetBlendLuminosityPipeline( + ContentContextOptions opts) const { + return GetPipeline(blend_luminosity_pipelines_, opts); + } + std::shared_ptr GetBlendMultiplyPipeline( ContentContextOptions opts) const { return GetPipeline(blend_multiply_pipelines_, opts); @@ -218,6 +245,11 @@ class ContentContext { return GetPipeline(blend_overlay_pipelines_, opts); } + std::shared_ptr GetBlendSaturationPipeline( + ContentContextOptions opts) const { + return GetPipeline(blend_saturation_pipelines_, opts); + } + std::shared_ptr GetBlendScreenPipeline( ContentContextOptions opts) const { return GetPipeline(blend_screen_pipelines_, opts); @@ -261,15 +293,19 @@ class ContentContext { mutable Variants glyph_atlas_pipelines_; mutable Variants vertices_pipelines_; // Advanced blends. + mutable Variants blend_color_pipelines_; mutable Variants blend_colorburn_pipelines_; mutable Variants blend_colordodge_pipelines_; mutable Variants blend_darken_pipelines_; mutable Variants blend_difference_pipelines_; mutable Variants blend_exclusion_pipelines_; mutable Variants blend_hardlight_pipelines_; + mutable Variants blend_hue_pipelines_; mutable Variants blend_lighten_pipelines_; + mutable Variants blend_luminosity_pipelines_; mutable Variants blend_multiply_pipelines_; mutable Variants blend_overlay_pipelines_; + mutable Variants blend_saturation_pipelines_; mutable Variants blend_screen_pipelines_; mutable Variants blend_softlight_pipelines_; diff --git a/engine/src/flutter/impeller/entity/contents/filters/blend_filter_contents.cc b/engine/src/flutter/impeller/entity/contents/filters/blend_filter_contents.cc index 1c0988c896..39d3d25d7f 100644 --- a/engine/src/flutter/impeller/entity/contents/filters/blend_filter_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/filters/blend_filter_contents.cc @@ -238,7 +238,7 @@ void BlendFilterContents::SetBlendMode(Entity::BlendMode blend_mode) { if (blend_mode > Entity::BlendMode::kLastPipelineBlendMode) { static_assert(Entity::BlendMode::kLastAdvancedBlendMode == - Entity::BlendMode::kMultiply); + Entity::BlendMode::kLuminosity); switch (blend_mode) { BLEND_CASE(Screen) @@ -252,6 +252,10 @@ void BlendFilterContents::SetBlendMode(Entity::BlendMode blend_mode) { BLEND_CASE(Difference) BLEND_CASE(Exclusion) BLEND_CASE(Multiply) + BLEND_CASE(Hue) + BLEND_CASE(Saturation) + BLEND_CASE(Color) + BLEND_CASE(Luminosity) default: FML_UNREACHABLE(); } diff --git a/engine/src/flutter/impeller/entity/entity.h b/engine/src/flutter/impeller/entity/entity.h index dec0c15605..9ac7be1ea8 100644 --- a/engine/src/flutter/impeller/entity/entity.h +++ b/engine/src/flutter/impeller/entity/entity.h @@ -53,9 +53,13 @@ class Entity { kDifference, kExclusion, kMultiply, + kHue, + kSaturation, + kColor, + kLuminosity, kLastPipelineBlendMode = kModulate, - kLastAdvancedBlendMode = kMultiply, + kLastAdvancedBlendMode = kLuminosity, }; enum class ClipOperation { diff --git a/engine/src/flutter/impeller/entity/shaders/blending/advanced_blend_color.frag b/engine/src/flutter/impeller/entity/shaders/blending/advanced_blend_color.frag new file mode 100644 index 0000000000..290e20adc7 --- /dev/null +++ b/engine/src/flutter/impeller/entity/shaders/blending/advanced_blend_color.frag @@ -0,0 +1,12 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "advanced_blend_utils.glsl" + +vec3 Blend(vec3 dst, vec3 src) { + // https://www.w3.org/TR/compositing-1/#blendingcolor + return SetLuminosity(src, Luminosity(dst)); +} + +#include "advanced_blend.glsl" diff --git a/engine/src/flutter/impeller/entity/shaders/blending/advanced_blend_hardlight.frag b/engine/src/flutter/impeller/entity/shaders/blending/advanced_blend_hardlight.frag index 186f5ecbcd..cbd109fc66 100644 --- a/engine/src/flutter/impeller/entity/shaders/blending/advanced_blend_hardlight.frag +++ b/engine/src/flutter/impeller/entity/shaders/blending/advanced_blend_hardlight.frag @@ -5,6 +5,7 @@ #include "advanced_blend_utils.glsl" vec3 Blend(vec3 dst, vec3 src) { + // https://www.w3.org/TR/compositing-1/#blendinghardlight return BlendHardLight(dst, src); } diff --git a/engine/src/flutter/impeller/entity/shaders/blending/advanced_blend_hue.frag b/engine/src/flutter/impeller/entity/shaders/blending/advanced_blend_hue.frag new file mode 100644 index 0000000000..bdcf6e2daf --- /dev/null +++ b/engine/src/flutter/impeller/entity/shaders/blending/advanced_blend_hue.frag @@ -0,0 +1,12 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "advanced_blend_utils.glsl" + +vec3 Blend(vec3 dst, vec3 src) { + // https://www.w3.org/TR/compositing-1/#blendinghue + return SetLuminosity(SetSaturation(src, Saturation(dst)), Luminosity(dst)); +} + +#include "advanced_blend.glsl" diff --git a/engine/src/flutter/impeller/entity/shaders/blending/advanced_blend_luminosity.frag b/engine/src/flutter/impeller/entity/shaders/blending/advanced_blend_luminosity.frag new file mode 100644 index 0000000000..5faecd9e50 --- /dev/null +++ b/engine/src/flutter/impeller/entity/shaders/blending/advanced_blend_luminosity.frag @@ -0,0 +1,12 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "advanced_blend_utils.glsl" + +vec3 Blend(vec3 dst, vec3 src) { + // https://www.w3.org/TR/compositing-1/#blendingluminosity + return SetLuminosity(dst, Luminosity(src)); +} + +#include "advanced_blend.glsl" diff --git a/engine/src/flutter/impeller/entity/shaders/blending/advanced_blend_saturation.frag b/engine/src/flutter/impeller/entity/shaders/blending/advanced_blend_saturation.frag new file mode 100644 index 0000000000..a127446db4 --- /dev/null +++ b/engine/src/flutter/impeller/entity/shaders/blending/advanced_blend_saturation.frag @@ -0,0 +1,12 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "advanced_blend_utils.glsl" + +vec3 Blend(vec3 dst, vec3 src) { + // https://www.w3.org/TR/compositing-1/#blendingsaturation + return SetLuminosity(SetSaturation(dst, Saturation(src)), Luminosity(dst)); +} + +#include "advanced_blend.glsl" diff --git a/engine/src/flutter/impeller/entity/shaders/blending/advanced_blend_utils.glsl b/engine/src/flutter/impeller/entity/shaders/blending/advanced_blend_utils.glsl index 94ec9b6e03..a92a05cc58 100644 --- a/engine/src/flutter/impeller/entity/shaders/blending/advanced_blend_utils.glsl +++ b/engine/src/flutter/impeller/entity/shaders/blending/advanced_blend_utils.glsl @@ -2,9 +2,13 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +const float kEhCloseEnough = 0.000001; + vec3 ComponentIsValue(vec3 n, float value) { vec3 diff = abs(n - value); - return vec3(diff.r < 0.0001, diff.g < 0.0001, diff.b < 0.0001); + return vec3(diff.r < kEhCloseEnough, // + diff.g < kEhCloseEnough, // + diff.b < kEhCloseEnough); } vec3 MixComponents(vec3 a, vec3 b, vec3 weight, float cutoff) { @@ -24,3 +28,44 @@ vec3 BlendScreen(vec3 dst, vec3 src) { vec3 BlendHardLight(vec3 dst, vec3 src) { return MixHalf(dst * (2 * src), BlendScreen(dst, 2 * src - 1), src); } + +//------------------------------------------------------------------------------ +// HSV utilities. +// + +float Luminosity(vec3 color) { + return color.r * 0.3 + color.g * 0.59 + color.b * 0.11; +} + +/// Scales the color's luma by the amount necessary to place the color +/// components in a 1-0 range. +vec3 ClipColor(vec3 color) { + float lum = Luminosity(color); + float mn = min(min(color.r, color.g), color.b); + float mx = max(max(color.r, color.g), color.b); + // `lum - mn` and `mx - lum` will always be >= 0 in the following conditions, + // so adding a tiny value is enough to make these divisions safe. + if (mn < 0) { + color = lum + (((color - lum) * lum) / (lum - mn + kEhCloseEnough)); + } + if (mx > 1) { + color = lum + (((color - lum) * (1 - lum)) / (mx - lum + kEhCloseEnough)); + } + return color; +} + +vec3 SetLuminosity(vec3 color, float luminosity) { + float relative_lum = luminosity - Luminosity(color); + return ClipColor(color + relative_lum); +} + +float Saturation(vec3 color) { + return max(max(color.r, color.g), color.b) - + min(min(color.r, color.g), color.b); +} + +vec3 SetSaturation(vec3 color, float saturation) { + float mn = min(min(color.r, color.g), color.b); + float mx = max(max(color.r, color.g), color.b); + return (mn < mx) ? ((color - mn) * saturation) / (mx - mn) : vec3(0); +}