Example #1
0
OSStatus
TextViewSetBGAlpha( HIViewRef textView, float alpha )
{
	OSStatus status = paramErr;
	
	SInt32 unused;
	CGColorRef prevColor = NULL;
	CGColorRef newColor = NULL;
	
	status = HITextViewCopyBackgroundColor( textView, &prevColor );
  
	require_noerr( status, FAIL );
	
	// make a new copy with alpha
	newColor = CGColorCreateCopyWithAlpha( prevColor, alpha );
	
	require_action( newColor != NULL, FAIL, status = paramErr );

	// reset the new color with alpha
	status = HITextViewSetBackgroundColor( textView, newColor );

	if( newColor != NULL )
		CGColorRelease( newColor );
		
	FAIL:
	return status;
}
void drawWithColorRefs(CGContextRef context)
{
	static CGColorRef opaqueRedColor = NULL, opaqueBlueColor = NULL, transparentBlueColor = NULL;

	// Initialize the CGColorRefs if necessary
	if(opaqueRedColor == NULL){
		// Initialize the color array to an opaque red 
		// in the generic calibrated RGB color space.
		float color[4] = { 0.663, 0.0, 0.031, 1.0 };
		CGColorSpaceRef theColorSpace = getTheCalibratedRGBColorSpace();
		// Create a CGColorRef for opaque red.
		opaqueRedColor = CGColorCreate(theColorSpace, color);
		// Make the color array correspond to an opaque blue color.
		color[0] = 0.482; color[1] = 0.62; color[2] = 0.871;
		// Create another CGColorRef for opaque blue.
		opaqueBlueColor = CGColorCreate(theColorSpace, color);
		// Create a new CGColorRef from the opaqueBlue CGColorRef 
		// but with a different alpha value.
		transparentBlueColor = CGColorCreateCopyWithAlpha(opaqueBlueColor, 0.5);
		if(!(opaqueRedColor && opaqueBlueColor && transparentBlueColor)){
		    fprintf(stderr, "Couldn't create one of the CGColorRefs!!!\n");
		    return;
		}
	}

	// Set the fill color to the opaque red CGColor object.
	CGContextSetFillColorWithColor(context, opaqueRedColor);
	// Set the stroke color to the opaque blue CGColor object.
	CGContextSetStrokeColorWithColor(context, opaqueBlueColor);
	
	CGContextSetLineWidth(context, 8.);
	// Draw the first rectangle.
	CGContextBeginPath(context);
	CGContextAddRect(context, CGRectMake(20., 20., 100., 100.));
	CGContextDrawPath(context, kCGPathFillStroke);
    
	// Set the stroke color to be that of the transparent blue 
	// CGColor object.
	CGContextSetStrokeColorWithColor(context, transparentBlueColor);	
	// Draw a second rectangle to the right of the first one.
	CGContextBeginPath(context);
 	CGContextAddRect(context, CGRectMake(140., 20., 100., 100.));
	CGContextDrawPath(context, kCGPathFillStroke);
}
//---------------------------------------------------------------------
// Sets the alpha of an HITextView to the specified value
//
OSStatus TextViewSetAlpha(HIViewRef textView, float alpha)
{
	CGColorRef prevColor, newColor;
	OSStatus status;
	
	status = HITextViewCopyBackgroundColor(textView, &prevColor);
	require_noerr( status, CantGetBackgroundColor );
	
	newColor = CGColorCreateCopyWithAlpha(prevColor, alpha);
	require( (newColor != NULL), CantCreateNewColor );
	
	status = HITextViewSetBackgroundColor(textView, newColor);
	check_noerr( status );

	CGColorRelease(newColor);
CantCreateNewColor:
	CGColorRelease(prevColor);
CantGetBackgroundColor:
	return status;
}