Overview
The Color class provides parsing, conversion, and comparison of CSS color values across multiple formats including RGB, RGBA, hex, HSL, HSLA, percentage-based RGB/RGBA, and named colors.
Description
Color is a utility class that represents an RGBA color value. It includes a powerful fromString() factory method that auto-detects and parses color strings in nine different CSS formats: rgb(), rgba(), percentage-based rgb() and rgba(), 6-digit hex (#rrggbb), 3-digit hex (#rgb), hsl(), hsla(), and named CSS colors (via the Colors enum). The class provides output methods to serialize colors as RGB, RGBA, and hex strings, and can convert to java.awt.Color. It implements equals() and hashCode() based on the RGBA string representation. The class uses a chain-of-responsibility pattern internally with multiple Converter subclasses, each backed by a regex pattern.
Usage
Use Color to parse CSS color values returned by WebElement.getCssValue() and convert them into a consistent format for assertions. It is essential for verifying visual styling in browser tests.
Code Reference
Source Location
Signature
public class Color {
public static Color fromString(String value);
public Color(int red, int green, int blue, double alpha);
public void setOpacity(double alpha);
public String asRgb();
public String asRgba();
public String asHex();
public java.awt.Color getColor();
public String toString();
public boolean equals(Object other);
public int hashCode();
}
Import
import org.openqa.selenium.support.Color;
I/O Contract
Constructor
| Parameter |
Type |
Description
|
red |
int |
Red channel value (0-255)
|
green |
int |
Green channel value (0-255)
|
blue |
int |
Blue channel value (0-255)
|
alpha |
double |
Alpha/opacity value (0.0 to 1.0)
|
Static Factory
| Method |
Parameter |
Returns |
Description
|
fromString |
String value |
Color |
Parses a CSS color string in any supported format; throws IllegalArgumentException if unrecognized
|
Supported Input Formats for fromString()
| Format |
Example |
Description
|
| RGB |
rgb(255, 0, 0) |
Integer RGB values
|
| RGB Percentage |
rgb(100%, 0%, 0%) |
Percentage-based RGB values
|
| RGBA |
rgba(255, 0, 0, 0.5) |
Integer RGBA with alpha
|
| RGBA Percentage |
rgba(100%, 0%, 0%, 0.5) |
Percentage-based RGBA with alpha
|
| Hex (6-digit) |
#ff0000 |
Standard hex color
|
| Hex (3-digit) |
#f00 |
Shorthand hex color
|
| HSL |
hsl(0, 100%, 50%) |
Hue, saturation, lightness
|
| HSLA |
hsla(0, 100%, 50%, 0.5) |
HSL with alpha
|
| Named |
red |
CSS named color (case-insensitive, resolved via Colors enum)
|
Output Methods
| Method |
Returns |
Example Output |
Description
|
asRgb() |
String |
rgb(255, 0, 0) |
RGB format string (no alpha)
|
asRgba() |
String |
rgba(255, 0, 0, 1) |
RGBA format string with alpha
|
asHex() |
String |
#ff0000 |
6-digit lowercase hex string (no alpha)
|
getColor() |
java.awt.Color |
-- |
AWT Color object with alpha mapped to 0-255 range
|
toString() |
String |
Color: rgba(255, 0, 0, 1) |
Debug string representation
|
Mutator
| Method |
Parameter |
Description
|
setOpacity |
double alpha |
Sets the alpha/opacity value (0.0 to 1.0)
|
Usage Examples
// Parse a color from CSS value
String cssColor = element.getCssValue("background-color");
Color color = Color.fromString(cssColor);
// Parse various formats
Color fromRgb = Color.fromString("rgb(255, 128, 0)");
Color fromHex = Color.fromString("#ff8000");
Color fromHsl = Color.fromString("hsl(30, 100%, 50%)");
Color fromName = Color.fromString("orange");
// Convert to different output formats
String rgb = color.asRgb(); // "rgb(255, 128, 0)"
String rgba = color.asRgba(); // "rgba(255, 128, 0, 1)"
String hex = color.asHex(); // "#ff8000"
// Convert to java.awt.Color
java.awt.Color awtColor = color.getColor();
// Construct directly
Color custom = new Color(100, 200, 50, 0.8);
// Modify opacity
custom.setOpacity(0.5);
// Compare colors (equality is based on RGBA string representation)
Color a = Color.fromString("#ff0000");
Color b = Color.fromString("rgb(255, 0, 0)");
boolean equal = a.equals(b); // true
// Assert CSS styling in a test
Color bgColor = Color.fromString(element.getCssValue("background-color"));
assertEquals(Colors.RED.getColorValue(), bgColor);
Related Pages