# BWToolkit

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79``` ```// // NSColor+PixelWideLines.m // BWToolkit // // Created by Brandon Walkin (www.brandonwalkin.com) // All code is provided under the New BSD license. // #import "NSColor+BWAdditions.h" @implementation NSColor (BWAdditions) // Use this method to draw 1 px wide lines independent of scale factor. Handy for resolution independent drawing. Still needs some work - there are issues with drawing at the edges of views. - (void)bwDrawPixelThickLineAtPosition:(int)posInPixels withInset:(int)insetInPixels inRect:(NSRect)aRect inView:(NSView *)view horizontal:(BOOL)isHorizontal flip:(BOOL)shouldFlip { // Convert the given rectangle from points to pixels aRect = [view convertRectToBase:aRect]; // Round up the rect's values to integers aRect = NSIntegralRect(aRect); // Add or subtract 0.5 so the lines are drawn within pixel bounds if (isHorizontal) { if ([view isFlipped]) aRect.origin.y -= 0.5; else aRect.origin.y += 0.5; } else { aRect.origin.x += 0.5; } NSSize sizeInPixels = aRect.size; // Convert the rect back to points for drawing aRect = [view convertRectFromBase:aRect]; // Flip the position so it's at the other side of the rect if (shouldFlip) { if (isHorizontal) posInPixels = sizeInPixels.height - posInPixels - 1; else posInPixels = sizeInPixels.width - posInPixels - 1; } float posInPoints = posInPixels / [[NSScreen mainScreen] userSpaceScaleFactor]; float insetInPoints = insetInPixels / [[NSScreen mainScreen] userSpaceScaleFactor]; // Calculate line start and end points float startX, startY, endX, endY; if (isHorizontal) { startX = aRect.origin.x + insetInPoints; startY = aRect.origin.y + posInPoints; endX = aRect.origin.x + aRect.size.width - insetInPoints; endY = aRect.origin.y + posInPoints; } else { startX = aRect.origin.x + posInPoints; startY = aRect.origin.y + insetInPoints; endX = aRect.origin.x + posInPoints; endY = aRect.origin.y + aRect.size.height - insetInPoints; } // Draw line NSBezierPath *path = [NSBezierPath bezierPath]; [path setLineWidth:0.0f]; [path moveToPoint:NSMakePoint(startX,startY)]; [path lineToPoint:NSMakePoint(endX,endY)]; [self set]; [path stroke]; } @end ```