ColorMath: Math capabilities for Color3

ABOUT:

An easy-to-use math extension of the Color3 library.
Color3 userdata doesn’t support math, and stores its values as 0-1 sRGB floats.
The sRGB color space splits dark and bright values between a linear scale [straight] and a gamma curve.
sRGB originated in 1996, allocating human-noticeable color differences to an 8-bit integer.
Due to this, sRGB needs converting to Linear RGB [AKA lRGB] so that we can do math.

API:

API is minimal, accepting and returning 0-1 sRGB Color3.
Color3’s are converted from sRGB, calculated as lRGB, and converted back to sRGB.
This can be costly in heavy loops, but this is designed for ease-of-use, not speed.
Long-term compatibility is ensured by preventing below API’s being overwritten by Roblox.
Colorspace conversions are included for more granular operation [eg. Color3:Lerp()]

Math [sRGB input only]:

Color3.add( Color3, ...Color3 ): Color3
Color3.sub( Color3, ...Color3 ): Color3
Color3.mul( Color3, ...Color3 ): Color3
Color3.div( Color3, ...Color3 ): Color3
Color3.idiv( Color3, ...Color3 ): Color3

Color Space:

Color3.sRGB_To_lRGB( ...Color3 ): ...Color3
Color3.lRGB_To_sRGB( ...Color3 ): ...Color3

SETUP:

As this module extends the Color3 library, it’s recommended to implement as such:
Public Module: local Color3 = require(93416946935477)
ModuleScript: local Color3 = require(Path.To.ColorMath)
Module Source: https://create.roblox.com/store/asset/93416946935477/ColorMath

This is my first public resource, feedback aimed toward improving the module would be much appreciated! Thanks.

20 Likes

Oh snap, instantly grabbed! Thank you Crazyblox!

1 Like