void doIndexedColorDrawGraphics(CGContextRef context) { CGColorSpaceRef theBaseRGBSpace = getTheCalibratedRGBColorSpace(); CGColorSpaceRef theIndexedSpace = NULL; unsigned char lookupTable[6]; float opaqueRed[] = { 0, 1 }; // index, alpha float aBlue[] = { 1, 1 }; // index, alpha // Set the first 3 values in the lookup table to a red of // 169/255 = 0.663, no green, and blue = 8/255 = 0.031. This makes // the first entry in the lookup table a shade of red. lookupTable[0] = 169; lookupTable[1] = 0; lookupTable[2] = 8; // Set the second 3 values in the lookup table to a red value // of 123/255 = 0.482, a green value of 158/255 = 0.62, and // a blue value of 222/255 = 0.871. This makes the second entry // in the lookup table a shade of blue. lookupTable[3] = 123; lookupTable[4] = 158; lookupTable[5] = 222; // Create the indexed color space with this color lookup table, // using the RGB color space as the base color space and a 2 element // color lookup table to characterize the indexed color space. theIndexedSpace = CGColorSpaceCreateIndexed(theBaseRGBSpace, 1, lookupTable); if(theIndexedSpace != NULL){ CGContextSetStrokeColorSpace(context, theIndexedSpace); CGContextSetFillColorSpace(context, theIndexedSpace); // Release the color space this code created since it is no // longer needed in this routine. CGColorSpaceRelease(theIndexedSpace); // Set the stroke color to an opaque blue. CGContextSetStrokeColor(context, aBlue); // Set the fill color to an opaque red. CGContextSetFillColor(context, opaqueRed); CGContextSetLineWidth(context, 8.); // Draw the first rectangle. CGContextBeginPath(context); CGContextAddRect(context, CGRectMake(20., 20., 100., 100.)); CGContextDrawPath(context, kCGPathFillStroke); // Continue to use the stroke colorspace already set // but change the stroke alpha value to a semitransparent value // while leaving the index value unchanged. aBlue[1] = 0.5; CGContextSetStrokeColor(context, aBlue); // Draw another rectangle to the right of the first one. CGContextBeginPath(context); CGContextAddRect(context, CGRectMake(140., 20., 100., 100.)); CGContextDrawPath(context, kCGPathFillStroke); }else fprintf(stderr, "Couldn't make the indexed color space!\n"); }
static CGContextRef createScratchContext() { CGDataConsumerCallbacks callbacks = { putBytesNowhere, 0 }; RetainPtr<CGDataConsumerRef> consumer(AdoptCF, CGDataConsumerCreate(0, &callbacks)); CGContextRef context = CGPDFContextCreate(consumer.get(), 0, 0); CGFloat black[4] = { 0, 0, 0, 1 }; CGContextSetFillColor(context, black); CGContextSetStrokeColor(context, black); return context; }
void doColorSpaceFillAndStroke(CGContextRef context) { CGColorSpaceRef theColorSpace = getTheCalibratedRGBColorSpace(); float opaqueRed[] = { 0.663, 0.0, 0.031, 1.0 }; // red,green,blue,alpha float aBlue[] = { 0.482, 0.62, 0.871, 1.0 }; // red,green,blue,alpha // Set the fill color space to be the generic calibrated RGB color space. CGContextSetFillColorSpace(context, theColorSpace); // Set the fill color to opaque red. The number of elements in the // array passed to this function must be the number of color // components in the current fill color space plus 1 for alpha. CGContextSetFillColor(context, opaqueRed); // Set the stroke color space to be the generic calibrated RGB color space. CGContextSetStrokeColorSpace(context, theColorSpace); // Set the stroke color to opaque blue. The number of elements // in the array passed to this function must be the number of color // components in the current stroke color space plus 1 for alpha. CGContextSetStrokeColor(context, aBlue); CGContextSetLineWidth(context, 8.); // Rectangle 1. CGContextBeginPath(context); CGContextAddRect(context, CGRectMake(20., 20., 100., 100.)); CGContextDrawPath(context, kCGPathFillStroke); // Continue to use the stroke colorspace already set // but change the stroke alpha value to a semitransparent blue. aBlue[3] = 0.5; CGContextSetStrokeColor(context, aBlue); // Rectangle 2. CGContextBeginPath(context); CGContextAddRect(context, CGRectMake(140., 20., 100., 100.)); CGContextDrawPath(context, kCGPathFillStroke); // Don't release the color space since this routine // didn't create it. }
CGContextRef scratchContext() { static CGContextRef scratch = 0; if (!scratch) { CFMutableDataRef empty = CFDataCreateMutable(NULL, 0); CGDataConsumerRef consumer = CGDataConsumerCreateWithCFData(empty); scratch = CGPDFContextCreate(consumer, NULL, NULL); CGDataConsumerRelease(consumer); CFRelease(empty); CGFloat black[4] = {0, 0, 0, 1}; CGContextSetFillColor(scratch, black); CGContextSetStrokeColor(scratch, black); } return scratch; }
void RSTileRendererDrawTile( CGContextRef c, CGRect bounds, uint8_t *data, uint32_t dataSize, uint32_t zoom, RSTileRendererGraphicsSpace graphicsSpace) { CGFloat width = (CGFloat)bounds.size.width; CGFloat height = (CGFloat)bounds.size.height; const CGFloat backgroundColor[4] = {0.5, 0.5, 0.5, 1}; const CGFloat fillColors[8][4] = { {0, 0, 0, 0}, {(210 / 255.0), (173 / 255.0), (104 / 255.0), 1}, // Building = 1, {(66 / 255.0), (66 / 255.0), (255 / 255.0), 1}, // HighwayMotorway = 2, {(255 / 255.0), (66 / 255.0), (66 / 255.0), 1}, // HighwayPrimary = 3, {(255 / 255.0), (155 / 255.0), (66 / 255.0), 1}, // HighwaySecondary = 4, {(255 / 255.0), (255 / 255.0), (66 / 255.0), 1}, // HighwayTertiary = 5, {(255 / 255.0), (255 / 255.0), (255 / 255.0), 1}, // HighwayResidential = 6, {(255 / 255.0), (255 / 255.0), (255 / 255.0), 1} // HighwayService = 7, }; const CGFloat strokeColors[8][4] = { {0, 0, 0, 0}, {(140 / 255.0), (93 / 255.0), (33 / 255.0), 1}, // Building = 1, {(44 / 255.0), (44 / 255.0), (200 / 255.0), 1}, // HighwayMotorway = 2, {(200 / 255.0), (44 / 255.0), (44 / 255.0), 1}, // HighwayPrimary = 3, {(200 / 255.0), (100 / 255.0), (44 / 255.0), 1}, // HighwaySecondary = 4, {(200 / 255.0), (200 / 255.0), (44 / 255.0), 1}, // HighwayTertiary = 5, {(200 / 255.0), (200 / 255.0), (200 / 255.0), 1}, // HighwayResidential = 6, {(200 / 255.0), (200 / 255.0), (200 / 255.0), 1} // HighwayService = 7, }; CGContextSetFillColor(c, backgroundColor); CGContextFillRect(c, bounds); uint32_t pos = 0; uint32_t resolutionBits = 8; double intToFloat = width / (1 << resolutionBits); int32_t ix, iy; double x, y, firstX, firstY; while (pos < dataSize) { ix = 0; iy = 0; RSTiledataThingType thingType = (RSTiledataThingType)RSVarintRead(data, &pos); uint32_t numNodes = RSVarintRead(data, &pos); for (uint32_t i = 0; i < numNodes; i++) { ix += RSVarintSignedRead(data, &pos); iy += RSVarintSignedRead(data, &pos); x = intToFloat * ix; y = intToFloat * iy; if (graphicsSpace == RSTileRendererGraphicsSpaceQuadrantOne) { y = height - y; } if (i == 0) { firstX = x; firstY = y; CGContextMoveToPoint(c, x, y); } else { CGContextAddLineToPoint(c, x, y); } } CGContextSetFillColor(c, fillColors[thingType]); CGContextSetStrokeColor(c, strokeColors[thingType]); switch (thingType) { case RSTiledataBuilding: CGContextSetLineWidth(c, 4); break; case RSTiledataHighwayMotorway: case RSTiledataHighwayPrimary: case RSTiledataHighwaySecondary: case RSTiledataHighwayTertiary: case RSTiledataHighwayResidential: case RSTiledataHighwayService: CGContextSetLineWidth(c, 12); break; } if (RSTiledataThingTypeIsClosedWay(thingType)) { CGContextAddLineToPoint(c, firstX, firstY); CGContextDrawPath(c, kCGPathFillStroke); } else { CGContextStrokePath(c); } } }
void CGContextSetStrokeColor_wrap(CGContext *con, float c1, float c2, float c3, float alpha) { float color[4] = {c1, c2, c3, alpha}; CGContextSetStrokeColor(con, color); }