• 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
Quartz 2d and Colour
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Quartz 2d and Colour


  • Subject: Quartz 2d and Colour
  • From: David Burnett <email@hidden>
  • Date: Wed, 11 Jun 2003 22:06:23 +0100

Here's a daft question.

How do you draw in colour using Quartz2d.

I'm using the source below to draw to a bitmap which will be used as a
background cache to avoid redrawing all the vector stuff as I write over it.

The trouble is everything I draw is grey scale, I'm sure its something
simple, but like most programming documentation there's not much help for
when things do not work as expected.

Dave

- (id)initWithFrame:(NSRect)frameRect
{
int i;
int bitmapByteCount;
int bitmapBytesPerRow;
[super initWithFrame:frameRect];
NSRect myBounds = [self bounds];

CGColorSpaceRef colorSpace;


bitmapBytesPerRow = (myBounds.size.width * 4);
bitmapByteCount = (bitmapBytesPerRow * myBounds.size.height);

// create an RGB color space
colorSpace = CGColorSpaceCreateDeviceRGB();
// create the bitmap
bitmapData = malloc( bitmapByteCount );

if( bitmapData != NULL )
{
// create the context
context = CGBitmapContextCreate( bitmapData,
myBounds.size.width,
myBounds.size.height,
8, // bits per component
bitmapBytesPerRow,
colorSpace,
kCGImageAlphaPremultipliedLast);
if( context != NULL )
{
imageRep = [[NSBitmapImageRep alloc]initWithBitmapDataPlanes:(unsigned char **)&bitmapData
pixelsWide:myBounds.size.width
pixelsHigh:myBounds.size.height
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSDeviceRGBColorSpace
bytesPerRow:bitmapBytesPerRow
bitsPerPixel:32];

viewCache = [[NSImage alloc]init];
[viewCache addRepresentation:imageRep];

}
else
{
// the context couldnt be created for some reason,
// and we have no use for the bitmap without the context
free( bitmapData );
}
}
// the context retains the color space, so we can release it
CGColorSpaceRelease( colorSpace );

return self;
}

- (void)setCache:(NSMutableArray *)shapes
{
int i;
float fillRGB[4];
float strokeRBG[4];

NSRect myBounds = [self bounds];
// this is not grey no matter which byte order we pick
fillRGB[0] = 1;
fillRGB[1] = 0.5;
fillRGB[2] = 0;
fillRGB[3] = 1;

// this is not grey
strokeRBG[0] = 1;
strokeRBG[1] = 1;
strokeRBG[2] = 0;
strokeRBG[3] = 1;

CGContextSetFillColor(context,fillRGB);
CGContextSetStrokeColor(context,strokeRBG);
CGContextClearRect(context, CGRectMake(myBounds.origin.x,myBounds.origin.x,myBounds.size.width, myBounds.size.height));
CGContextFillRect(context, CGRectMake(myBounds.origin.x,myBounds.origin.x,myBounds.size.width, myBounds.size.height));

CGContextBeginPath(context);

for(i=0; i<400; i+=8) {
CGContextAddArc(context, i, i, 20 ,0 ,2*M_PI, 1);
}

CGContextDrawPath(context, kCGPathFillStroke);
CGContextFillPath(context);
[self setNeedsDisplay:YES];
}

- (void)drawRect:(NSRect)rect
{

[viewCache drawInRect:rect fromRect:rect operation:NSCompositeCopy fraction:1.0];

}
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: Quartz 2d and Colour
      • From: "John C. Randolph" <email@hidden>
  • Prev by Date: Character set usage
  • Next by Date: Re: troubling article
  • Previous by thread: Character set usage
  • Next by thread: Re: Quartz 2d and Colour
  • Index(es):
    • Date
    • Thread