Change opacity for pre-defined color?
Asked Answered
V

2

13

What would be the best (as in: most concise) way for setting just the alpha component of a predefined color?

Say I'd like to use something like + (NSColor *)brownColor but with 50% opacity -
I couldn't find an appropriate constructor like ..fromNSColor: that would allow us to change the alpha component and with NSColor being immutable is there any other convenience method we can use to alter the alpha component whilst keeping r/g/b components the same?

Virg answered 6/2, 2014 at 19:42 Comment(0)
L
24

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.

Lurie answered 6/2, 2014 at 19:44 Comment(2)
Man, I must go blind :-)Virg
@ATV: Apple's documentation isn't exactly the easiest to search.Lurie
A
4

There is indeed a method for NSColor doing that:

[[NSColor brownColor] colorWithAlphaComponent:0.1];

That would give you brown with 10% opacity.

Antagonist answered 6/2, 2014 at 19:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.