Get the window titlebar background color

Hi,
on my macOS app I would like to set the window background color as the titlebar background color such a way to get a unique color all over the window on both light and dark mode.

But I cannot find the way to read the titlebar background color nor I found a standard color name for that. How can I do that?
Answered by Claude31 in 664417022
I think that titlebar has in fact the color of the window background, plus a non transparent effect that dims the bg color.

Making the titlebar transparent will give it the exact color of background.

You could try this (I tested, got a uniform red color):
window?.titlebarAppearsTransparent = true // gives it "flat" look
window?.backgroundColor = .red // set the background color
https://stackoverflow.com/questions/47759424/how-to-colour-the-titlebar-of-nswindow

Or look at this:
https://stackoverflow.com/questions/47759424/how-to-colour-the-titlebar-of-nswindow

Accepted Answer
I think that titlebar has in fact the color of the window background, plus a non transparent effect that dims the bg color.

Making the titlebar transparent will give it the exact color of background.

You could try this (I tested, got a uniform red color):
window?.titlebarAppearsTransparent = true // gives it "flat" look
window?.backgroundColor = .red // set the background color
https://stackoverflow.com/questions/47759424/how-to-colour-the-titlebar-of-nswindow

Or look at this:
https://stackoverflow.com/questions/47759424/how-to-colour-the-titlebar-of-nswindow

Thank you Claude. In the window.contentView subclass I set

Code Block
- (void)awakeFromNib
{
self.window.titlebarAppearsTransparent = true;
}
- (void)drawRect:(NSRect)rect
{
    [self.window.backgroundColor set];
    NSRectFill(rect);
}

but the titlebar has still a lighter color than the background color. By chance I discovered that if I set

Code Block
- (void)awakeFromNib
{
self.window.titlebarAppearsTransparent = true;
self.window.backgroundColor = [NSColor controlBackgroundColor];
/* I even tried the color [NSColor windowBackgroundColor] but the titleBar has still a lighter color. */
}
- (void)drawRect:(NSRect)rect
{
    [self.window.backgroundColor set];
    NSRectFill(rect);
}

It works well for both light and dark mode on my macOS Big Sur 11.2. Will this work well on any macOS version? Even on Mojave and Catalina?
titlebarAppearsTransparent exists since MacOS 10.10.

So I do not see a reason why this would not work on later OS.

but the titlebar has still a lighter color than the background color. By chance I discovered that if I set
When I test in an app (Swift), even without setting bg color for the window, everything is the same color.

When you add:
Code Block
self.window.backgroundColor = [NSColor controlBackgroundColor];

Does it change the content color ?
If I set any background color, it always work, no matter the color I choose, the background color is always equal to the background titlebar color. e.g. self.window.backgroundColor = anyColor;

Anyway, actually I simply turn ON the NSWindow "Transparent Title Bar" check-box on IB. I use no code at all.
So when the window is active I get a window background color equal to the background title bar RGB color as 40,40,40 on dark mode and 246,246,246 on light mode.

I noticed that the other windows as e.g. the System Preferences window, when active, have these RGB colours: 54,54,54 on dark mode and 252,252,252 on light mode. So, the color is indeed different. Unique (titlebar/back) but different than on the other windows.

@LeonardoDev, Could you solve this issue? In my app, the color of title bar is slightly lighter than the main content view.

Get the window titlebar background color
 
 
Q