HTML and CSS Reference
In-Depth Information
Of course, using a string for the color name is not the only available method of specifying a
solid color fill. The following list includes a few other methods:
Setting the fill color with the
rgb()
rgb()
method
The
rgb()
method lets us use the 24-bit RGB value when specifying our fill colors:
context
.
fillStyle
=
"rgb(255,0,0)"
;
This will result in the same
red
color as the string value above.
Setting the fill color with a hex number string
We can also set the
fillStyle
color with a hex number in a string:
context
.
fillStyle
=
"#ff0000"
;
Setting the fill color with the
rgba()
rgba()
method
The
rgba()
method allows us to specify a 32-bit color value with the final 8 bits repres-
enting the alpha value of the fill color:
context
.
fillStyle
=
"rgba(255,0,0,1)"
;
The alpha value can be from
1
(opaque) to
0
(transparent).