How exactly Windows 10 detects if it should use a black or white title?
Asked Answered
N

0

7

With the latest stable build of Windows 10 (10586), the color of the title of Win32 windows changes respecting the color of the window chrome.

For example, if the window color is darker, the title uses a white color. Vice-versa.

So, since I want that that kind of behavior, I had to get the brightness of the color of the window chrome.

By using this method:

public static int GetBrightness(this Color c)
{
    return (int)Math.Sqrt(
    c.R * c.R * .241 +
    c.G * c.G * .691 +
    c.B * c.B * .068);
}

And I was using this other method:

public static float GetBrightness2(this Color color)
{
    float num = ((float)color.R) / 255f;
    float num2 = ((float)color.G) / 255f;
    float num3 = ((float)color.B) / 255f;
    float num4 = num;
    float num5 = num;
    if (num2 > num4)
        num4 = num2;
    if (num3 > num4)
        num4 = num3;
    if (num2 < num5)
        num5 = num2;
    if (num3 < num5)
        num5 = num3;
    return ((num4 + num5) / 2f);
}

So all I had to do was test all of the colors to get a threshold (First method, second method):

White Title: 
127 0,58
120 0,55
105 0,39
127 0,34
101 0,43
109 0,43
105 0,44
86 0,31
105 0,36
83 0,32
68 0,26
136 0,47
106 0,41
108 0,44
109 0,42
110 0,39
89 0,34
108 0,58
105 0,25
83 0,34
75 0,29
108 0,39
106 0,40
108 0,51

Black Title:
189 0,47
161 0,47
138 0,65
150 0,35
143 0,32
138 0,58
161 0,37
167 0,45

Only a few bunch of colors resulted in a black title color and the threshold apparently is 137.

The problem happened when I selected the Automatically pick an accent color from my background:

Automatic, Black Title:
132 0,37 Color: {#FF94842D}

As you can see, 132 is smaller than 137, yet it shows a black title. So my GetBrightness() method may differ from the one that Windows uses.

So, how exactly Windows 10 decides what color the title should use?

Nonobservance answered 2/3, 2016 at 3:56 Comment(9)
Since we're talking about brightness here, try testing with HSL/HSV instead of RGB.Cramp
@Cramp That's what I was led to believe with the GetBrightness() method.Nonobservance
Ah, I overlooked that. My bad.Cramp
Alright, I just don't know which one is the correct, since internet provides me both. I may look directly into a "official" source such as Microsoft or W3C.Nonobservance
Why do you calculate brightness with quadratics? That's not the way to calculate itHouseholder
@SamiKuhmonen I didn't know :ONonobservance
So, why exactly someone voted to close this question?Nonobservance
See: stackoverflow.com/a/596243Threshold
@koshinae Yes, I used that same code, but it still does not match the window scheme.Nonobservance

© 2022 - 2024 — McMap. All rights reserved.