Skip to content

Commit ce28511

Browse files
committed
Good tab colors based on default fore/back colors
1 parent eeeb43b commit ce28511

1 file changed

Lines changed: 21 additions & 15 deletions

File tree

src/MacVim/MMTabline/MMTabline.m

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -263,25 +263,31 @@ - (void)setTablineSelBackground:(NSColor *)back foreground:(NSColor *)fore
263263
return;
264264
}
265265

266-
self.tablineSelBgColor = back;
267-
self.tablineSelFgColor = fore;
268-
269266
// Set the colors for the tabline based on the default background and
270-
// foreground colors. We get the brightness component of back and fore
271-
// and then adjust up or down depending on whether the color is more
272-
// or less than 50% bright.
273-
CGFloat h, s, b, b2, b3, b4, a;
267+
// foreground colors. Calculate brightness according to a formula from
268+
// the W3C that gives brightness as the eye perceives it. Then lighten
269+
// or darken the default colors based on whether brightness is greater
270+
// than 50% to achieve good visual contrast.
271+
// www.w3.org/WAI/ER/WD-AERT/#color-contrast
272+
CGFloat r, g, b, brightness;
273+
[back getRed:&r green:&g blue:&b alpha:NULL];
274+
brightness = r * 0.299 + g * 0.114 + b * 0.587;
274275

275-
[back getHue:&h saturation:&s brightness:&b alpha:&a];
276-
b3 = (b > 0.5) ? b - 0.25 : b + 0.35;
277-
self.tablineFillFgColor = [NSColor colorWithHue:h saturation:s brightness:b3 alpha:a];
276+
self.tablineSelBgColor = back;
277+
278+
self.tablineSelFgColor = (brightness > 0.5)
279+
? [fore blendedColorWithFraction:0.6 ofColor:NSColor.blackColor]
280+
: [fore blendedColorWithFraction:0.6 ofColor:NSColor.whiteColor];
281+
282+
self.tablineBgColor = (brightness > 0.5)
283+
? [back blendedColorWithFraction:0.12 ofColor:NSColor.blackColor]
284+
: [back blendedColorWithFraction:0.20 ofColor:NSColor.whiteColor];
278285

279-
b2 = (b3 + b) / 2.;
280-
self.tablineBgColor = [NSColor colorWithHue:h saturation:s brightness:b2 alpha:a];
286+
self.tablineFgColor = [self.tablineSelFgColor blendedColorWithFraction:0.5 ofColor:self.tablineBgColor];
281287

282-
[fore getHue:&h saturation:&s brightness:&b alpha:&a];
283-
b4 = (b2 > 0.5) ? b2 - 0.25 : b2 + 0.25;
284-
self.tablineFgColor = [NSColor colorWithHue:h saturation:s brightness:b4 alpha:a];
288+
self.tablineFillFgColor = (brightness > 0.5)
289+
? [back blendedColorWithFraction:0.25 ofColor:NSColor.blackColor]
290+
: [back blendedColorWithFraction:0.40 ofColor:NSColor.whiteColor];
285291
}
286292

287293
#pragma mark - Helpers

0 commit comments

Comments
 (0)