Package micycle.peasygradients.gradient
Class Gradient
- java.lang.Object
-
- micycle.peasygradients.gradient.Gradient
-
public final class Gradient extends Object
A Gradient is a color spectrum, comprising ofcolor stops
(each specifying a color and a position) arranged on a 1D axis.Gradients each define the gradient interpolation function (such as sine()), which governs how the gradient's color transitions (the step used during interpolation) and the color space this gradient will be rendered this gradient's color stops are represented by.
Use a {@link #peasyGradients.PeasyGradients} instance to render Gradients as 2D spectrums.
- 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, float 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(float 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(float 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(float 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(float 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, float 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(float 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, float 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(float 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(float)
,primeAnimation() -- consider calling this method before animate() to prevent a color seam
-
setOffset
public void setOffset(float offset)
Sets the offset of all color stops to a specific value.- Parameters:
offset
-- See Also:
animate(float)
-
mutatecolor
public void mutatecolor(float 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, float 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));"
-
-