From c900c9cc82e74d50bbcb56f24271c94f34b38b81 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Wed, 20 May 2020 14:49:56 +0200 Subject: [PATCH] reduce derived colors calculations --- .../com/formdev/flatlaf/util/DerivedColor.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/util/DerivedColor.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/util/DerivedColor.java index 83860510..798d8684 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/util/DerivedColor.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/util/DerivedColor.java @@ -32,13 +32,28 @@ public class DerivedColor { private final ColorFunction[] functions; + private boolean hasBaseOfDefaultColor; + private int baseOfDefaultColorRGB; + public DerivedColor( Color defaultColor, ColorFunction... functions ) { super( (defaultColor != null) ? defaultColor : Color.red ); this.functions = functions; } public Color derive( Color baseColor ) { - return ColorFunctions.applyFunctions( baseColor, functions ); + if( hasBaseOfDefaultColor && baseOfDefaultColorRGB == baseColor.getRGB() ) + return this; // return default color + + Color result = ColorFunctions.applyFunctions( baseColor, functions ); + + // if the result is equal to the default color, then the original base color + // was passed and we can cache this to avoid color calculations + if( !hasBaseOfDefaultColor && result.getRGB() == this.getRGB() ) { + hasBaseOfDefaultColor = true; + baseOfDefaultColorRGB = baseColor.getRGB(); + } + + return result; } public ColorFunction[] getFunctions() {