Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for quick-creating Colors from other Colors #23786

Closed
KoBeWi opened this issue Nov 17, 2018 · 5 comments · Fixed by #36379
Closed

Allow for quick-creating Colors from other Colors #23786

KoBeWi opened this issue Nov 17, 2018 · 5 comments · Fixed by #36379

Comments

@KoBeWi
Copy link
Member

KoBeWi commented Nov 17, 2018

(sorry if the title isn't clear, but I wasn't sure how to name it)

The issue is:
Godot 3.1 (or 3.0?) introduced color constants. You can now use Color.red for red etc. However, I sometimes need a translucent red. So I still need to use Color(1, 0, 0, 0.5). This is minor inconvenience of course, but since the Color.red was introduced to avoid Color(1, 0, 0) then why not go further?

There are two "fixes" I can think of:
Color.red.with_alpha(0.5) - it's actually longer, but I'd prefer that for whatever reason. Of course shorter version is possible (and preferred?), like Color.red.w_a(0). (or Color.red.translucent(0.5))

Or Color.red.a = 0.5 could magically return red with 0.5 alpha`

This might of course apply to other Color properties to quickly create colors from other colors, but with minor changes.

Well, this >_>

@akien-mga
Copy link
Member

You can just do

var col = Color.red
col.a = 0.5

or

const HALF_ALPHA = Color(1, 1, 1, 0.5)

var col = Color.red * HALF_ALPHA

@KoBeWi
Copy link
Member Author

KoBeWi commented Nov 18, 2018

Um, the first one doesn't fix anything. I want it to be a one-liner, because I often need this as a method argument. Doing it in two lines is counter-productive.

As for the second one, I didn't know we can multiply colors like that. Looks like it could be enough, but still I need to define the constant each time and it's only for one alpha value.

@Zylann
Copy link
Contributor

Zylann commented Nov 18, 2018

@KoBeWi you don't have to define the constant:

var col = Color.red * Color(1,1,1, 0.5)

@ghost
Copy link

ghost commented Nov 19, 2018

What about ColorN? The first argument is the same constant name in String.

var col = ColorN("red", alpha)

Although a Color constructor that clones a Color and assign new alpha might be handy too.

@aaronfranke
Copy link
Member

aaronfranke commented Nov 27, 2018

I added this to my PR for self-constructors and set methods: #21348 EDIT: See #36379

Now you can do this in GDScript:

var myRed = Color.red
var transparentRed = Color(myRed, 0.5)
var copyOfRed = Color(myRed)

Or, if you want a one-liner:

var transparentRed = Color(Color.red, 0.5)

But @Naryosha's suggestion of ColorN would be better for this specific case of grabbing a preset color.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants