LinearGradient

smile.colors.LinearGradient
See theLinearGradient companion object
final case class LinearGradient(start: Pos, end: Pos, stops: Seq[(Double, Color)], cycleMethod: CycleMethod = ...) extends Paint

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)

A sky shading from midnight blue through red to gold at the horizon A
stripe shading from teal on the left to navy on the right

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 start to end.

Attributes

Companion
object
Source
LinearGradient.scala
Graph
Supertypes
trait Serializable
trait Product
trait Equals
trait Paint
class Object
trait Matchable
class Any
Show all

Members list

Value members

Inherited methods

An iterator over the names of all the elements of this product.

An iterator over the names of all the elements of this product.

Attributes

Inherited from:
Product
Source
Product.scala

An iterator over all the elements of this product.

An iterator over all the elements of this product.

Attributes

Returns

in the default implementation, an Iterator[Any]

Inherited from:
Product
Source
Product.scala

Concrete fields

lazy val averageColor: Color

The color this gradient amounts to over its length. Each color is weighted by how much of the way from start to end it covers, and by its opacity. It matches what the renderers draw, which interpolate the stops' colors and opacity separately, unpremultiplied, so a transparent stop's color still shows on the way to it. Red to a transparent white averages to a translucent pink. Before the first stop and after the last, their colors hold. A gradient with no opacity anywhere averages to a transparent black.

The color this gradient amounts to over its length. Each color is weighted by how much of the way from start to end it covers, and by its opacity. It matches what the renderers draw, which interpolate the stops' colors and opacity separately, unpremultiplied, so a transparent stop's color still shows on the way to it. Red to a transparent white averages to a translucent pink. Before the first stop and after the last, their colors hold. A gradient with no opacity anywhere averages to a transparent black.

Attributes

Source
LinearGradient.scala