Pattern colors and alpha

  • I'm trying to create a hatch pattern to be drawn over some other
    graphics in a custom view.

    Researching how to draw hatches (at least in Cocoa, not the underlying
    Quartz 2D), suggested that the way to go was to create an appropriate
    image and then use NSColor's colorWithPatternImage: to generate a
    color that can be use to fill out the required area e.g. with
    NSRectFill.

    In my code, I draw the background, and some other objects, then I do
    the NSRectFill having set this color.  Indeed, the result is that my
    hatch appears in the correct place, but unfortunately the bits between
    the black bars of the hatch (which I'd left fully transparent in my
    source image driving the color) renders as completely transparent
    through the window.  Stuff that is beneath the window (i.e. other
    applications and the desktop) show right through.

    Now, I've experimented a bit with different compositing modes in the
    active NSGraphicsContext, but the NSRectFill with the pattern color
    seems to completely ignore this (in contrast to other drawing that I
    can see is respecting the asserted composition mode).

    Could it be that pattern colors NEVER respect the composition mode?
    If so, how is one supposed to draw a hatch over other graphics?
    Presumably one could simply render the pattern image over the graphics
    (I know that will work), but AFAIK this would mean doing any tiling
    myself, which is the whole point of a pattern IMHO.  Another option
    might be to use the color to paint into an image (getting the alpha
    there), then painting the appropriately sized image over the rest off
    my graphics - this offends my developer's sense of frugality.

    Anyway, there's every chance I'm just being dumb - which is easily
    overcome by somebody telling me my glaring oversight and I'll be on my
    way ;-)

    -- Lwe
  • > Anyway, there's every chance I'm just being dumb - ...
    >

    Indeed there is.

    The moral of the story here is never to assume.
    I _assumed_ (and partly this was a false sense of familiarity) that
    NSRectFill would use whatever compositing operation was set in the
    graphics context.

    Apparently not.

    Once RTFM occurred, it was plain that this function forces
    NSCompositeCopy, and I clearly wanted NSCompositeSourceOver

    Ergo, no image is required - using the more general
    NSRectFillUsingOperation I can now bang my hatch pattern with its
    alpha right over the top of any previously drawn graphics - with
    expected results.  Hooray, this n00b gets to move on to the next
    conundrum ;-)

    -- Lwe
previous month january 2008 next month
MTWTFSS
  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      
Go to today