Color
All properties and functions that expect color values in the form of instances of Color objects, also accept named colors and hex values as strings which are then converted to instances of Color
internally.
Example: Named color values:
Example: Hex color values:
Constructors
Color(red, green, blue[, alpha])
Creates a RGB Color object.
Parameters:
red:
Number
— the amount of red in the color as a value between0
and1
green:
Number
— the amount of green in the color as a value between0
and1
blue:
Number
— the amount of blue in the color as a value between0
and1
alpha:
Number
— the alpha of the color as a value between0
and1
— optionalReturns:
Color
Example:Creating a RGB Color:
Color(gray[, alpha])
Creates a gray Color object.
Parameters:
gray:
Number
— the amount of gray in the color as a value between0
and1
alpha:
Number
— the alpha of the color as a value between0
and1
— optionalReturns:
Color
Example:Creating a gray Color:
Color(object)
Creates a HSB, HSL or gradient Color object from the properties of the provided object:
Options:
hsb.hue: Number
— the hue of the color as a value in degrees between0
and360
hsb.saturation: Number
— the saturation of the color as a value between0
and1
hsb.brightness: Number
— the brightness of the color as a value between0
and1
hsb.alpha: Number
— the alpha of the color as a value between0
and1
hsl.hue: Number
— the hue of the color as a value in degrees between0
and360
hsl.saturation: Number
— the saturation of the color as a value between0
and1
hsl.lightness: Number
— the lightness of the color as a value between0
and1
hsl.alpha: Number
— the alpha of the color as a value between0
and1
gradient.gradient: Gradient
— the gradient object that describes the color stops and type of gradient to be usedgradient.origin: Point
— the origin point of the gradientgradient.destination: Point
— the destination point of the gradientgradient.stops: Array of GradientStop
objects — the gradient stops describing the gradient, as an alternative to providing a gradient objectgradient.radial: Boolean
— controls whether the gradient is radial, as an alternative to providing a gradient objectParameters:
object:
Object
— an object describing the components and properties of the colorReturns:
Color
Example:Creating a HSB Color:
Example:Creating a HSL Color:
Example:Creating a gradient color from an object literal:
Color(color)
Creates a Color object from a CSS string. All common CSS color string formats are supported: - Named colors (e.g.
'red'
,'fuchsia'
, …) - Hex strings ('#ffff00'
,'#ff0'
, …) - RGB strings ('rgb(255, 128, 0)'
,'rgba(255, 128, 0, 0.5)'
, …) - HSL strings ('hsl(180deg, 20%, 50%)'
,'hsla(3.14rad, 20%, 50%, 0.5)'
, …)Parameters:
color:
String
— the color’s CSS string representationReturns:
Color
Example:
Color(gradient, origin, destination[, highlight])
Creates a gradient Color object.
Parameters:
gradient:
Gradient
origin:
Point
destination:
Point
highlight:
Point
— optionalReturns:
Color
Example:Applying a linear gradient color containing evenly distributed color stops:
Example:Applying a radial gradient color containing unevenly distributed color stops:
Operators
+number
,+color
Returns the addition of the supplied value to both coordinates of the color as a new color. The object itself is not modified!
Parameters:
number:
Number
— the number to addReturns:
Color
— the addition of the color and the value as a new color
Example:
Returns the addition of the supplied color to the color as a new color. The object itself is not modified!
Parameters:
color:
Color
— the color to addReturns:
Color
— the addition of the two colors as a new color
Example:
-number
,-color
Returns the subtraction of the supplied value to both coordinates of the color as a new color. The object itself is not modified!
Parameters:
number:
Number
— the number to subtractReturns:
Color
— the subtraction of the color and the value as a new color
Example:
Returns the subtraction of the supplied color to the color as a new color. The object itself is not modified!
Parameters:
color:
Color
— the color to subtractReturns:
Color
— the subtraction of the two colors as a new color
Example:
*number
,*color
Returns the multiplication of the supplied value to both coordinates of the color as a new color. The object itself is not modified!
Parameters:
number:
Number
— the number to multiplyReturns:
Color
— the multiplication of the color and the value as a new color
Example:
Returns the multiplication of the supplied color to the color as a new color. The object itself is not modified!
Parameters:
color:
Color
— the color to multiplyReturns:
Color
— the multiplication of the two colors as a new color
Example:
/number
,/color
Returns the division of the supplied value to both coordinates of the color as a new color. The object itself is not modified!
Parameters:
number:
Number
— the number to divideReturns:
Color
— the division of the color and the value as a new color
Example:
Returns the division of the supplied color to the color as a new color. The object itself is not modified!
Parameters:
color:
Color
— the color to divideReturns:
Color
— the division of the two colors as a new color
Example:
Properties
type
The type of the color as a string.
Type:
String
Example:
components
The color components that define the color, including the alpha value if defined.
Read only.
Type:
Array of
Numbers
alpha
The color’s alpha value as a number between
0
and1
. All colors of the different subclasses support alpha values.Default:
1
Type:
Number
Example: A filled path with a half transparent stroke:
RGB Components
red
The amount of red in the color as a value between
0
and1
.Type:
Number
Example: Changing the amount of red in a color:
green
The amount of green in the color as a value between
0
and1
.Type:
Number
Example: Changing the amount of green in a color:
blue
The amount of blue in the color as a value between
0
and1
.Type:
Number
Example: Changing the amount of blue in a color:
Gray Components
gray
The amount of gray in the color as a value between
0
and1
.Type:
Number
HSB Components
hue
The hue of the color as a value in degrees between
0
and360
.Type:
Number
Example: Changing the hue of a color:
Example: Hue cycling:
saturation
The saturation of the color as a value between
0
and1
.Type:
Number
brightness
The brightness of the color as a value between
0
and1
.Type:
Number
HSL Components
lightness
The lightness of the color as a value between
0
and1
.Note that all other components are shared with HSB.
Type:
Number
Gradient Components
gradient
The gradient object describing the type of gradient and the stops.
Type:
Gradient
highlight
The highlight point of the gradient.
Type:
Point
Example: Create a circle shaped path at the center of the view, using 40% of the height of the view as its radius and fill it with a radial gradient color:
Methods
set(...values)
Sets the color to the passed values. Note that any sequence of parameters that is supported by the various
Color
() constructors also work for calls ofset()
.Parameters:
values:
any value
Returns:
Color
convert(type)
Converts the color to another type.
Parameters:
type:
String
— the color type to convert to. Possible values:‘rgb’
,‘gray’
,‘hsb’
,‘hsl’
Returns:
Color
— the converted color as a new instance
hasAlpha()
Checks if the color has an alpha value.
Returns:
Boolean
—true
if the color has an alpha value,false
otherwise
equals(color)
Checks if the component color values of the color are the same as those of the supplied one.
Parameters:
color:
Color
— the color to compare withReturns:
Boolean
—true
if the colors are the same,false
otherwise
clone()
Returns:
Color
— a copy of the color object
String Representations
toString()
Returns:
String
— a string representation of the color
toCSS(hex)
Returns the color as a CSS string.
Parameters:
hex:
Boolean
— whether to return the color in hexadecimal representation or as a CSS RGB / RGBA string.Returns:
String
— a CSS string representation of the color
transform(matrix)
Transform the gradient color by the specified matrix.
Parameters:
matrix:
Matrix
— the matrix to transform the gradient color by
Math Operator Functions
add(number)
Returns the addition of the supplied value to both coordinates of the color as a new color. The object itself is not modified!
Parameters:
number:
Number
— the number to addReturns:
Color
— the addition of the color and the value as a new color
Example:
add(color)
Returns the addition of the supplied color to the color as a new color. The object itself is not modified!
Parameters:
color:
Color
— the color to addReturns:
Color
— the addition of the two colors as a new color
Example:
subtract(number)
Returns the subtraction of the supplied value to both coordinates of the color as a new color. The object itself is not modified!
Parameters:
number:
Number
— the number to subtractReturns:
Color
— the subtraction of the color and the value as a new color
Example:
subtract(color)
Returns the subtraction of the supplied color to the color as a new color. The object itself is not modified!
Parameters:
color:
Color
— the color to subtractReturns:
Color
— the subtraction of the two colors as a new color
Example:
multiply(number)
Returns the multiplication of the supplied value to both coordinates of the color as a new color. The object itself is not modified!
Parameters:
number:
Number
— the number to multiplyReturns:
Color
— the multiplication of the color and the value as a new color
Example:
multiply(color)
Returns the multiplication of the supplied color to the color as a new color. The object itself is not modified!
Parameters:
color:
Color
— the color to multiplyReturns:
Color
— the multiplication of the two colors as a new color
Example:
divide(number)
Returns the division of the supplied value to both coordinates of the color as a new color. The object itself is not modified!
Parameters:
number:
Number
— the number to divideReturns:
Color
— the division of the color and the value as a new color
Example:
divide(color)
Returns the division of the supplied color to the color as a new color. The object itself is not modified!
Parameters:
color:
Color
— the color to divideReturns:
Color
— the division of the two colors as a new color
Example:
Static Methods
Color.random()
Returns a color object with random
red
,green
andblue
values between0
and1
.Returns:
Color
— the newly created color object
Example: