[futurebasic] CGColorGetComponents

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : August 2011 : Group Archive : Group : All Groups

From: Ken Shmidheiser <kshmidheiser@...>
Date: Wed, 24 Aug 2011 00:03:47 -0400
Bernie asked:

>Is there a neater way than this to get RGBA values from a CGColorRef?


Bernie

Assuming myColor is a CGColorRef:

components = fn CGColorGetComponents( myColor )
componentsCount = fn CGColorGetNumberOfComponents( myColor )
colorArray = fn CFArrayCreate( _kCFAllocatorDefault, components, 
componentsCount, #0 )

Creates a CFArray of pointers to GGFloats-- no array bounds checking 
or XRefs needed.

Here's some working code that outputs the RGBA values as a formatted 
CFString. Most of the code is used to convert the CGFloats to 
strings, but the core of the code is the three simple functions above.

Ken



include "ConsoleWindow"
include "Tlbx CoreGraphics.incl"
include "Tlbx CFNumberFormatter.incl"

local fn CreateCFStringFromRGBAComponent( componentValue as Pointer ) 
as CFStringRef
'~'1
dim as CFNumberFormatterRef formatter
dim as CFNumberRef          decimalPlaces
dim as Pointer              digits : digits = 5
dim as CFStringRef          resultStr

formatter = fn CFNumberFormatterCreate (_kCFAllocatorDefault, fn 
CFLocaleCopyCurrent, _kCFNumberFormatterDecimalStyle )
decimalPlaces = fn CFNumberCreate( _kCFAllocatorDefault, 
_kCFNumberIntType, @digits )
CFNumberFormatterSetProperty( formatter, 
kCFNumberFormatterMaxFractionDigits, decimalPlaces )
CFRelease( decimalPlaces )
resultStr = fn CFNumberFormatterCreateStringWithValue ( 
_kCFAllocatorDefault, formatter, _kCFNumberFloatType, @componentValue 
)
CFRelease( formatter )
end fn = resultStr     // User must release

dim as CGColorRef           steelBlueRef
dim as Pointer              components, valuePtr
dim as UInt32               componentsCount
dim as CFIndex              i
dim as CFArrayRef           colorArray
dim as CFNumberRef          colorValueRef
dim as CFStringRef          tempStr
dim as CFMutableStringRef   colorValuesStr

colorValuesStr = fn CFStringCreateMutable( _kCFAllocatorDefault, 0 )

steelBlueRef = fn CGColorCreateGenericRGB( 0.31, 0.427, 0.60573, 0.95 )

components = fn CGColorGetComponents( steelBlueRef )
componentsCount = fn CGColorGetNumberOfComponents( steelBlueRef )
colorArray = fn CFArrayCreate( _kCFAllocatorDefault, components, 
componentsCount, #0 )

for i = 0 to fn CFArrayGetCount( colorArray ) -1
valuePtr = fn CFArrayGetValueAtIndex( colorArray, i )
tempStr = fn CreateCFStringFromRGBAComponent( valuePtr )
if i = 0 then CFStringAppend( colorValuesStr, @"Red = " )
if i = 1 then CFStringAppend( colorValuesStr, @"Green = " )
if i = 2 then CFStringAppend( colorValuesStr, @"Blue = " )
if i = 3 then CFStringAppend( colorValuesStr, @"Alpha = " )
CFStringAppend( colorValuesStr, tempStr )
CFStringAppend( colorValuesStr, @"\r" )
CFRelease( tempStr )
next i
CFRelease( colorArray )

fn HIViewSetText( sConsoleHITextView, colorValuesStr )
CFRelease( colorValuesStr )
CFRelease( steelBlueRef )