Class Gradient

java.lang.Object
com.oorian.css.Gradient

public class Gradient extends Object
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 Details

    • linear

      public static Gradient linear()
      Creates a linear gradient with the default direction (top to bottom).
      Returns:
      a new Gradient builder
    • linear

      public static Gradient linear(String direction)
      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

      public static Gradient linear(int degrees)
      Creates a linear gradient with an angle in degrees.
      Parameters:
      degrees - the gradient angle in degrees (e.g., 45 for a diagonal)
      Returns:
      a new Gradient builder
    • radial

      public static Gradient radial()
      Creates a radial gradient with the default shape.
      Returns:
      a new Gradient builder
    • radial

      public static Gradient radial(String shape)
      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

      public static Gradient conic()
      Creates a conic gradient with the default starting angle.
      Returns:
      a new Gradient builder
    • conic

      public static Gradient conic(String fromAngle)
      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

      public static Gradient conic(int degrees)
      Creates a conic gradient with a starting angle in degrees.
      Parameters:
      degrees - the starting angle in degrees (e.g., 45 for a 45-degree offset)
      Returns:
      a new Gradient builder
    • addStop

      public Gradient addStop(String color)
      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

      public Gradient addStop(String color, String position)
      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

      public Gradient addStop(Color color)
      Adds a color stop using a Color object.
      Parameters:
      color - the Color object
      Returns:
      this Gradient for method chaining
    • addStop

      public Gradient addStop(Color color, String position)
      Adds a color stop using a Color object at a specific position.
      Parameters:
      color - the Color object
      position - the stop position (e.g., "50%", "100px")
      Returns:
      this Gradient for method chaining
    • addStop

      public Gradient addStop(String color, int percent)
      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., 50 for 50%)
      Returns:
      this Gradient for method chaining
    • addStop

      public Gradient addStop(Color color, int percent)
      Adds a color stop using a Color object at a specific percentage position.
      Parameters:
      color - the Color object
      percent - the stop position as a percentage (e.g., 50 for 50%)
      Returns:
      this Gradient for method chaining
    • repeating

      public Gradient repeating()
      Converts this gradient to a repeating variant.

      Changes the gradient type from linear-gradient to repeating-linear-gradient, radial-gradient to repeating-radial-gradient, etc.

      Returns:
      this Gradient for method chaining
    • toString

      public String toString()
      Returns the CSS gradient string.
      Overrides:
      toString in class Object
      Returns:
      the gradient function string (e.g., "linear-gradient(to right, #ff0000 0%, #0000ff 100%)")