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
Creates a RGB Color object.
-
red:
Number
— the amount of red in the color as a value between
0
and1
-
green:
Number
— the amount of green in the color as a value between
0
and1
-
blue:
Number
— the amount of blue in the color as a value between
0
and1
-
alpha:
Number
— the alpha of the color as a value between
0
and1
— optional
Parameters:
- Color
Returns:
Example:Creating a RGB Color:
Creates a gray Color object.
-
gray:
Number
— the amount of gray in the color as a value between
0
and1
-
alpha:
Number
— the alpha of the color as a value between
0
and1
— optional
Parameters:
- Color
Returns:
Example:Creating a gray Color:
Creates a HSB, HSL or gradient Color object from the properties of the provided object:
- hsb.hue: Number — the hue of the color as a value in degrees between
0
and360
- hsb.saturation: Number — the saturation of the color as a value between
0
and1
- hsb.brightness: Number — the brightness of the color as a value between
0
and1
- hsb.alpha: Number — the alpha of the color as a value between
0
and1
- hsl.hue: Number — the hue of the color as a value in degrees between
0
and360
- hsl.saturation: Number — the saturation of the color as a value between
0
and1
- hsl.lightness: Number — the lightness of the color as a value between
0
and1
- hsl.alpha: Number — the alpha of the color as a value between
0
and1
- gradient.gradient: Gradient — the gradient object that describes the color stops and type of gradient to be used
- gradient.origin: Point — the origin point of the gradient
- gradient.destination: Point — the destination point of the gradient
- gradient.stops: Array of GradientStop objects — the gradient stops describing the gradient, as an alternative to providing a gradient object
- gradient.radial: Boolean — controls whether the gradient is radial, as an alternative to providing a gradient object
Options:
- object: Object — an object describing the components and properties of the color
Parameters:
- Color
Returns:
Example:Creating a HSB Color:
Example:Creating a HSL Color:
Example:Creating a gradient color from an object literal:
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)'
, …)
- color: String — the color’s CSS string representation
Parameters:
- Color
Returns:
Example:
Creates a gradient Color object.
- Color
Returns:
Example:Applying a linear gradient color containing evenly distributed color stops:
Example:Applying a radial gradient color containing unevenly distributed color stops:
Operators
Returns the addition of the supplied value to both coordinates of the color as a new color. The object itself is not modified!
- number: Number — the number to add
Parameters:
- Color — the addition of the color and the value as a new color
Returns:
Example:
var color = new Color(0.5, 1, 1);
var result = color + 1;
console.log(result); // { red: 1, blue: 1, green: 1 }
Returns the addition of the supplied color to the color as a new color. The object itself is not modified!
- color: Color — the color to add
Parameters:
- Color — the addition of the two colors as a new color
Returns:
Example:
var color1 = new Color(0, 1, 1);
var color2 = new Color(1, 0, 0);
var result = color1 + color2;
console.log(result); // { red: 1, blue: 1, green: 1 }
Returns the subtraction of the supplied value to both coordinates of the color as a new color. The object itself is not modified!
- number: Number — the number to subtract
Parameters:
- Color — the subtraction of the color and the value as a new color
Returns:
Example:
var color = new Color(0.5, 1, 1);
var result = color - 1;
console.log(result); // { red: 0, blue: 0, green: 0 }
Returns the subtraction of the supplied color to the color as a new color. The object itself is not modified!
- color: Color — the color to subtract
Parameters:
- Color — the subtraction of the two colors as a new color
Returns:
Example:
var color1 = new Color(0, 1, 1);
var color2 = new Color(1, 0, 0);
var result = color1 - color2;
console.log(result); // { red: 0, blue: 1, green: 1 }
Returns the multiplication of the supplied value to both coordinates of the color as a new color. The object itself is not modified!
- number: Number — the number to multiply
Parameters:
- Color — the multiplication of the color and the value as a new color
Returns:
Example:
var color = new Color(0.5, 1, 1);
var result = color * 0.5;
console.log(result); // { red: 0.25, blue: 0.5, green: 0.5 }
Returns the multiplication of the supplied color to the color as a new color. The object itself is not modified!
- color: Color — the color to multiply
Parameters:
- Color — the multiplication of the two colors as a new color
Returns:
Example:
var color1 = new Color(0, 1, 1);
var color2 = new Color(0.5, 0, 0.5);
var result = color1 * color2;
console.log(result); // { red: 0, blue: 0, green: 0.5 }
Returns the division of the supplied value to both coordinates of the color as a new color. The object itself is not modified!
- number: Number — the number to divide
Parameters:
- Color — the division of the color and the value as a new color
Returns:
Example:
var color = new Color(0.5, 1, 1);
var result = color / 2;
console.log(result); // { red: 0.25, blue: 0.5, green: 0.5 }
Returns the division of the supplied color to the color as a new color. The object itself is not modified!
- color: Color — the color to divide
Parameters:
- Color — the division of the two colors as a new color
Returns:
Example:
var color1 = new Color(0, 1, 1);
var color2 = new Color(0.5, 0, 0.5);
var result = color1 / color2;
console.log(result); // { red: 0, blue: 0, green: 1 }
Properties
The type of the color as a string.
- String
Type:
Example:
var color = new Color(1, 0, 0);
console.log(color.type); // 'rgb'
The color components that define the color, including the alpha value if defined.
Read only.
- Array of Numbers
Type:
The color’s alpha value as a number between 0
and 1
. All colors of the different subclasses support alpha values.
- 1
Default:
- Number
Type:
Example:A filled path with a half transparent stroke:
RGB Components
The amount of red in the color as a value between 0
and 1
.
- Number
Type:
Example:Changing the amount of red in a color:
The amount of green in the color as a value between 0
and 1
.
- Number
Type:
Example:Changing the amount of green in a color:
The amount of blue in the color as a value between 0
and 1
.
- Number
Type:
Example:Changing the amount of blue in a color:
Gray Components
The amount of gray in the color as a value between 0
and 1
.
- Number
Type:
HSB Components
The hue of the color as a value in degrees between 0
and 360
.
- Number
Type:
Example:Changing the hue of a color:
Example:Hue cycling:
The saturation of the color as a value between 0
and 1
.
- Number
Type:
The brightness of the color as a value between 0
and 1
.
- Number
Type:
HSL Components
The lightness of the color as a value between 0
and 1
.
Note that all other components are shared with HSB.
- Number
Type:
Gradient Components
Methods
Converts the color to another type.
- type: String — the color type to convert to. Possible values: ‘rgb’, ‘gray’, ‘hsb’, ‘hsl’
Parameters:
- Color — the converted color as a new instance
Returns:
Checks if the color has an alpha value.
- Boolean — true if the color has an alpha value, false otherwise
Returns:
Checks if the component color values of the color are the same as those of the supplied one.
- color: Color — the color to compare with
Parameters:
- Boolean — true if the colors are the same, false otherwise
Returns:
String Representations
- String — a string representation of the color
Returns:
Returns the color as a CSS string.
- hex: Boolean — whether to return the color in hexadecimal representation or as a CSS RGB / RGBA string.
Parameters:
- String — a CSS string representation of the color
Returns:
Transform the gradient color by the specified matrix.
- matrix: Matrix — the matrix to transform the gradient color by
Parameters:
Math Operator Functions
Returns the addition of the supplied value to both coordinates of the color as a new color. The object itself is not modified!
- number: Number — the number to add
Parameters:
- Color — the addition of the color and the value as a new color
Returns:
Example:
var color = new Color(0.5, 1, 1);
var result = color + 1;
console.log(result); // { red: 1, blue: 1, green: 1 }
Returns the addition of the supplied color to the color as a new color. The object itself is not modified!
- color: Color — the color to add
Parameters:
- Color — the addition of the two colors as a new color
Returns:
Example:
var color1 = new Color(0, 1, 1);
var color2 = new Color(1, 0, 0);
var result = color1 + color2;
console.log(result); // { red: 1, blue: 1, green: 1 }
Returns the subtraction of the supplied value to both coordinates of the color as a new color. The object itself is not modified!
- number: Number — the number to subtract
Parameters:
- Color — the subtraction of the color and the value as a new color
Returns:
Example:
var color = new Color(0.5, 1, 1);
var result = color - 1;
console.log(result); // { red: 0, blue: 0, green: 0 }
Returns the subtraction of the supplied color to the color as a new color. The object itself is not modified!
- color: Color — the color to subtract
Parameters:
- Color — the subtraction of the two colors as a new color
Returns:
Example:
var color1 = new Color(0, 1, 1);
var color2 = new Color(1, 0, 0);
var result = color1 - color2;
console.log(result); // { red: 0, blue: 1, green: 1 }
Returns the multiplication of the supplied value to both coordinates of the color as a new color. The object itself is not modified!
- number: Number — the number to multiply
Parameters:
- Color — the multiplication of the color and the value as a new color
Returns:
Example:
var color = new Color(0.5, 1, 1);
var result = color * 0.5;
console.log(result); // { red: 0.25, blue: 0.5, green: 0.5 }
Returns the multiplication of the supplied color to the color as a new color. The object itself is not modified!
- color: Color — the color to multiply
Parameters:
- Color — the multiplication of the two colors as a new color
Returns:
Example:
var color1 = new Color(0, 1, 1);
var color2 = new Color(0.5, 0, 0.5);
var result = color1 * color2;
console.log(result); // { red: 0, blue: 0, green: 0.5 }
Returns the division of the supplied value to both coordinates of the color as a new color. The object itself is not modified!
- number: Number — the number to divide
Parameters:
- Color — the division of the color and the value as a new color
Returns:
Example:
var color = new Color(0.5, 1, 1);
var result = color / 2;
console.log(result); // { red: 0.25, blue: 0.5, green: 0.5 }
Returns the division of the supplied color to the color as a new color. The object itself is not modified!
- color: Color — the color to divide
Parameters:
- Color — the division of the two colors as a new color
Returns:
Example:
var color1 = new Color(0, 1, 1);
var color2 = new Color(0.5, 0, 0.5);
var result = color1 / color2;
console.log(result); // { red: 0, blue: 0, green: 1 }