Units¶
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.
Cautions¶
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
Although QML is statically typed, JavaScript, which it inherits, is not. It's beneficial to remember it, especially when dealing with numbers. QML has types int
, real
, and double
while JavaScript has only number
.
So, if a unit name is a valid JS identificator, you are able to use it the following way:
however, you can't write like this because it leads to an interpreter error: “What's the difference?” you might think. It's all with the dot.JavaScript allows to omit either a leading or a trailing zero in number literals as following:
So, when you write1.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.
Available units¶
%
(or percent
)¶
- Percent – a ratio expressed as a fraction of 100.
°
(or deg
)¶
- Degree represents an angle measurement and is mainly used for hue values in HSL, HSV, or HWB color models.
int
¶
- 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]\).
byte
¶
- Transforms a real offset to integer offset.
Warning
Note that it doesn't guarantee an integer result. So in fact,
0.5.byte
returns127.5
. This is done intentionally to avoid rounding errors when dealing with color functions.