Professional Web Applications Themes

Cocoa color with alpha weirdness - Mac Programming

"Liz S. Reynolds" wrote: > > Code snippet below. It goes in a drawRect method in a subclass of NSView. > > The first path fills a rectangle in the right half of the view colored > transparent blue. the second path fills a rectangle in the bottom half of > the view colored transparent yellow. Either of these by itself looks just > like you'd expect. > > +------------------+ > | no color | blue | > |------------------| > | no color | blue | > +------------------+ > > +---------------------+ > | no color | no color | > ...

  1. #1

    Default Re: Cocoa color with alpha weirdness

    "Liz S. Reynolds" wrote:
    >
    > Code snippet below. It goes in a drawRect method in a subclass of NSView.
    >
    > The first path fills a rectangle in the right half of the view colored
    > transparent blue. the second path fills a rectangle in the bottom half of
    > the view colored transparent yellow. Either of these by itself looks just
    > like you'd expect.
    >
    > +------------------+
    > | no color | blue |
    > |------------------|
    > | no color | blue |
    > +------------------+
    >
    > +---------------------+
    > | no color | no color |
    > |---------------------|
    > | yellow | yellow |
    > +---------------------+
    >
    > When both are drawn, they intersect in the bottom right
    > quadrant of the view. However, the quartered view looks like this:
    >
    > +------------------+
    > | no color | green |
    > |------------------|
    > | yellow | blue |
    > +------------------+
    >
    > while I expected this:
    >
    > +------------------+
    > | no color | blue |
    > |------------------|
    > | yellow | green |
    > +------------------+
    >
    > I'm totally puzzled, any clues?
    Liz,

    What's tripping you up here is that unlike in Postscript, where the
    "fill" operator consumes and destroys the current path, NSBezierPath
    doesn't lose any of its state after you fill it. So, the second
    sequence of -moveToPoint:/-lineToPoint: messages that you're sending to
    the path are just getting appended to the existing path.
    >
    > // top left bottom and right are args to the function
    > // and represent the origin and origin + size in each direction
    > NSColor *blue ;
    > NSColor *yellow;
    >
    > NSBezierPath *thePath = [[ NSBezierPath alloc ] init];
    > blue = [[NSColor blueColor] colorWithAlphaComponent: 0.5];
    > yellow = [[NSColor yellowColor] colorWithAlphaComponent: 0.5];
    >
    > // down the center
    > [ blue set];
    > [thePath moveToPoint: NSMakePoint ((right / 2),top)];
    > [thePath lineToPoint: NSMakePoint ((right / 2),bottom)];
    > // bottom to left edge
    > [thePath lineToPoint: NSMakePoint (right,bottom)];
    > // bottom left to top left
    > [thePath lineToPoint: NSMakePoint (right,top)];
    > // top left to top center
    > [thePath lineToPoint: NSMakePoint ((right / 2),top)];
    > [thePath closePath];
    > [thePath fill];
    Try adding:

    [thePath removeAllPoints]; //here.

    >
    > [ yellow set];
    > // across the center
    > [thePath moveToPoint: NSMakePoint (left, ((top - bottom) / 2))];
    > [thePath lineToPoint: NSMakePoint (right,((top - bottom) / 2))];
    > // center right to bottom right
    > [thePath lineToPoint: NSMakePoint (right,bottom)];
    > // bottom right to bottom left
    > [thePath lineToPoint: NSMakePoint (left,bottom)];
    > // up to center left
    > [thePath lineToPoint: NSMakePoint (left, (top - bottom) / 2)];
    > [thePath closePath];
    > [thePath fill];
    Also, if you just want to fill in some rectangles, you don't need the
    overhead of creating an NSBezierPath. You can just call NSRectFill() or
    NSRectFillList(), or send +fillRect: to the NSBezierPath class itself.

    HTH,

    -jcr
    John C. Randolph Guest

  2. #2

    Default Re: Cocoa color with alpha weirdness

    In article <3F4BD291.32BB9A6Dnospam.idiom.com>,
    >
    >What's tripping you up here is that unlike in Postscript, where the
    >"fill" operator consumes and destroys the current path, NSBezierPath
    >doesn't lose any of its state after you fill it. So, the second
    >sequence of -moveToPoint:/-lineToPoint: messages that you're sending to
    >the path are just getting appended to the existing path.
    >> [thePath fill];
    >
    >Try adding:
    >
    >[thePath removeAllPoints]; //here.
    >
    That did it - thanks!
    >
    >Also, if you just want to fill in some rectangles, you don't need the
    >overhead of creating an NSBezierPath. You can just call NSRectFill() or
    >NSRectFillList(), or send +fillRect: to the NSBezierPath class itself.
    It's just rectangles for practice, more complex shapes later, but thanks
    for the tip.

    -Liz



    Liz S. Reynolds Guest

Similar Threads

  1. Background Image with Alpha + background color?
    By trytertqwr in forum HTML & CSS
    Replies: 3
    Last Post: February 17th, 11:14 AM
  2. #40572 [NEW]: Alpha composite allows color to bleed through
    By seth at pricepages dot org in forum PHP Bugs
    Replies: 4
    Last Post: February 23rd, 12:50 AM
  3. Alpha color value?
    By bech in forum Macromedia Flash Actionscript
    Replies: 3
    Last Post: January 27th, 07:08 PM
  4. Q About Cocoa & CF Prefs..
    By David Phillip Oster in forum Mac Programming
    Replies: 2
    Last Post: July 31st, 12:04 PM
  5. Cocoa Filemaker
    By Olly Groves in forum FileMaker
    Replies: 2
    Last Post: July 25th, 01:55 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139