//============================================================================ // NCGShading::UpdateShading : Update the shading. //---------------------------------------------------------------------------- void NCGShading::UpdateShading(void) const { CGColorSpaceRef cgColorSpace; // Validate our state NN_ASSERT(!mShading.IsValid()); // Create the new shading cgColorSpace = NCGColor::GetDeviceRGB(); switch (mMode) { case kShadingNone: ResetShading(); break; case kShadingLinear: mShading.SetObject(CGShadingCreateAxial( cgColorSpace, ToCG(mStartPoint), ToCG(mEndPoint), mEvaluate, mStartExtend, mEndExtend)); break; case kShadingRadial: mShading.SetObject(CGShadingCreateRadial(cgColorSpace, ToCG(mStartPoint), mStartRadius, ToCG(mEndPoint), mEndRadius, mEvaluate, mStartExtend, mEndExtend)); break; default: NN_LOG("Unknown shading mode: %d", mMode); break; } }
// ----------------------------------------------------------------------------- // ShadeRectColor // ----------------------------------------------------------------------------- // OSStatus ShadeRectColor( const CGRGB* inStartColor, const CGRGB* inEndColor, const HIRect* inRect, CGContextRef inContext ) { OSStatus err = noErr; CGColorSpaceRef colorSpace; CGFunctionCallbacks callbacks = { 0, ColorGradientEvaluate, NULL }; CGFunctionRef function; CGShadingRef shading; ColorShadeData data; // Warning: this stuff is sitting on the stack. Be careful if you move // the shading code around. data.start = *inStartColor; data.range.red = inEndColor->red - inStartColor->red; data.range.green = inEndColor->green - inStartColor->green; data.range.blue = inEndColor->blue - inStartColor->blue; CGContextSaveGState( inContext ); CGContextClipToRect( inContext, *inRect ); colorSpace = CGColorSpaceCreateDeviceRGB(); require_action( colorSpace != NULL, CantCreateColorSpace, err = memFullErr ); function = CGFunctionCreate( &data, // info 1, // domainDimension NULL, // input domain NULL == no range clipping 4, // rangeDimension, NULL, // output domain NULL == no range clipping &callbacks ); // CGFunctionCallbacks require_action( function != NULL, CantCreateFunction, err = memFullErr ); shading = CGShadingCreateAxial( colorSpace, inRect->origin, // start CGPointMake( CGRectGetMinX( *inRect ), CGRectGetMaxY( *inRect ) ), // end function, false, // extendStart false ); // extendEnd require_action( colorSpace != NULL, CantCreateShading, err = memFullErr ); CGContextDrawShading( inContext, shading); CantCreateFunction: CGShadingRelease( shading ); CantCreateShading: CGColorSpaceRelease( colorSpace ); CantCreateColorSpace: CGContextRestoreGState( inContext ); return err; }
static CGShadingRef CGShadingRefForLinearGradient(const SVGPaintServerLinearGradient* server) { CGPoint start = CGPoint(server->gradientStart()); CGPoint end = CGPoint(server->gradientEnd()); CGFunctionCallbacks callbacks = {0, cgGradientCallback, NULL}; CGFloat domainLimits[2] = {0, 1}; CGFloat rangeLimits[8] = {0, 1, 0, 1, 0, 1, 0, 1}; CGFunctionRef shadingFunction = CGFunctionCreate((void *)server, 1, domainLimits, 4, rangeLimits, &callbacks); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGShadingRef shading = CGShadingCreateAxial(colorSpace, start, end, shadingFunction, true, true); CGColorSpaceRelease(colorSpace); CGFunctionRelease(shadingFunction); return shading; }
CGShadingRef Gradient::platformGradient() { if (m_gradient) return m_gradient; const CGFloat intervalRanges[2] = { 0, 1 }; const CGFloat colorComponentRanges[4 * 2] = { 0, 1, 0, 1, 0, 1, 0, 1 }; const CGFunctionCallbacks gradientCallbacks = { 0, gradientCallback, 0 }; RetainPtr<CGFunctionRef> colorFunction(AdoptCF, CGFunctionCreate(this, 1, intervalRanges, 4, colorComponentRanges, &gradientCallbacks)); static CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); if (m_radial) m_gradient = CGShadingCreateRadial(colorSpace, m_p0, m_r0, m_p1, m_r1, colorFunction.get(), true, true); else m_gradient = CGShadingCreateAxial(colorSpace, m_p0, m_p1, colorFunction.get(), true, true); return m_gradient; }
void TIPGradientAxialFillRect( CGContextRef theContext, TIPGradientRef theGradient, CGRect theRect, float angle) { CGPoint startPoint; CGPoint endPoint; TIPGradientFindEndpointsForRotation(theRect,angle,&startPoint,&endPoint); // CoreGraphics Calls CGContextSaveGState(theContext); CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB); CGShadingRef myCGShading = CGShadingCreateAxial(colorSpace,startPoint,endPoint,theGradient->gradientFunction,FALSE,FALSE); CGContextClipToRect(theContext,theRect); CGContextDrawShading(theContext,myCGShading); CGShadingRelease(myCGShading); CGColorSpaceRelease(colorSpace); CGContextRestoreGState(theContext); }
CGShadingRef CanvasGradient::platformShading() { if (m_shading) return m_shading; const CGFloat intervalRanges[2] = { 0, 1 }; const CGFloat colorComponentRanges[4 * 2] = { 0, 1, 0, 1, 0, 1, 0, 1 }; const CGFunctionCallbacks gradientCallbacks = { 0, gradientCallback, 0 }; CGFunctionRef colorFunction = CGFunctionCreate(this, 1, intervalRanges, 4, colorComponentRanges, &gradientCallbacks); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); if (m_radial) m_shading = CGShadingCreateRadial(colorSpace, m_p0, m_r0, m_p1, m_r1, colorFunction, true, true); else m_shading = CGShadingCreateAxial(colorSpace, m_p0, m_p1, colorFunction, true, true); CGColorSpaceRelease(colorSpace); CGFunctionRelease(colorFunction); return m_shading; }