Represents a linear gradient paint with two or more colors.
import smile.colors.PresetColor.*
import smile.modeling.*
import smile.pictures.*
val stops = Seq(0.0 -> MidnightBlue, 0.55 -> Tomato, 1.0 -> Gold)
val sunset = Rectangle(160, 100, fillWith(LinearGradient(Pos(0, 0), Pos(0, 100), stops)), None)
val sea = Rectangle(160, 40, fillWith(LinearGradient(Pos(0, 0), Pos(160, 0), Teal, Navy)), None)

Coordinate contract
start and end are element-local coordinates whose origin is the top-left corner of the shape's own, untransformed box (a text's layout box), widened by a stroke's radius. For a shape that is not turned or scaled, Pos(0, 0) is the top-left corner of its bounds and Pos(bounds.width, bounds.height) is the opposite one, so
val gradient = LinearGradient(Pos(0, 0), Pos(width, 0), red, blue)
always paints a horizontal red-to-blue gradient across the whole shape, no matter where the shape sits in the picture. They are not world coordinates. The same gradient on two shapes paints each of them from its own corner. Every renderer (Java2D on the JVM, canvas and SVG on Scala.js) resolves the points this way.
The frame travels with the element. Coordinates outside the box are legal and place the gradient's ends outside the shape, where cycleMethod takes over. A gradient lives in its shape's own frame, and every renderer paints it under the shape's affine. So a moved, rotated, scaled or mirrored element moves, turns, stretches or mirrors its gradient rigidly with it, and the gradient's points never change.
What a gradient has to be
A gradient that can be constructed can be painted, on every backend. Its two points are finite and differ, since a gradient without a length has no direction to run in. There are at least two stops, and their fractions lie within zero to one and do not run backwards. Two stops that share a fraction make a hard edge from one color to the next.
A gradient so short that a renderer's floats cannot tell its two points apart is painted with its smile.colors.LinearGradient.averageColor.
Value parameters
- cycleMethod
-
The method used to repeat the gradient pattern if the shape to be filled is larger than the gradient defined by the start and end points.
- end
-
The ending position of the gradient, relative to the top-left corner of the shape's own, untransformed box.
- start
-
The starting position of the gradient, relative to the top-left corner of the shape's own, untransformed box.
- stops
-
The colors of the gradient, each at its fraction of the way from
starttoend.
Attributes
- Companion
- object
- Source
- LinearGradient.scala
- Graph
-
- Supertypes
