Cocoa color with alpha weirdness

Ask a Question related to Mac Programming, Design and Development.

  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. Similar Questions and Discussions

    1. #40572 [NEW]: Alpha composite allows color to bleed through
      From: seth at pricepages dot org Operating system: Mac 10.4 PHP version: 5.2.1 PHP Bug Type: GD related Bug description: ...
    2. Alpha color value?
      Is there any COLOR code like 0xFFFFFF that means "0% alpha"?
    3. Bit shifts in Cocoa
      Does anyone know how to do bit shifts in Cocoa? I have declared an integer and I want to shift it one bit to the right (same as dividing by 2). ...
    4. Q About Cocoa & CF Prefs..
      In article <no-EA814A.20172229072003@netnews.upenn.edu>, Mark Haase <no@spam.please> wrote: Not only that, if the same preference file is...
    5. [Cocoa] Bug with NSTableView?
      > I'll check these accessor methods, as this may be the problem. It was the setCol* methods that were causing the problem. I stupidly put:...
  3. #2

    Default Re: Cocoa color with alpha weirdness

    In article <3F4BD291.32BB9A6D@nospam.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

Posting Permissions

  • You may not post new threads
  • You may 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