BezierPath Stroke Unexpected Results

Discussion in 'Mac Programming' started by Darkroom, Oct 9, 2008.

  1. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    1st, my setLineWidth isn't working at all... it will only give me 1 pixel width... don't really know why.

    2nd, i wasn't *really* expecting my IF/ELSE statement to work, but i notice that it does change up here and there between black and white strokes... essentially i'm trying to tell the computer that if the fillColor is a light color, to stroke it with black, and if it's a dark color, to stroke it with white.

    any thoughts?

    Code:
    - (void)swatchDragWithStroke:(NSRect)rect
    	{
    	[fillColor setFill];
    	NSRectFill(rect);
    	
    	NSBezierPath *pathThickness = [NSBezierPath bezierPathWithRect:rect]
    	[pathThickness setLineWidth:5];
    	
    	if (fillColor < [NSColor grayColor])
    		{
    		[[NSColor whiteColor] setStroke];
    		}
    		else
    		{
    		[[NSColor blackColor] setStroke];
    		}
    		
    	[NSBezierPath strokeRect:rect];
    	}
    
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    setLineWidth: is only being applied to the pathThickness object, not to all bezier paths. So instead of
    Code:
    [NSBezierPath strokeRect:rect];
    use
    Code:
    [pathThickness stroke];
    NSColors are just objects - you can't compare them with operators like you can primitive numbers. If you want to determine if a color is lighter than another, compare the individual components via [color redComponent], etc. You may need to convert the color first before accessing the components.
     
  3. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #3
    thanks...

    also, i decided to use a 1px Black + 1px White checkerboard pattern image as my rect's stroke instead... it works well...

    Code:
    - (void)swatchDragWithStroke:(NSRect)rect
    	{
    	[fillColor setFill];
    	NSRectFill(rect);
    	
    	NSBezierPath *pathThickness = [NSBezierPath bezierPathWithRect:rect];
    	[pathThickness setLineWidth:2];
     
    	NSImage *dottedStroke = [NSImage imageNamed:@"DottedStroke.png"];
    	NSColor *dottedColor = [NSColor colorWithPatternImage:dottedStroke];
    
    	[dottedColor setStroke];
    	[pathThickness stroke];
    	}
    
     

Share This Page