Godot: Allow for quick-creating Colors from other Colors

Created on 18 Nov 2018  路  5Comments  路  Source: godotengine/godot

(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 >_>

discussion enhancement core

Most helpful comment

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.

All 5 comments

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

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.

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

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

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.

I added this to my PR for self-constructors and set methods: https://github.com/godotengine/godot/pull/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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

testman42 picture testman42  路  3Comments

EdwardAngeles picture EdwardAngeles  路  3Comments

Zylann picture Zylann  路  3Comments

nunodonato picture nunodonato  路  3Comments

SleepProgger picture SleepProgger  路  3Comments