Revision: 16407
          http://sourceforge.net/p/skim-app/code/16407
Author:   hofman
Date:     2026-09-01 16:10:56 +0000 (Tue, 01 Sep 2026)
Log Message:
-----------
invert custom background color in dark mode when inverting colors, so the 
background color will be as close as possible to the actual chosen color

Modified Paths:
--------------
    trunk/NSGraphics_SKExtensions.h
    trunk/NSGraphics_SKExtensions.m
    trunk/PDFView_SKExtensions.m
    trunk/SKMainWindowController.m

Modified: trunk/NSGraphics_SKExtensions.h
===================================================================
--- trunk/NSGraphics_SKExtensions.h     2026-08-31 16:13:19 UTC (rev 16406)
+++ trunk/NSGraphics_SKExtensions.h     2026-09-01 16:10:56 UTC (rev 16407)
@@ -67,4 +67,6 @@
 
 extern NSArray<CIFilter *> *SKColorEffectFilters(void);
 
+extern NSColor *SKPreInvertedColor(NSColor *color);
+
 NS_ASSUME_NONNULL_END

Modified: trunk/NSGraphics_SKExtensions.m
===================================================================
--- trunk/NSGraphics_SKExtensions.m     2026-08-31 16:13:19 UTC (rev 16406)
+++ trunk/NSGraphics_SKExtensions.m     2026-09-01 16:10:56 UTC (rev 16407)
@@ -186,7 +186,11 @@
 
 #define SKInvertedColorsBackgroundWhiteKey @"SKInvertedColorsBackgroundWhite"
 
-static CGFloat SKInvertedColorsBackgroundWhite() {
+static inline CGFloat sRGBToGamma16(CGFloat c) { return c <= 0.04045 ? pow(c / 
12.92, 0.625) : pow((c + 0.055) / 1.055, 1.5); }
+
+static inline CGFloat sRGBFromGamma16(CGFloat c) { return c <= 0.0031308 ? 
12.92 * pow(c, 1.6) : 1.055 * pow(c, 2.0/3.0) - 0.055; }
+
+static CGFloat invertedColorsFilterFactor() {
     static CGFloat backgroundWhite = -2.0;
     if (backgroundWhite < -1.0) {
         NSNumber *bgw = [[NSUserDefaults standardUserDefaults] 
objectForKey:SKInvertedColorsBackgroundWhiteKey];
@@ -195,10 +199,10 @@
         } else {
             backgroundWhite = fmax(0.0, fmin(1.0, [bgw  doubleValue]));
             // map from sRGB to gamma=1.6 colorspace
-            backgroundWhite = backgroundWhite <= 0.04045 ? pow(backgroundWhite 
/ 12.92, 0.625) : pow((backgroundWhite + 0.055) / 1.055, 1.5);
+            backgroundWhite = sRGBToGamma16(backgroundWhite);
         }
     }
-    return backgroundWhite;
+    return backgroundWhite >= 0.0 ? 2.0 - backgroundWhite : [[NSWorkspace 
sharedWorkspace] accessibilityDisplayShouldIncreaseContrast] ? 1.9338 : 1.8972;
 }
 
 NSArray *SKColorEffectFilters(void) {
@@ -217,8 +221,7 @@
     }
     if (SKHasDarkAppearance() && [sud boolForKey:SKInvertColorsInDarkModeKey]) 
{
         // map the white page background to 45/255, or 30/255 with high 
contrast
-        CGFloat bgw = SKInvertedColorsBackgroundWhite();
-        CGFloat f = bgw >= 0.0 ? 2.0 - bgw : [[NSWorkspace sharedWorkspace] 
accessibilityDisplayShouldIncreaseContrast] ? 1.9338 : 1.8972;
+        CGFloat f = invertedColorsFilterFactor();
         CGFloat lrf = LR * f, lgf = LG * f, lbf = LB * f;
         // This is like CIColorInvert + CIHueAdjust, modified to map white to 
dark gray rather than black
         // Inverts a linear luminocity with weights from the CIE standards
@@ -232,3 +235,18 @@
     }
     return filters;
 }
+
+NSColor *SKPreInvertedColor(NSColor *color) {
+    CGFloat f = invertedColorsFilterFactor();
+    if (f <= 1.0001)
+        return color;
+    CGFloat c[4] = {0.0, 0.0, 0.0, 1.0};
+    NSUInteger i;
+    [[color colorUsingColorSpace:[NSColorSpace sRGBColorSpace]] 
getComponents:c];
+    for (i = 0; i < 3; i++)
+        c[i] = sRGBToGamma16(c[i]);
+    CGFloat offset = (1.0 - f * (LR * c[0] + LG * c[1] + LB * c[2])) / (f - 
1.0);
+    for (i = 0; i < 3; i++)
+        c[i] = sRGBFromGamma16(fmin(1.0, fmax(0.0, c[i] + offset)));
+    return [NSColor colorWithSRGBRed:c[0] green:c[1] blue:c[2] alpha:c[3]];
+}

Modified: trunk/PDFView_SKExtensions.m
===================================================================
--- trunk/PDFView_SKExtensions.m        2026-08-31 16:13:19 UTC (rev 16406)
+++ trunk/PDFView_SKExtensions.m        2026-09-01 16:10:56 UTC (rev 16407)
@@ -301,8 +301,11 @@
 
 static NSColor *defaultBackgroundColor(NSString *backgroundColorKey, NSString 
*darkBackgroundColorKey) {
     NSColor *color = nil;
-    if (SKHasDarkAppearance())
+    if (SKHasDarkAppearance()) {
         color = [[NSUserDefaults standardUserDefaults] 
colorForKey:darkBackgroundColorKey];
+        if (color && [color type] == NSColorTypeComponentBased && 
[[NSUserDefaults standardUserDefaults] boolForKey:SKInvertColorsInDarkModeKey])
+            color = SKPreInvertedColor(color);
+    }
     if (color == nil)
         color = [[NSUserDefaults standardUserDefaults] 
colorForKey:backgroundColorKey];
     return color;

Modified: trunk/SKMainWindowController.m
===================================================================
--- trunk/SKMainWindowController.m      2026-08-31 16:13:19 UTC (rev 16406)
+++ trunk/SKMainWindowController.m      2026-09-01 16:10:56 UTC (rev 16407)
@@ -2551,6 +2551,7 @@
     NSUserDefaults *sud = [NSUserDefaults standardUserDefaults];
     for (NSString *key in @[SKBackgroundColorKey, 
SKFullScreenBackgroundColorKey,
                             SKDarkBackgroundColorKey, 
SKDarkFullScreenBackgroundColorKey,
+                            SKInvertColorsInDarkModeKey,
                             SKThumbnailSizeKey, SKSnapshotThumbnailSizeKey,
                             SKInterpolationQualityKey,
                             SKTableFontSizeKey])
@@ -2567,6 +2568,7 @@
     NSUserDefaults *sud = [NSUserDefaults standardUserDefaults];
     for (NSString *key in @[SKBackgroundColorKey, 
SKFullScreenBackgroundColorKey,
                             SKDarkBackgroundColorKey, 
SKDarkFullScreenBackgroundColorKey,
+                            SKInvertColorsInDarkModeKey,
                             SKThumbnailSizeKey, SKSnapshotThumbnailSizeKey,
                             SKInterpolationQualityKey,
                             SKTableFontSizeKey]) {
@@ -2647,7 +2649,7 @@
     if (context == &SKMainWindowDefaultsObservationContext) {
         
         // A default value that we are observing has changed
-        if ([keyPath isEqualToString:SKBackgroundColorKey] || [keyPath 
isEqualToString:SKDarkBackgroundColorKey]) {
+        if ([keyPath isEqualToString:SKBackgroundColorKey] || [keyPath 
isEqualToString:SKDarkBackgroundColorKey] || [keyPath 
isEqualToString:SKInvertColorsInDarkModeKey]) {
             NSColor *backgroundColor = nil;
             if (interactionMode == SKNormalMode)
                 backgroundColor = [PDFView defaultBackgroundColor];

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.



_______________________________________________
Skim-app-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/skim-app-commit

Reply via email to