• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Setting a color in a NSBitmapImageRep to white or black
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Setting a color in a NSBitmapImageRep to white or black


  • Subject: Setting a color in a NSBitmapImageRep to white or black
  • From: Eric Gorr <email@hidden>
  • Date: Thu, 17 Feb 2011 16:09:00 -0500

I've got this category method on NSColor:

+ (NSColor*) patternColorWithData:(unsigned char*)bits oneColor:(NSColor*)oneColor zeroColor:(NSColor*)zeroColor
{
    NSBitmapImageRep*   patternBitmap;

    patternBitmap = [[[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
                                                             pixelsWide:8
                                                             pixelsHigh:8
                                                          bitsPerSample:8
                                                        samplesPerPixel:3
                                                               hasAlpha:NO
                                                               isPlanar:NO
                                                         colorSpaceName:NSDeviceRGBColorSpace
                                                            bytesPerRow:0
                                                           bitsPerPixel:0] autorelease];

    NSColor*            patternColor    = nil;

    long                x, y;
    NSColor*            theColor;

    for ( y = 0; y < 8; y++ )
    {
        for ( x = 0; x < 8; x++ )
        {
            NSUInteger  bit = ( bits[y] & ( 1 << x ) );

            theColor = ( bit ) ? oneColor : zeroColor;

            [patternBitmap setColor:theColor atX:x y:y];
        }
    }

    NSImage* patternImage = [[[NSImage alloc] initWithSize:[patternBitmap size]] autorelease];
    [patternImage addRepresentation:patternBitmap];

    patternColor = [NSColor colorWithPatternImage:patternImage];

    return patternColor;
}

I invoke it by doing:

     unsigned char	bits[8] = { 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55 };

     duelPatternColor = [[NSColor patternColorWithData:bits oneColor:[NSColor blackColor] zeroColor:[NSColor whiteColor]] retain];

However, the patternColor is always solid black and, iI examine the bitmapData of patternBitmap, it remains just a bunch of zeros.

If I modify the code such that theColor is always [NSColor greenColor], I will get a solid green color. So, I know the call to setColor is doing something.

So, why can I not use [NSColor whiteColor]?


I'm sure I'm missing something simple, but what that is, I do not know...


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Setting a color in a NSBitmapImageRep to white or black
      • From: Graham Cox <email@hidden>
  • Prev by Date: Re: How to optimize image loading during core animation?
  • Next by Date: Re: How to optimize image loading during core animation?
  • Previous by thread: Re: Odd behavior / appearance
  • Next by thread: Re: Setting a color in a NSBitmapImageRep to white or black
  • Index(es):
    • Date
    • Thread