Tagged literals¶
This feature is based on tagged templates from ES2016.
It allow constructing color objects using one of the available tags listed below in a quick and easy manner.
Skipped notation part?
You can always find it here.
Instead of introduction¶
If you are too lazy to read the documentation on tagged template literals from ES2016, I'm here to help you with this short introduction.
About tagged literals
Some general information on how to pass parameters to tagged literals is available here.
For all tags except qolor
and color
the following rules apply:
- String parts are ignored. The following literals are equal:
- Previous rule means the literals can be multiline. These are equal:
-
Every parameter is an expression, so it can contain calculations or any other code. These are equal:
-
If a parameter has some default value, it can be omitted. The following literals are equal:
List of available tags¶
The first two tags—qolor
and color
—are trying to cover 80% of needs of an average user of the library by using a multi-step heuristics approach.
If you need to construct a color using a precise form, please, see argb
, argb32
, hsla
, hsva
, hwba
, rgba
, or rgba32
below.
qolor
¶
-
Alias: q
(various types) →
qolor
- (
name
:color-name
) →qolor
- (
#aarrggbb
|#rrggbb
|#argb
|#rgb
:color-literal
) →qolor
- (
a = 255
,r
,g
,b
:8bit
) →qolor
This tag tries to construct a Qt color based on the following magic (order matters):
- Treat the resulting string as color name.
- Treat the resulting string as color literal.
- Treat the template parameters as ARGB32.
- Treat the template parameters as RGB24
- If nothing works, return
undefined
.
Examples:
Color names¶
Color literals¶
(A)RGB32¶
Beware the hash
#
!If your literal starts with
#
and contains no spaces, it will be treated first as a regular color literal giving you most probably an unexpected result.Let's consider an example:
It contructs us exactly what we wanted:#2000ff
. How? It first tries to match it as a color name, doesn't succeed, then tries to match as a regular color literal, again no success, and then it matches it as RGB24.Now, let's look at and analyze the following:
We see that it's nearly the same except the hash sign#
in the very beginning. And again, it tries to match agains color names first with no success, obviously.However, then it tries to match it as a regular color literal. To do so, it needs to combine the parameters into a string:
- First, let's convert hexadecimal numbers into decimal
- 2016 = 3210
- 016 = 010
- FF16 = 25510
- Then we combine a string as if it were a regular (not tagged) template as following:
`#${32}${0}${255}`
- The resulting string starts with
#
and contains exactly 6 digits, so it is treated as a color literal. - The
#320255
is returned.
- (
color
¶
-
Alias: cc
(various types) →
color
- (
name
:color-name
) →color
- (
#aarrggbb
|#rrggbb
|#argb
|#rgb
:color-literal
) →color
- (
a = 255
,r
,g
,b
:8bit
) →color
The only difference to
qolor
tag is that this one creates an instance of theColor
class instead of a regular Qt color. This class contains lots of useful methods and is far more superior in general. However, everthing comes with a price.Color names¶
Color literals¶
(A)RGB32¶
- (
argb
¶
-
Type: ( alpha
,red
,green
,blue
:norm
) →qolor
Creates a color from ARGB parameters.
If this tag can't construct a color from literal's parameters, an exception is raised.
Examples:
argb32
¶
-
Type: ( alpha
,red
,green
,blue
:8bit
) →qolor
Creates a color from ARGB parameters.
If this tag can't construct a color from literal's parameters, an exception is raised.
Examples:
hsla
¶
-
Type: ( hue
,saturation
,lightness
,alpha = 1.0
:norm
) →qolor
Alias: hsl
Creates a color from HSLA parameters.
If this tag can't construct a color from literal's parameters, an exception is raised.
Examples:
(no alpha):
hsva
¶
-
Type: ( hue
,saturation
,value
,alpha = 1.0
:norm
) →qolor
Alias: hsv
Creates a color from HSVA parameters.
If this tag can't construct a color from literal's parameters, an exception is raised.
Examples:
(no alpha):
hwba
¶
-
Type: ( hue
,whiteness
,blackness
,alpha = 1.0
:norm
) →qolor
Alias: hwb
Creates a color from HWBA parameters.
If this tag can't construct a color from literal's parameters, an exception is raised.
Examples:
(no alpha):
rgba
¶
-
Type: ( red
,green
,blue
,alpha = 1.0
:norm
) →qolor
Alias: rgb
Creates a color from RGBA parameters.
If this tag can't construct a color from literal's parameters, an exception is raised.
Examples:
(no alpha):