garrysmod
garrysmod copied to clipboard
Allow numerical indexing of colors
- 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/rawseton 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.
Doesn't this make the regular indexing slower, what's do developers gain from being able to use numerical indexing?
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++.
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.