garrysmod icon indicating copy to clipboard operation
garrysmod copied to clipboard

Allow numerical indexing of colors

Open Kefta opened this issue 6 years ago • 3 comments

  • Allows color[i] for indexing elements
  • Prevents any keys other than r, g, b, and a from being modified on the color object
  • Prevents setting color components to non-numerical types if the key was set to nil prior. This is the best that can be done with type safety in Lua without aliasing the Color object since __newindex isn't called for keys that already exist in the table, and the object can't be aliased without breaking scripts calling rawget/rawset on colors.
  • Don't allow negative color components, clamp between [0, 255]
  • Make 255 the default for every argument of Color

Relies on https://github.com/Facepunch/garrysmod/pull/1587.

Kefta avatar Sep 27 '19 17:09 Kefta

Doesn't this make the regular indexing slower, what's do developers gain from being able to use numerical indexing?

SimonSchick avatar Sep 28 '19 21:09 SimonSchick

To be consistent with Vectors and Angles. Indexing color objects is already pretty uncommon from the lack of member functions, but considering https://github.com/Facepunch/garrysmod-issues/issues/2407 has existed for the longest time (prevents indexing engine colors), I don't think this will have any major negative performance consequences.

Really this is just a temporary solution until Colors are moved to C++.

Kefta avatar Sep 28 '19 22:09 Kefta

I should also mention this does not affect the speed of regular key indexing (color.r, color.g, color.b, color.a) since __index is only called if the key doesn't exist in the table.

Kefta avatar Oct 14 '19 22:10 Kefta