Skip to content

Commit

Permalink
Use lighter scrollbar scolor when background is dark.
Browse files Browse the repository at this point in the history
  • Loading branch information
gnachman committed Jul 23, 2011
1 parent ebc2c46 commit a20e416
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Headers/iTerm/PTYScrollView.h
Expand Up @@ -33,6 +33,9 @@
{
BOOL userScroll;
}

@property (nonatomic, assign) BOOL hasDarkBackground;

+ (BOOL)isCompatibleWithOverlayScrollers;
- (id)init;
- (void) mouseDown: (NSEvent *)theEvent;
Expand Down
1 change: 1 addition & 0 deletions Headers/iTerm/PTYTextView.h
Expand Up @@ -559,5 +559,6 @@ typedef enum {
// Returns true if any onscreen char is blinking.
- (BOOL)_markChangedSelectionAndBlinkDirty:(BOOL)redrawBlink width:(int)width;

- (double)_perceivedBrightness:(NSColor*)c;
@end

16 changes: 16 additions & 0 deletions NSBitmapImageRep+CoreImage.h
@@ -0,0 +1,16 @@
// From http://www.cocoadev.com/index.pl?NSImageCategory
//
// NSBitmapImageRep+CoreImage.h
// iTerm2
//

#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>

@interface NSBitmapImageRep (CoreImage)
/* Draws the specified image representation using Core Image. */
- (void)drawAtPoint:(NSPoint)point
fromRect:(NSRect)fromRect
coreImageFilter:(NSString *)filterName
arguments:(NSDictionary *)arguments;
@end
68 changes: 68 additions & 0 deletions NSBitmapImageRep+CoreImage.m
@@ -0,0 +1,68 @@
// From http://www.cocoadev.com/index.pl?NSImageCategory
// NSBitmapImageRep+CoreImage.m
// iTerm2

#import <QuartzCore/QuartzCore.h>
#import "NSBitmapImageRep+CoreImage.h"
#import "NSImage+CoreImage.h"

#define CIIMAGE_PADDING 16.0f

@implementation NSBitmapImageRep (CoreImage)

- (void)drawAtPoint: (NSPoint)point fromRect: (NSRect)fromRect coreImageFilter: (NSString *)filterName arguments: (NSDictionary *)arguments {
NSAutoreleasePool *pool;
CIFilter *filter;
CIImage *before;
CIImage *after;
CIContext *ciContext;
CGContextRef cgContext;

pool = [[NSAutoreleasePool alloc] init];
before = nil;

@try {
before = [[CIImage alloc] initWithBitmapImageRep: self];
if (before) {
filter = [CIFilter filterWithName: filterName];
[filter setDefaults];
if (arguments)
[filter setValuesForKeysWithDictionary: arguments];
[filter setValue: before forKey: @"inputImage"];
} else {
filter = nil;
}

after = [filter valueForKey: @"outputImage"];
if (after) {
if (![[arguments objectForKey: @"gt_noRenderPadding"] boolValue]) {
/* Add a wide berth to the bounds -- the padding can be turned
off by passing an NSNumber with a YES value in the argument
"gt_noRenderPadding" in the argument dictionary. */
fromRect.origin.x -= CIIMAGE_PADDING;
fromRect.origin.y -= CIIMAGE_PADDING;
fromRect.size.width += CIIMAGE_PADDING * 2.0f;
fromRect.size.height += CIIMAGE_PADDING * 2.0f;
point.x -= CIIMAGE_PADDING;
point.y -= CIIMAGE_PADDING;
}

cgContext = CGContextRetain((CGContextRef)[[NSGraphicsContext currentContext] graphicsPort]);
if (cgContext) {
ciContext = [CIContext contextWithCGContext: cgContext options: nil];
[ciContext
drawImage: after
atPoint: *(CGPoint *)(&point)
fromRect: *(CGRect *)(&fromRect)];
CGContextRelease(cgContext);
}
}
} @catch (NSException *e) {
NSLog("exception encountered during core image filtering: %@", e);
} @finally {
[before release];
}

[pool release];
}
@end
16 changes: 16 additions & 0 deletions NSImage+CoreImage.h
@@ -0,0 +1,16 @@
// From http://www.cocoadev.com/index.pl?NSImageCategory
//
// NSImage+CoreImage.h
// iTerm2

#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>

@interface NSImage (CoreImage)
/* Draws the specified image using Core Image. */
- (void)drawAtPoint: (NSPoint)point fromRect: (NSRect)fromRect coreImageFilter: (NSString *)filterName arguments: (NSDictionary *)arguments;

/* Gets a bitmap representation of the image, or creates one if the image does not have any. */
- (NSBitmapImageRep *)bitmapImageRepresentation;
@end

53 changes: 53 additions & 0 deletions NSImage+CoreImage.m
@@ -0,0 +1,53 @@
// From http://www.cocoadev.com/index.pl?NSImageCategory
//
// NSImage+CoreImage.m
// iTerm2

#import "NSImage+CoreImage.h"

@implementation NSImage (CoreImage)
- (void)drawAtPoint: (NSPoint)point fromRect: (NSRect)fromRect coreImageFilter: (NSString *)filterName arguments: (NSDictionary *)arguments {
NSAutoreleasePool *pool;
NSBitmapImageRep *rep;

pool = [[NSAutoreleasePool alloc] init];

if (filterName) {
rep = [self bitmapImageRepresentation];
[rep
drawAtPoint: point
fromRect: fromRect
coreImageFilter: filterName
arguments: arguments];
} else {
/* bypass core image if no filter is specified */
[self
drawAtPoint: point
fromRect: fromRect
operation: NSCompositeSourceOver
fraction: 1.0f];
}

[pool release];
}

- (NSBitmapImageRep *)bitmapImageRepresentation {
NSImageRep *rep;
NSEnumerator *e;
Class bitmapImageRep;

bitmapImageRep = [NSBitmapImageRep class];
e = [[self representations] objectEnumerator];
while ((rep = [e nextObject]) != nil) {
if ([rep isKindOfClass: bitmapImageRep])
break;
rep = nil;
}

if (!rep)
rep = [NSBitmapImageRep imageRepWithData: [self TIFFRepresentation]];

return (NSBitmapImageRep *)rep;
}

@end
28 changes: 28 additions & 0 deletions PTYScrollView.m
Expand Up @@ -35,9 +35,12 @@
#import <iTerm/PTYScrollView.h>
#import <iTerm/PTYTextView.h>
#import <PreferencePanel.h>
#import <Cocoa/Cocoa.h>

@implementation PTYScroller

@synthesize hasDarkBackground;

- (id)init
{
userScroll=NO;
Expand Down Expand Up @@ -97,6 +100,31 @@ - (NSScrollerPart)hitPart
return [super hitPart];
}

- (void)drawRect:(NSRect)dirtyRect {
if (IsLionOrLater() && self.hasDarkBackground) {
NSImage *superDrawn = [[NSImage alloc] initWithSize:NSMakeSize(dirtyRect.origin.x + dirtyRect.size.width,
dirtyRect.origin.y + dirtyRect.size.height)];
[superDrawn lockFocus];
[super drawRect:dirtyRect];
[superDrawn unlockFocus];

NSImage *temp = [[NSImage alloc] initWithSize:[superDrawn size]];
[temp lockFocus];
[superDrawn drawAtPoint:dirtyRect.origin
fromRect:dirtyRect
coreImageFilter:@"CIColorControls"
arguments:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithDouble:0.5], @"inputBrightness", nil]];
[temp unlockFocus];

[temp drawAtPoint:dirtyRect.origin
fromRect:dirtyRect
operation:NSCompositeCopy
fraction:1.0];
} else {
[super drawRect:dirtyRect];
}
}

@end

@implementation PTYScrollView
Expand Down
12 changes: 8 additions & 4 deletions PTYTextView.m
Expand Up @@ -66,6 +66,7 @@
// Minimum distance that the mouse must move before a cmd+drag will be
// recognized as a drag.
static const int kDragThreshold = 3;
static const double kBackgroundConsideredDarkThreshold = 0.5;

// When drawing lines, we use this structure to represent a run of cells of
// the same font, color, and attributes.
Expand Down Expand Up @@ -113,6 +114,10 @@
static NSImage* wrapToTopImage = nil;
static NSImage* wrapToBottomImage = nil;

static CGFloat PerceivedBrightness(CGFloat r, CGFloat g, CGFloat b) {
return (RED_COEFFICIENT * r) + (GREEN_COEFFICIENT * g) + (BLUE_COEFFICIENT * b);
}

@interface Coord : NSObject
{
@public
Expand Down Expand Up @@ -425,6 +430,9 @@ - (void)setBGColor:(NSColor*)color
[defaultBGColor release];
[color retain];
defaultBGColor = color;
PTYScroller *scroller = (PTYScroller*)[[[dataSource session] SCROLLVIEW] verticalScroller];
BOOL isDark = ([self _perceivedBrightness:color] < kBackgroundConsideredDarkThreshold);
[scroller setHasDarkBackground:isDark];
[self setNeedsDisplay:YES];
}

Expand Down Expand Up @@ -4477,10 +4485,6 @@ - (int)_setGlyphsInRun:(CharRun*)charRunArray
return newRuns;
}

static CGFloat PerceivedBrightness(CGFloat r, CGFloat g, CGFloat b) {
return (RED_COEFFICIENT * r) + (GREEN_COEFFICIENT * g) + (BLUE_COEFFICIENT * b);
}

- (double)_perceivedBrightness:(NSColor*) c
{
return PerceivedBrightness([c redComponent], [c greenComponent], [c blueComponent]);
Expand Down

0 comments on commit a20e416

Please sign in to comment.