Units are functions that transform a value from one interval to another and are made for pure convenience. You are free to not use them at all since they have no impact on the main library functionality.
All units except
byte are intended to return a real offset value \(r \in [-1, 1]\). But they never check the boundaries, so the resulting value may be out of bounds for the specified interval. This is done intentionally.
Units are defined globally
All units are defined on
Number.prototype. It means that you can use them with any number literal, or variable, or property in any context, not only when dealing with colors.
Note on syntax
So, if a unit name is a valid JS identificator, you are able to use it the following way:
1.unit, the interpreter treats the dot as a part of the number literal and not as a method call.
To help interpreter understand what you would like to do, you have the following variants (
stolen inspired here):
For those units with names that are invalid identificators, the only possible way of using it is obviously the last one.
- Percent – a ratio expressed as a fraction of 100.
- Degree represents an angle measurement and is mainly used for hue values in HSL, HSV, or HWB color models.
- This unit can be used to normalize individual components of RGB(A), HSL(A), HSV(A), or HWB(A) color models (such as red, green, blue, lightness, blackness, alpha, etc.) from integer offset \(s\in [-255, 255]\).
- Transforms a real offset to integer offset.
Note that it doesn't guarantee an integer result. So in fact,
127.5. This is done intentionally to avoid rounding errors when dealing with color functions.