From 1ec73247e55b1dc66508bfdffbaff0a408845eb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zo=C3=AB=20Peterson?= Date: Thu, 16 Feb 2023 15:23:11 -0600 Subject: [PATCH 1/3] Added a new global variable, DisabledDrawEffectStyle, that controls how TRawImage.PerformEffect(gdeDisabled) works to improve the appearance on colored backgrounds and in dark mode apps. There are three possible values: - ddesGrayscale This is the default and matches the existing behavior of just converting the pixels to gray. - ddesDarken Convert the image to fully black pixels with alpha transparency. This roughly matches ddesGrayscale on traditional gray backgrounds but lets colored backgrounds through. - ddesLighten Convert the image to fully white pixels with alpha transparency. This is the dark mode equivalent of ddesDarken. Existing alpha transparency is factored into the conversion. The constants used for the visibility calculation roughly match what other macOS apps use. --- components/lazutils/graphtype.pp | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/components/lazutils/graphtype.pp b/components/lazutils/graphtype.pp index 34ca17ab2e..80bae2b2c6 100644 --- a/components/lazutils/graphtype.pp +++ b/components/lazutils/graphtype.pp @@ -268,6 +268,10 @@ function RawImageQueryFlagsToString(AFlags: TRawImageQueryFlags): string; var MissingBits: array[0..15] of array[0..7] of word; + // Control whether gdeDisabled is a grayscale image (classic), or uses + // alpha-transparent black or alpha-transparent white, improving appearance + // on non-grey light and dark backgrounds + DisabledDrawEffectStyle: (ddesGrayscale, ddesDarken, ddesLighten) = ddesGrayscale; implementation @@ -1651,6 +1655,7 @@ var AData: PRGBAQuad; P: Pointer; i, j: integer; + Gray: Byte; begin // check here for Description. Only RGBA data can be processed here. if not CheckDescription then @@ -1676,9 +1681,25 @@ begin begin with AData^ do begin - Red := (Red + Green + Blue) div 3; - Green := Red; - Blue := Red; + Gray := (Red + Green + Blue) div 3; + if DisabledDrawEffectStyle = ddesLighten then + begin + // Apply existing alpha and reduce visibility by a further 66% + Alpha := Byte((Integer(Gray) * Alpha) div 768); + Gray := $FF; + end + else begin + Gray := Byte(Integer(Gray) + Integer((DimPercent * (DimColor - Gray)) div 100)); + if DisabledDrawEffectStyle = ddesDarken then + begin + // Apply existing alpha and Reduce visibility by a further 25% + Alpha := Byte((Integer($FF - Gray) * Alpha) div 384); + Gray := $00; + end; + end; + Red := Gray; + Green := Gray; + Blue := Gray; end; inc(AData); end; From b8bb9527ef4348dd3793f260dbf8788ed668b2b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zo=C3=AB=20Peterson?= Date: Fri, 7 Jun 2024 12:55:22 -0500 Subject: [PATCH 2/3] Switched documentation from an inline comment in graphtype.pp to the fpdoc graphtype.xml --- components/lazutils/graphtype.pp | 3 --- docs/xml/lazutils/graphtype.xml | 20 ++++++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/components/lazutils/graphtype.pp b/components/lazutils/graphtype.pp index 80bae2b2c6..73621ec25e 100644 --- a/components/lazutils/graphtype.pp +++ b/components/lazutils/graphtype.pp @@ -268,9 +268,6 @@ function RawImageQueryFlagsToString(AFlags: TRawImageQueryFlags): string; var MissingBits: array[0..15] of array[0..7] of word; - // Control whether gdeDisabled is a grayscale image (classic), or uses - // alpha-transparent black or alpha-transparent white, improving appearance - // on non-grey light and dark backgrounds DisabledDrawEffectStyle: (ddesGrayscale, ddesDarken, ddesLighten) = ddesGrayscale; implementation diff --git a/docs/xml/lazutils/graphtype.xml b/docs/xml/lazutils/graphtype.xml index eb2d28841c..5bba084b47 100644 --- a/docs/xml/lazutils/graphtype.xml +++ b/docs/xml/lazutils/graphtype.xml @@ -1317,6 +1317,26 @@ boundaries. + +Controls the appearance of disabled images. + +

+This variable controls how +represents gdeDisabled. +Applications should set it based on the background color for their +interfaces. +

+
+
+ +Convert RGB values to grayscale. + + +Convert to black with alpha representing grayscale. + + +Convert to white with alpha representing grayscale. + From 14fdb7e6f9cb41739a8dc069c2abbdf21462e6a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zo=C3=AB=20Peterson?= Date: Wed, 12 Jun 2024 17:12:31 -0500 Subject: [PATCH 3/3] Removed DimPercent/DimColor calculation. The existing grayscale calculation doesn't change the icon if it's already grayscale, and that was a remnant from an earlier patch that simulated the alpha that ddesDarken does. --- components/lazutils/graphtype.pp | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/components/lazutils/graphtype.pp b/components/lazutils/graphtype.pp index 73621ec25e..3c7ee17763 100644 --- a/components/lazutils/graphtype.pp +++ b/components/lazutils/graphtype.pp @@ -1679,20 +1679,16 @@ begin with AData^ do begin Gray := (Red + Green + Blue) div 3; + // Apply existing alpha and reduce visibility by a further 66% if DisabledDrawEffectStyle = ddesLighten then begin - // Apply existing alpha and reduce visibility by a further 66% Alpha := Byte((Integer(Gray) * Alpha) div 768); Gray := $FF; end - else begin - Gray := Byte(Integer(Gray) + Integer((DimPercent * (DimColor - Gray)) div 100)); - if DisabledDrawEffectStyle = ddesDarken then - begin - // Apply existing alpha and Reduce visibility by a further 25% - Alpha := Byte((Integer($FF - Gray) * Alpha) div 384); - Gray := $00; - end; + else if DisabledDrawEffectStyle = ddesDarken then + begin + Alpha := Byte((Integer($FF - Gray) * Alpha) div 768); + Gray := $00; end; Red := Gray; Green := Gray;