Change CALayer background color; setting a CGColor in ASOC?

I’m trying to set the background color of a CALayer to clear. It’s currently showing opaque gray.

I’m not sure exactly where the color is coming from. The CALayer is used for animating view transitions.

If I turn off the view’s “setWantsLayer” the view is clear. From what I’ve read, all I need to do is set the layer’s background color to a cg color reference. I’ve tried various combinations, but I can’t seem to find the right syntax or method to set the color, or access any CG methods. I’d like to use the constant color ref, but I’m fine with setting the rgb values manually, just can’t get that to work either.

It’s very possible that I may be running down the wrong problem :). So, I’m open to any other suggestions as well.

Any help would be greatly appreciated.

set myColor to current application's CGColor's CGColorGetConstantColor("kCGColorClear")
myWindow's contentView's layer's setBackgroundColor_(myColor)


Firstly the CALayer class is part of the QuartzCore Framework which will have to be imported into you’re project, as the standard ASOC project template only uses the standard Cocoa frameworks like Foundation and AppKit and ApplescriptObjC ect.
Secondly I beleive the QuartzCore framework is written in the C++ language, so it may not be accessible with the ASOC language, which I also believe can only access framework classes written in Objective-C, but I could be wrong with that opinion.

also your code example has missing braces for the method calls, and is also trying to call a setBackgroundColor() method on a CALayer which does not have a setBackgroundColor() method, only a backgroundColor property.

you’ve written this.

myWindow's contentView's layer's setBackgroundColor_(myColor)

To access the Window’s layer would be more like this.

set theLayer to myWindow's contentView()'s layer()

On the few occasions I’ve worked with the CALayer class in ObjC projects, I’ve accessed and modified it’s properties with the dot syntax like the example below, which is why I suspect it’s class is written with the C++ language.
Also most of the examples I’ve read in books and online also seem to all use this dot syntax.

CALayer *myLayer = [CALayer layer];
myLayer.anchorPoint = CGPointZero;
theLayer.position = CGPointMake(10, 10);
myLayer.backgroundColor = CGColorGetConstantColor(kCGColorClear);

This code is not valid code in an ASOC project, so dont try to use it in your own project, but I’ve posted it so that the more experienced ASOC coders that hang out here, may be able to show how to translate this type of syntax to ASOC code.

Lastly I think you can also use the NS classes for your CGRef’s as they are toll free bridged by the Scripting Bridge, for example the NSColor has a CGColor method, and the NSMakePoint() method is bridged to the CGPointMake() method by the scripting bridge, but I’ve not tested it in an ASOC project, maybe again one of the other members could confirm this.

set myColor to current application's NSColor's clearColor()
myLayer.backgroundColor = (myColor's CGColor())

Sorry I could not be help more, but maybe some of my points may help in some way, If I get a chance this week I’ll create a new ASOC project and play around myself with a CALayer class, and let you know if I have any joy.

Regards Mark


Not so. It doesn’t matter what it’s written in, but the API. CALayer has an Objective-C API, so it’s accessible to ASObjC. ASObjC can also access C-based APIs, although in practice this often fails because it can’t support the argument types used. That may well be the case here.

Right. Sometimes this works, because it’s effectively a key-value call, but other times it fails.

It’s fine to call setBackgroundColor_. A writable property’s accessor is formed by preceding its name with “set” and capping the first character.

Dot-syntax is just syntactic sugar, designed to differentiate properties from other methods in source code. myLayer.anchorPoint and [myLayer anchorPoint] generate identical code.

I haven’t played with this stuff, but I suspect the problem might be that ASObjC has no way of dealing with a CGColorRef. If that’s the case, I don’t see any non-Objective-C solution.

Well I stand corrected by Shane on all counts.

But I do beleive that the NSColor’s CGColor() method does return a CGColorRef, although if it’s useable by ASOC I hav’nt tried using it yet.

I’ve also played around for an hour with the CALayer class in an ASOC project, and I also had no luck with accessing the CALayer’s properties, and getting unrecognized selector error messages when using “set” before the capitalized property names.

Regards Mark

Thanks for the help gents!

Making the window opaque hides the issue, but I lose the view transparency.

No big loss, but I’d like to sort it out one of these days.

I think I’ll file the CG issue as “figure it out later”.

I did notice another issue with a search field that I’ll post in another thread.

Thanks again!!

It does return one, but you can’t can’t use it in ASObjC. Here’s a snippet of log from ASObjC Explorer:

0000.108 [14] set aColor to current application's NSColor's redColor()'s CGColor()
--> <BAImmediateData: 0x60000000aff0>
0000.135 [15] aLayer's setBackgroundColor:aColor
0000.135 *** -[CALayer setBackgroundColor:]: value passed to argument of type ^{CGColor=} must be either `missing value' or `reference'; assuming `missing value'.

It does work – for example:

0000.049 [11] set aLayer to current application's CALayer's layer()
--> <CALayer: 0x600002a394a0>
0000.068 [12] aLayer's setOpacity:0.0
0000.087 [13] aLayer's opacity()
--> 0.0

Hi There

I am sure you have figured it out by now, but just in case, I had a similar problem with CGColor and ASOC.

I added a cocoa class (.m and .h files) that would deal with all the functions that could not be accessed directly via ASOC.
For example , CMTime , CGColor etc. Below is a snippet of what I used to get/create CGColor
( It is not what you need, but it is a similar process. My ASOC code sent a CG Text layer to cocoa and the code made the background of the text layer black with a 75% opacity)

-(void)makeBlack:(CALayer *)sender
    CALayer *myTextLayer = sender;
  //  CGColorRef color;
    CGFloat fillComponents[4] = {0, 0, 0, 0.75};
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    myTextLayer.backgroundColor = CGColorCreate(colorSpace, fillComponents);

I am sure that you could use a similar function to access CGColor