Package micycle.peasygradients.gradient
Class Gradient
- java.lang.Object
-
- micycle.peasygradients.gradient.Gradient
-
public final class Gradient extends Object
Represents a one-dimensional color gradient specification, defined by a sequence ofcolor stops
along a normalized axis [0,1]. Each color stop defines both a color value and its position on this axis.The gradient specification consists of two key components:
- An interpolation function (e.g. linear, sine) that determines how colors transition between stops during sampling
- A color space (e.g. RGB, LAB) in which the color interpolation is performed
This class defines only the mathematical gradient function - it does not handle the actual rendering or rasterization. To generate a two-dimensional visualization of this gradient, use a
PeasyGradients
instance, which will sample this function to create a 2D spectrum.- Author:
- Michael Carleton
-
-
Field Summary
Fields Modifier and Type Field Description ColorSpace
colorSpace
micycle.peasygradients.utilities.Interpolation
interpolationMode
-
Constructor Summary
Constructors Constructor Description Gradient()
Creates a new gradient consisting of 2 random equidistant complementary colors.Gradient(int... colors)
Creates a gradient with equidistant color stops, using the colors provided.Gradient(List<ColorStop> colorStops)
Creates a gradient using the colorstops provided.Gradient(ColorStop... colorStops)
Creates a gradient using the colorstops provided.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(int clr, double percent)
Adds a specific color to the gradient at a given percentage.void
add(ColorStop colorStop)
Adds a color stop to the gradient.void
animate(double amt)
Increases the positional offset of all color stops by the amount given (call this each frame (within draw() for example) to animate a gradient).int
colorAt(int colorStopIndex)
Returns the ARGB color of the color stop at a given index.int
getColor(double position)
Evalutes the ARGB (Processing) color value of the gradient at the given step through its 1D color axis.int
lastcolor()
Returns the color of the this gradient's last color stop.void
mutatecolor(double amt)
Mutates the color of all color stops in the RGB255 space by the amount given.void
nextColSpace()
void
nextInterpolationMode()
void
prevColSpace()
void
prevInterpolationMode()
void
primeAnimation()
Primes the gradient for animation (pushes copy of the first color in the gradient to the end, and repositions all other color stops proportionally to where they were before), to ensure a seamless gradient spectrum, regardless of offset.void
pushColor(int color)
Pushes a new color stop to the end of the gradient (position = 1.00), and repositions all other color stops proportionally to where they were before.static Gradient
randomGradient(int numColors)
Return randomised gradient (comprised using random colors but at equidistant positions).static Gradient
randomGradientWithStops(int numColors)
Returns a randomised gradient (comprised using random colors and random positions).void
removeLast()
Removes the last color stop from the gradient and scales the position of the remaining stops such that the position of the previous second-to-last color stop is equal to the position of the removed stop.void
reverse()
Reverses the order of colors in this gradient.void
setColorSpace(ColorSpace colorSpace)
Sets the color space this gradient uses to represent colors.void
setInterpolationMode(micycle.peasygradients.utilities.Interpolation interpolation)
Sets the interpolation mode this gradient uses to generate colors between adjacent color stops.void
setOffset(double offset)
Sets the offset of all color stops to a specific value.void
setStopColor(int stopIndex, int col)
Sets the color of a color stop (given by its index).void
setStopPosition(int index, double position)
Sets the 1D position of a color stop (given by its index) to a certain position on the 1D gradient axis.String
toJavaConstructor()
Returns Java source to paste.String
toString()
Returns detailed information about the gradient.
-
-
-
Field Detail
-
colorSpace
public ColorSpace colorSpace
-
interpolationMode
public micycle.peasygradients.utilities.Interpolation interpolationMode
-
-
Constructor Detail
-
Gradient
public Gradient()
Creates a new gradient consisting of 2 random equidistant complementary colors.
-
Gradient
public Gradient(int... colors)
Creates a gradient with equidistant color stops, using the colors provided.- Parameters:
colors
- ARGB color integers (the kind returned by Processing's color() method)
-
Gradient
public Gradient(ColorStop... colorStops)
Creates a gradient using the colorstops provided.- Parameters:
colorStops
- varargs of colorstops
-
-
Method Detail
-
getColor
public int getColor(double position)
Evalutes the ARGB (Processing) color value of the gradient at the given step through its 1D color axis.This is the main method of Gradient class. Internally, the the position input undergoes is transformed by the current interpolation function.
- Parameters:
position
- a linear position expressed as a decimal between 0 and 1. Numbers outside the range of 0...1 will wrap back into the gradient- Returns:
- ARGB integer for Processing pixel array.
-
setStopColor
public void setStopColor(int stopIndex, int col)
Sets the color of a color stop (given by its index).- Parameters:
stopIndex
-col
- ARGB color integer representation
-
setStopPosition
public void setStopPosition(int index, double position)
Sets the 1D position of a color stop (given by its index) to a certain position on the 1D gradient axis. Positions < 0 or > 1 will wrap around the gradient.- Parameters:
index
-position
-
-
reverse
public void reverse()
Reverses the order of colors in this gradient. This mutates the gradient's colorstops' positions.
-
animate
public void animate(double amt)
Increases the positional offset of all color stops by the amount given (call this each frame (within draw() for example) to animate a gradient).- Parameters:
amt
- 0...1 smaller is less change- See Also:
setOffset(double)
,primeAnimation() -- consider calling this method before animate() to prevent a color seam
-
setOffset
public void setOffset(double offset)
Sets the offset of all color stops to a specific value.- Parameters:
offset
-- See Also:
animate(double)
-
mutatecolor
public void mutatecolor(double amt)
Mutates the color of all color stops in the RGB255 space by the amount given. Mutation randomises between adding or subtracting the mutation amount from each of the R,G,B channels.- Parameters:
amt
- magnitude of mutation [0...255]
-
primeAnimation
public void primeAnimation()
Primes the gradient for animation (pushes copy of the first color in the gradient to the end, and repositions all other color stops proportionally to where they were before), to ensure a seamless gradient spectrum, regardless of offset.Animating a gradient without calling
primeAnimation()
may lead to an ugly and undesirable seam in the gradient where the first and last color stops (at positions 0.00 and 1.00 respectively) bump right up against each other.
-
pushColor
public void pushColor(int color)
Pushes a new color stop to the end of the gradient (position = 1.00), and repositions all other color stops proportionally to where they were before.- Parameters:
color
-- See Also:
removeLast()
-
removeLast
public void removeLast()
Removes the last color stop from the gradient and scales the position of the remaining stops such that the position of the previous second-to-last color stop is equal to the position of the removed stop.- See Also:
pushColor(int)
-
colorAt
public int colorAt(int colorStopIndex)
Returns the ARGB color of the color stop at a given index.- Parameters:
colorStopIndex
-- Returns:
- 32bit ARGB color int
-
lastcolor
public int lastcolor()
Returns the color of the this gradient's last color stop.- Returns:
- 32bit ARGB color int
-
add
public void add(int clr, double percent)
Adds a specific color to the gradient at a given percentage.- Parameters:
clr
-percent
- 0...1
-
add
public void add(ColorStop colorStop)
Adds a color stop to the gradient.- Parameters:
colorStop
-
-
setColorSpace
public void setColorSpace(ColorSpace colorSpace)
Sets the color space this gradient uses to represent colors. This affects the appearance of the gradient when it is rendered as a 2D spectrum.- Parameters:
colorSpace
-
-
nextColSpace
public void nextColSpace()
-
prevColSpace
public void prevColSpace()
-
setInterpolationMode
public void setInterpolationMode(micycle.peasygradients.utilities.Interpolation interpolation)
Sets the interpolation mode this gradient uses to generate colors between adjacent color stops. This affects the appearance of the gradient when it is rendered as a 2D spectrum.- Parameters:
interpolation
-
-
nextInterpolationMode
public void nextInterpolationMode()
-
prevInterpolationMode
public void prevInterpolationMode()
-
randomGradientWithStops
public static Gradient randomGradientWithStops(int numColors)
Returns a randomised gradient (comprised using random colors and random positions).- Parameters:
numColors
- the number of colors the gradient is comprised of- Returns:
- a randomised gradient
- See Also:
randomGradient(int)
-
randomGradient
public static Gradient randomGradient(int numColors)
Return randomised gradient (comprised using random colors but at equidistant positions).- Parameters:
numColors
- the number of colors the gradient is comprised of- Returns:
- a randomised gradient
- See Also:
randomGradientWithStops(int)
-
toString
public String toString()
Returns detailed information about the gradient. For each color stop, returns:- Position
- RGBA representation
- Integer representation
- [Current color Space] Representation
-
toJavaConstructor
public String toJavaConstructor()
Returns Java source to paste. Call this method if a randomly generated gradient is pleasant. Export ready to construct the gradient using Processing color().- Returns:
- a constructor for this gradient, e.g. "Gradient(color(0, 0, 50), color(125, 55, 25));"
-
-