Package com.oorian.css
Class Gradient
java.lang.Object
com.oorian.css.Gradient
A builder for composing CSS gradient values.
Gradient provides a type-safe, fluent API for building CSS gradient strings
including linear-gradient(), radial-gradient(), and conic-gradient().
Gradients are used as CSS image values for properties like background-image.
Usage:
// Linear gradient
element.setBackgroundImage(Gradient.linear("to right")
.addStop("#ff0000", "0%")
.addStop("#0000ff", "100%"));
// Result: background-image: linear-gradient(to right, #ff0000 0%, #0000ff 100%)
// Radial gradient
element.setBackgroundImage(Gradient.radial("circle")
.addStop("red")
.addStop("blue"));
// Result: background-image: radial-gradient(circle, red, blue)
// Conic gradient with angle
element.setBackgroundImage(Gradient.conic("from 45deg")
.addStop("red")
.addStop("yellow")
.addStop("green"));
// Result: background-image: conic-gradient(from 45deg, red, yellow, green)
// Repeating gradient
element.setBackgroundImage(Gradient.linear(45)
.addStop("#000", "0px")
.addStop("#000", "10px")
.addStop("#fff", "10px")
.addStop("#fff", "20px")
.repeating());
// Result: background-image: repeating-linear-gradient(45deg, #000 0px, ...)
// Use with CssRule
CssRule rule = new CssRule(".gradient-bg");
rule.setBackgroundImage(Gradient.linear().addStop("red").addStop("blue"));
- Since:
- 2026
- Version:
- 1.0
- Author:
- Marvin P. Warble Jr.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionAdds a color stop using aColorobject.Adds a color stop using aColorobject at a specific percentage position.Adds a color stop using aColorobject at a specific position.Adds a color stop with no position.Adds a color stop at a specific percentage position.Adds a color stop at a specific position.static Gradientconic()Creates a conic gradient with the default starting angle.static Gradientconic(int degrees) Creates a conic gradient with a starting angle in degrees.static GradientCreates a conic gradient with a starting angle.static Gradientlinear()Creates a linear gradient with the default direction (top to bottom).static Gradientlinear(int degrees) Creates a linear gradient with an angle in degrees.static GradientCreates a linear gradient with a direction or angle string.static Gradientradial()Creates a radial gradient with the default shape.static GradientCreates a radial gradient with a shape or size specification.Converts this gradient to a repeating variant.toString()Returns the CSS gradient string.
-
Method Details
-
linear
Creates a linear gradient with the default direction (top to bottom).- Returns:
- a new Gradient builder
-
linear
Creates a linear gradient with a direction or angle string.- Parameters:
direction- the gradient direction (e.g.,"to right","to bottom left")- Returns:
- a new Gradient builder
-
linear
Creates a linear gradient with an angle in degrees.- Parameters:
degrees- the gradient angle in degrees (e.g.,45for a diagonal)- Returns:
- a new Gradient builder
-
radial
Creates a radial gradient with the default shape.- Returns:
- a new Gradient builder
-
radial
Creates a radial gradient with a shape or size specification.- Parameters:
shape- the gradient shape (e.g.,"circle","ellipse","circle at center","closest-side")- Returns:
- a new Gradient builder
-
conic
Creates a conic gradient with the default starting angle.- Returns:
- a new Gradient builder
-
conic
Creates a conic gradient with a starting angle.- Parameters:
fromAngle- the starting angle (e.g.,"from 45deg","from 0.25turn at 50% 50%")- Returns:
- a new Gradient builder
-
conic
Creates a conic gradient with a starting angle in degrees.- Parameters:
degrees- the starting angle in degrees (e.g.,45for a 45-degree offset)- Returns:
- a new Gradient builder
-
addStop
Adds a color stop with no position.- Parameters:
color- the CSS color value (e.g.,"red","#ff0000","rgba(0,0,0,0.5)")- Returns:
- this Gradient for method chaining
-
addStop
Adds a color stop at a specific position.- Parameters:
color- the CSS color value (e.g.,"red","#ff0000")position- the stop position (e.g.,"50%","100px")- Returns:
- this Gradient for method chaining
-
addStop
Adds a color stop using aColorobject.- Parameters:
color- the Color object- Returns:
- this Gradient for method chaining
-
addStop
Adds a color stop using aColorobject at a specific position.- Parameters:
color- the Color objectposition- the stop position (e.g.,"50%","100px")- Returns:
- this Gradient for method chaining
-
addStop
Adds a color stop at a specific percentage position.- Parameters:
color- the CSS color value (e.g.,"red","#ff0000")percent- the stop position as a percentage (e.g.,50for 50%)- Returns:
- this Gradient for method chaining
-
addStop
Adds a color stop using aColorobject at a specific percentage position.- Parameters:
color- the Color objectpercent- the stop position as a percentage (e.g.,50for 50%)- Returns:
- this Gradient for method chaining
-
repeating
Converts this gradient to a repeating variant.Changes the gradient type from
linear-gradienttorepeating-linear-gradient,radial-gradienttorepeating-radial-gradient, etc.- Returns:
- this Gradient for method chaining
-
toString
Returns the CSS gradient string.
-