Change opacity for a given color?

What would be the best (as in the case: the most concise) way to set only the alpha component of the color ?

Say I would like to use something like + (NSColor *)brownColor , but with 50% opacity -
I could not find a suitable constructor, such as ..fromNSColor: which would allow us to change the alpha component and NSColor be unchanged, is there any other convenience method that we can use to change the alpha component while preserving the r / components g / b same?

+8
objective-c cocoa nscolor
source share
2 answers

Use this method :

 [[NSColor brownColor] colorWithAlphaComponent:0.5]; 

It returns a new color with the same RGB as the receiver, but with a new alpha.

+16
source share

Indeed, for NSColor there is such a way:

 [[NSColor brownColor] colorWithAlphaComponent:0.1]; 

This will give you a brown color with an opacity of 10%.

+4
source share

All Articles